DNMP(Docker + Nginx/Openresty + MySQL5,8 + PHP5,7,8 + Redis + ElasticSearch + MongoDB + RabbitMQ)是一款全功能的LNMP一键安装程序,支持Arm CPU。
使用前最好提前阅读一遍目录,以便快速上手,遇到问题也能及时排除。
QQ交流群:
- 1群:572041090(已满)
- 2群:300723526(已满)
- 3群:878913761(有位)
[ENGLISH] - [GitHub地址] - [Gitee地址]
DNMP项目特点:
100%
开源100%
遵循Docker标准- 支持多版本PHP共存,可任意切换(PHP5.4、PHP5.6、PHP7.1、PHP7.2、PHP7.3、PHP7.4、PHP8.0)
- 支持绑定任意多个域名
- 支持HTTPS和HTTP/2
- PHP源代码、MySQL数据、配置文件、日志文件都可在Host中直接修改查看
- 内置完整PHP扩展安装命令
- 默认支持
pdo_mysql
、mysqli
、mbstring
、gd
、curl
、opcache
等常用热门扩展,根据环境灵活配置 - 可一键选配常用服务:
- 多PHP版本:PHP5.4、PHP5.6、PHP7.0-7.4、PHP8.0
- Web服务:Nginx、Openresty
- 数据库:MySQL5、MySQL8、Redis、memcached、MongoDB、ElasticSearch
- 消息队列:RabbitMQ
- 辅助工具:Kibana、Logstash、phpMyAdmin、phpRedisAdmin、AdminMongo
- 实际项目中应用,确保
100%
可用 - 所有镜像源于Docker官方仓库,安全可靠
- 一次配置,Windows、Linux、MacOs皆可用
- 支持快速安装扩展命令
install-php-extensions apcu
- 支持安装certbot获取免费https用的SSL证书
/
├── data 数据库数据目录
│ ├── esdata ElasticSearch 数据目录
│ ├── mongo MongoDB 数据目录
│ ├── mysql MySQL8 数据目录
│ └── mysql5 MySQL5 数据目录
├── services 服务构建文件和配置文件目录
│ ├── elasticsearch ElasticSearch 配置文件目录
│ ├── mysql MySQL8 配置文件目录
│ ├── mysql5 MySQL5 配置文件目录
│ ├── nginx Nginx 配置文件目录
│ ├── php PHP5.6 - PHP7.4 配置目录
│ ├── php54 PHP5.4 配置目录
│ └── redis Redis 配置目录
├── logs 日志目录
├── docker-compose.sample.yml Docker 服务配置示例文件
├── env.smaple 环境配置示例文件
└── www PHP 代码目录
- 本地安装
git
Docker
(系统需为Linux,Windows 10 Build 15063+,或MacOS 10.12+,且必须要64
位)docker-compose 1.7.0+
clone
项目:$ git clone https://github.com/yeszao/dnmp.git # 假如速度太慢,可以使用加速拉取镜像 $ git clone https://github.com.cnpmjs.org/yeszao/dnmp.git
- 如果主机是 Linux系统,且当前用户不是
root
用户,还需将当前用户加入docker
用户组:$ sudo gpasswd -a ${USER} docker
- 拷贝并命名配置文件(Windows系统请用
copy
命令),启动:$ cd dnmp # 进入项目目录 $ cp env.sample .env # 复制环境变量文件 $ cp docker-compose.sample.yml docker-compose.yml # 复制 docker-compose 配置文件。默认启动3个服务: # Nginx、PHP7和MySQL8。要开启更多其他服务,如Redis、 # PHP5.6、PHP5.4、MongoDB,ElasticSearch等,请删 # 除服务块前的注释 $ docker-compose up # 启动
- 在浏览器中访问:
https://localhost
或https://localhost
(自签名HTTPS演示)就能看到效果,PHP代码在文件./www/localhost/index.php
。
首先,需要启动其他版本的PHP,比如PHP5.4,那就先在docker-compose.yml
文件中删除PHP5.4前面的注释,再启动PHP5.4容器。
PHP5.4启动后,打开Nginx 配置,修改fastcgi_pass
的主机地址,由php
改为php54
,如下:
fastcgi_pass php:9000;
为:
fastcgi_pass php54:9000;
其中 php
和 php54
是docker-compose.yml
文件中服务器的名称。
最后,重启 Nginx 生效。
$ docker exec -it nginx nginx -s reload
这里两个nginx
,第一个是容器名,第二个是容器中的nginx
程序。
PHP的很多功能都是通过扩展实现,而安装扩展是一个略费时间的过程,
所以,除PHP内置扩展外,在env.sample
文件中我们仅默认安装少量扩展,
如果要安装更多扩展,请打开你的.env
文件修改如下的PHP配置,
增加需要的PHP扩展:
PHP_EXTENSIONS=pdo_mysql,opcache,redis # PHP 要安装的扩展列表,英文逗号隔开
PHP54_EXTENSIONS=opcache,redis # PHP 5.4要安装的扩展列表,英文逗号隔开
然后重新build PHP镜像。
docker-compose build php
可用的扩展请看同文件的env.sample
注释块说明。
1.进入容器:
docker exec -it php /bin/sh
install-php-extensions apcu
2.支持快速安装扩展列表
Extension | PHP 5.5 | PHP 5.6 | PHP 7.0 | PHP 7.1 | PHP 7.2 | PHP 7.3 | PHP 7.4 | PHP 8.0 | PHP 8.1 |
---|---|---|---|---|---|---|---|---|---|
amqp | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
apcu | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
apcu_bc | ✓ | ✓ | ✓ | ✓ | ✓ | ||||
ast | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||
bcmath | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
blackfire | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
bz2 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
calendar | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
cmark | ✓ | ✓ | ✓ | ✓ | ✓ | ||||
csv | ✓ | ✓ | ✓ | ✓ | |||||
dba | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
decimal | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||
ds | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||
enchant | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
ev | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
event | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
excimer | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |||
exif | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
ffi | ✓ | ✓ | ✓ | ||||||
gd | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
gearman | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |
geoip | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||
geospatial | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
gettext | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
gmagick | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
gmp | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
gnupg | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
grpc | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
http | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
igbinary | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
imagick | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
imap | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
inotify | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
interbase | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |||
intl | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
ioncube_loader | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||
jsmin | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||
json_post | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
ldap | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
lzf | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
mailparse | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
maxminddb | ✓ | ✓ | ✓ | ✓ | ✓ | ||||
mcrypt | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
memcache | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
memcached | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
mongo | ✓ | ✓ | |||||||
mongodb | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
mosquitto | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||
msgpack | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
mssql | ✓ | ✓ | |||||||
mysql | ✓ | ✓ | |||||||
mysqli | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
oauth | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
oci8 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
odbc | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
opcache | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
opencensus | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||
openswoole | ✓ | ✓ | ✓ | ✓ | ✓ | ||||
parallel* | ✓ | ✓ | ✓ | ✓ | |||||
pcntl | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
pcov | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||
pdo_dblib | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |
pdo_firebird | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
pdo_mysql | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
pdo_oci | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||
pdo_odbc | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
pdo_pgsql | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
pdo_sqlsrv* | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||
pgsql | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
propro | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||
protobuf | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |
pspell | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
pthreads* | ✓ | ✓ | ✓ | ||||||
raphf | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
rdkafka | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
recode | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |||
redis | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
seaslog | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
shmop | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
smbclient | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
snmp | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
snuffleupagus | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||
soap | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
sockets | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
solr | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
sourceguardian | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |
spx | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |
sqlsrv* | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||
ssh2 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
stomp | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||
swoole | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
sybase_ct | ✓ | ✓ | |||||||
sysvmsg | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
sysvsem | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
sysvshm | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
tensor* | ✓ | ✓ | ✓ | ✓ | |||||
tidy | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
timezonedb | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
uopz | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
uploadprogress | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
uuid | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
vips* | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||
wddx | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |||
xdebug | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
xhprof | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
xlswriter | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||
xmldiff | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
xmlrpc | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
xsl | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
yac | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||
yaml | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
yar | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
zephir_parser | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||
zip | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
zookeeper | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
zstd | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Number of supported extensions: 116
此扩展来自https://github.com/mlocati/docker-php-extension-installer 参考示例文件
- 参考bash.alias.sample示例文件,将对应 php cli 函数拷贝到主机的
~/.bashrc
文件。 - 让文件起效:
source ~/.bashrc
- 然后就可以在主机中执行php命令了:
~ php -v PHP 7.2.13 (cli) (built: Dec 21 2018 02:22:47) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.13, Copyright (c) 1999-2018, by Zend Technologies with Xdebug v2.6.1, Copyright (c) 2002-2018, by Derick Rethans
方法1:主机中使用composer命令
- 确定composer缓存的路径。比如,我的dnmp下载在
~/dnmp
目录,那composer的缓存路径就是~/dnmp/data/composer
。 - 参考bash.alias.sample示例文件,将对应 php composer 函数拷贝到主机的
~/.bashrc
文件。这里需要注意的是,示例文件中的
~/dnmp/data/composer
目录需是第一步确定的目录。 - 让文件起效:
source ~/.bashrc
- 在主机的任何目录下就能用composer了:
cd ~/dnmp/www/ composer create-project yeszao/fastphp project --no-dev
- (可选)第一次使用 composer 会在
~/dnmp/data/composer
目录下生成一个config.json文件,可以在这个文件中指定国内仓库,例如:{ "config": {}, "repositories": { "packagist": { "type": "composer", "url": "https://mirrors.aliyun.com/composer/" } } }
方法二:容器内使用composer命令
还有另外一种方式,就是进入容器,再执行composer
命令,以PHP7容器为例:
docker exec -it php /bin/sh
cd /www/localhost
composer update
如需管理服务,请在命令后面加上服务器名称,例如:
$ docker-compose up # 创建并且启动所有容器
$ docker-compose up -d # 创建并且后台运行方式启动所有容器
$ docker-compose up nginx php mysql # 创建并且启动nginx、php、mysql的多个容器
$ docker-compose up -d nginx php mysql # 创建并且已后台运行的方式启动nginx、php、mysql容器
$ docker-compose start php # 启动服务
$ docker-compose stop php # 停止服务
$ docker-compose restart php # 重启服务
$ docker-compose build php # 构建或者重新构建服务
$ docker-compose rm php # 删除并且停止php容器
$ docker-compose down # 停止并删除容器,网络,图像和挂载卷
在开发的时候,我们可能经常使用docker exec -it
进入到容器中,把常用的做成命令别名是个省事的方法。
首先,在主机中查看可用的容器:
$ docker ps # 查看所有运行中的容器
$ docker ps -a # 所有容器
输出的NAMES
那一列就是容器的名称,如果使用默认配置,那么名称就是nginx
、php
、php56
、mysql
等。
然后,打开~/.bashrc
或者~/.zshrc
文件,加上:
alias dnginx='docker exec -it nginx /bin/sh'
alias dphp='docker exec -it php /bin/sh'
alias dphp56='docker exec -it php56 /bin/sh'
alias dphp54='docker exec -it php54 /bin/sh'
alias dmysql='docker exec -it mysql /bin/bash'
alias dredis='docker exec -it redis /bin/sh'
下次进入容器就非常快捷了,如进入php容器:
$ dphp
ifconfig docker0
用于填写extra_hosts
容器访问宿主机的hosts
地址
Log文件生成的位置依赖于conf下各log配置的值。
Nginx日志是我们用得最多的日志,所以我们单独放在根目录log
下。
log
会目录映射Nginx容器的/var/log/nginx
目录,所以在Nginx配置文件中,需要输出log的位置,我们需要配置到/var/log/nginx
目录,如:
error_log /var/log/nginx/nginx.localhost.error.log warn;
大部分情况下,PHP-FPM的日志都会输出到Nginx的日志中,所以不需要额外配置。
另外,建议直接在PHP中打开错误日志:
error_reporting(E_ALL);
ini_set('error_reporting', 'on');
ini_set('display_errors', 'on');
如果确实需要,可按一下步骤开启(在容器中)。
- 进入容器,创建日志文件并修改权限:
$ docker exec -it php /bin/sh $ mkdir /var/log/php $ cd /var/log/php $ touch php-fpm.error.log $ chmod a+w php-fpm.error.log
- 主机上打开并修改PHP-FPM的配置文件
conf/php-fpm.conf
,找到如下一行,删除注释,并改值为:php_admin_value[error_log] = /var/log/php/php-fpm.error.log
- 重启PHP-FPM容器。
因为MySQL容器中的MySQL使用的是mysql
用户启动,它无法自行在/var/log
下的增加日志文件。所以,我们把MySQL的日志放在与data一样的目录,即项目的mysql
目录下,对应容器中的/var/log/mysql/
目录。
slow-query-log-file = /var/log/mysql/mysql.slow.log
log-error = /var/log/mysql/mysql.error.log
以上是mysql.conf中的日志文件的配置。
本项目默认在docker-compose.yml
中不开启了用于MySQL在线管理的phpMyAdmin,以及用于redis在线管理的phpRedisAdmin,可以根据需要修改或删除。
phpMyAdmin容器映射到主机的端口地址是:8080
,所以主机上访问phpMyAdmin的地址是:
https://localhost:8080
MySQL连接信息:
- host:(本项目的MySQL容器网络)
- port:
3306
- username:(手动在phpmyadmin界面输入)
- password:(手动在phpmyadmin界面输入)
phpRedisAdmin容器映射到主机的端口地址是:8081
,所以主机上访问phpMyAdmin的地址是:
https://localhost:8081
Redis连接信息如下:
- host: (本项目的Redis容器网络)
- port:
6379
要在正式环境中使用,请:
- 在php.ini中关闭XDebug调试
- 增强MySQL数据库访问的安全策略
- 增强redis访问的安全策略
参考这个issue:yeszao#91
容器时间在.env文件中配置TZ
变量,所有支持的时区请看时区列表·维基百科或者PHP所支持的时区列表·PHP官网。
这要分两种情况,
第一种情况,在PHP代码中。
// 连接MySQL
$dbh = new PDO('mysql:host=mysql;dbname=mysql', 'root', '123456');
// 连接Redis
$redis = new Redis();
$redis->connect('redis', 6379);
因为容器与容器是expose
端口联通的,而且在同一个networks
下,所以连接的host
参数直接用容器名称,port
参数就是容器内部的端口。更多请参考《docker-compose ports和expose的区别》。
第二种情况,在主机中通过命令行或者Navicat等工具连接。主机要连接mysql和redis的话,要求容器必须经过ports
把端口映射到主机了。以 mysql 为例,docker-compose.yml
文件中有这样的ports
配置:3306:3306
,就是主机的3306和容器的3306端口形成了映射,所以我们可以这样连接:
$ mysql -h127.0.0.1 -uroot -p123456 -P3306
$ redis-cli -h127.0.0.1
这里host
参数不能用localhost是因为它默认是通过sock文件与mysql通信,而容器与主机文件系统已经隔离,所以需要通过TCP方式连接,所以需要指定IP。
1.宿主机执行ifconfig docker0
得到inet
就是要连接的ip
地址
$ ifconfig docker0
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
...
2.运行宿主机Mysql命令行
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
mysql>flush privileges;
// 其中各字符的含义:
// *.* 对任意数据库任意表有效
// "root" "123456" 是数据库用户名和密码
// '%' 允许访问数据库的IP地址,%意思是任意IP,也可以指定IP
// flush privileges 刷新权限信息
3.接着直接php容器使用172.0.17.1:3306
连接即可
- 目前使用mysql-server
8.0.28
以上的版本,php版本需要7.4.7
以上才能连接
MIT