Docker LNMP环境搭建


本文介绍如何使用Docker容器,快速部署LNMP环境。
最终完成的环境我们称为:DNMP(即 Docker + Nginx + MySQL + PHP)。

最终实现一键部署LNMP环境的目的,该环境特点:

  1. 完全开源
  2. 支持多版本PHP切换(PHP5.4、PHP5.6、PHP7.2...)
  3. 支持绑定任意多个域名
  4. 支持HTTPS和HTTP/2
  5. PHP源代码位于host
  6. MySQL data位于host
  7. 所有配置文件可在host中直接修改
  8. 所有日志文件可在host中直接查看
  9. 内置完整PHP扩展安装命令
  10. 本站实际使用,确保100%可用
  11. 实现一次配置,Windows、Linux、MacOs皆可用

github仓库地址https://github.com/yeszao/dnmp

1 快速使用

1、安装gitdockerdocker-compose

2、使用git下载完整代码:

$ git clone https://github.com/yeszao/dnmp.git

3、用docker-compose命令启动容器,首次使用需要下载镜像,会稍慢:

$ docker-compose up

4、然后在浏览器中访问localhost,就可以看到如下页面:

5、源代码在:./www/site1/ 目录下。

2 安装docker和docker-compose

具体安装步骤:

注意:Docker安装要求Linux 3.10以上版本,用uname -a命令可查看到。

安装之后,可查看版本:

$ docker -v
$ docker-compose -v

然后把当前用户加到docker用户组里面:

$ sudo gpasswd -a ${USER} docker

就不用每次启动Docker都得加sudo了。

注意,执行gpasswd命令之后要重新登陆才有效。

3 使用国内镜像仓库

在没有梯子的情况下,Docker默认从Docker Hub仓库下载镜像,完整的LNMP镜像估计需要一两天时间。

所以只能换个途径,比如使用阿里云的加速仓库。

首先注册一个阿里云账号,然后访问阿里云的Docker镜像仓库,能找到加速器地址。

对于Docker 1.10+,打开配置文件 /etc/docker/daemon.json(没有时新建该文件):

{
    "registry-mirrors": ["https://6evg8u3r.mirror.aliyuncs.com"]
}

以上是我的加速地址。

然后重启Docker Daemon:

sudo systemctl daemon-reload
sudo systemctl restart docker

就可以了。

Docker 1.10以下请看:https://yq.aliyun.com/articles/29941

4 目录说明

大致框架如下:

(图片使用UMLet绘制,下载原文件

4.1 目录结构

目录结构如下:

.
├── docker-compose.yml          容器启动配置文件
├── Dockerfile                  PHP-FPM构建配置文件
├── conf                        配置目录
│   ├── mysql                   MySQL配置文件目录
│   │   └── my.cnf              MySQL配置文件
│   ├── nginx                   Nginx配置文件目录
│   │   ├── conf.d              站点配置文件目录
│   │   │   ├── certs           SSL认证文件、密钥和加密文件目录
│   │   │   │   └── site2       站点2的认证文件目录
│   │   │   ├── site1.conf      站点1 Nginx配置文件
│   │   │   └── site2.conf      站点2 Nginx配置文件
│   │   └── nginx.conf          Nginx通用配置文件
│   └── php                     PHP配置目录
│       ├── php-fpm.d           PHP-FPM配置目录
│       │   └── www.conf        PHP-FPM配置文件
│       └── php.ini             PHP配置文件
├── log                         日志目录
│   ├── mysql                   MySQL日志目录
│   ├── nginx                   Nginx日志目录
│   └── php-fpm                 PHP-FPM日志目录
├── mysql                       MySQL数据文件目录
└── www                         站点根目录
    ├── site1                   站点1根目录
    └── site2                   站点2根目录

4.2 站点部署

本文有默认加了两个站点:www.site1.com(同localhost)和www.site2.com

要在本地访问这两个域名,需要修改你的hosts文件,添加以下两行:

127.0.0.1 www.site1.com
127.0.0.1 www.site2.com

其中,www.site2.com支持SSL/https和HTTP/2的示例站点

因为站点2的SSL采用自签名方式,所以浏览器有安全提示,继续访问就可以了,自己的站点用第三方SSL认证证书替换即可。

如果只用到站点1,把站点2相关的目录和配置文件删除:

./conf/nginx/conf.d/certs/site2/
./conf/nginx/conf.d/site2.conf
./www/site2/

重启容器内的Nginx生效:

docker exec -it dlnmp_nginx_1 nginx -s reload

4.3 HTTPS使用

在容器中我们也是可以用HTTPS的,具体的配置请参考如下文件:

./conf/nginx/conf.d/site2.conf

如果是自签名,可以用廖雪峰提供的一个自动生成认证文件、私钥脚本:gencert.sh

这个脚本已经放在项目中,在这个目录下:

./conf/nginx/conf.d/certs/site2/

在Bash中输入:

$ ./gencert.sh

输入一次域名,和几次密码(内容随意)后,就会生成几个认证文件。

其中自签名情况不需要.csr.origin.key后缀的文件。

然后修改Nginx配置文件,配置SSL支持就可以了。

5 docker-compose.yml文件

如下是docker容器的运行配置docker-compose.yml的内容:

nginx:
  image: nginx:alpine
  ports:
    - "80:80"
    - "443:443"
  volumes:
    - ./www/:/var/www/html/:rw
    - ./conf/nginx/conf.d:/etc/nginx/conf.d/:ro
    - ./conf/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
    - ./log/nginx/:/var/log/nginx/:rw
  links:
    - php-fpm:fpm

php-fpm:
  build: .
  expose:
    - "9000"
  volumes:
    - ./www/:/var/www/html/:rw
    - ./conf/php/php.ini:/usr/local/etc/php/php.ini:ro
    - ./conf/php/php-fpm.d/www.conf:/usr/local/etc/php-fpm.d/www.conf:rw
    - ./log/php-fpm/:/var/log/php-fpm/:rw
  links:
    - mysql:mysql
    - redis:redis

mysql:
  image: mysql:latest
  ports:
    - "3306:3306"
  volumes:
    - ./conf/mysql/my.cnf:/etc/mysql/my.cnf:ro
    - ./mysql/:/var/lib/mysql/:rw
    - ./log/mysql/:/var/log/mysql/:rw
  environment:
    MYSQL_ROOT_PASSWORD: "123456"

redis:
  image: redis:latest
  ports:
    - "6379:6379"

这里我们用了nginx的alpine镜像,以及php-fpm和MySQL的最新镜像。

这里默认加了redis容器,不需要的话可以把文件的最后4以及php-fpm块下的- redis:redis删除。

5.1 为什么用nginx:alpine?

因为相比nginx:latestnginx:alpine有几点优势:

  1. 用的是最新版nginx镜像,功能与nginx:latest一模一样
  2. alpine镜像用的是Alpine Linux内核,比ubuntu内核要小很多。
  3. nginx:alpine默认支持http2。

Alpine的更多请看《Alpine Linux,一个只有5M的Docker镜像》。

如要进入alpine容器,命令是(后面的路径不是/bin/bash):

$ docker exec -it dnmp_nginx_1 /bin/sh

5.2 站点根目录挂载

为什么站点根目录在Nginx和PHP-FPM都这样挂载?

./www/:/var/www/html/

我们知道,Nginx配置都有这样一项:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

其中,$document_root就是server块下root所指的路径:

server {
    #...
    root /var/www/html;
    #...
}

这里$document_root就是/var/www/html

如果Nginx和PHP-FPM在同一主机,Nginx会通过9000端口把这个目录值脚本URI传给PHP-FPM。

PHP-FPM再通过9000端口接收Nginx发过来的目录值和脚本URI,发给PHP解析。

PHP收到后,就到指定的目录下查找PHP文件并解析,完成后再通过9000端口返回给Nginx。

如果Nginx和PHP-FPM在同一个主机里面,PHP就总能找到Nginx指定的目录。

但是,如果他们在不同的容器呢?

未做任何处理的情况,Nginx容器中的站点根目录,PHP-FPM容器肯定不存在。

所以,这里需要保证Nginx和PHP-FPM都挂载了Host的./www/,并且都挂载在容器的:/var/www/html

(当然,你也可以指定别的目录,确保统一即可)

5.3 修改docker-compose.yml文件之后?

如果容器已经生成,回头再编辑docker-compose.yml,用

docker-compose up

命令会直接启动原来的容器,修改的内容不会体现在启动的容器里

所以,要使修改的docker-compose.yml生效,需要以下4步:

$ docker stop dnmp_nginx_1                      # 第一步:停止容器
$ docker rm dnmp_nginx_1                        # 第二步:删除容器
# !!第三步:重启Docker服务!!
$ docker-compose up -d --no-deps --build mysql  # 第四步:重新启动容器

其中最后一条命令参数作用:

  • -d:后台执行
  • --no-deps:不启动link的容器
  • --build:启动容器前先构建镜像

6 Dockerfile文件

因为PHP-FPM构建稍微复杂,涉及到很多扩展。

所以单独用Dockerfile文件构建PHP-FPM:

FROM php:fpm

## Copy sources.list to container.
## Here we use 163.com sources list.
##     PHP 5.6.31+ should use jessie sources list
##     PHP 7.2.0+ should use stretch sources list
## For more please check:
## PHP official docker repository: https://hub.docker.com/r/library/php/
#COPY ./files/sources.list.stretch /etc/apt/sources.list
#COPY ./files/sources.list.jessie /etc/apt/sources.list

## Update Ubuntu
RUN apt-get update

## mcrypt
RUN apt-get install -y libmcrypt-dev
RUN docker-php-ext-install mcrypt

## GD
RUN apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng12-dev
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
RUN docker-php-ext-install -j$(nproc) gd

## Intl
RUN apt-get install -y libicu-dev
RUN docker-php-ext-install -j$(nproc) intl

## General
RUN docker-php-ext-install zip
RUN docker-php-ext-install pcntl
RUN docker-php-ext-install opcache
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-install mysqli
RUN docker-php-ext-install mbstring

## ...

完整Dockerfile请移步本项目GitHub仓库

7 nginx配置文件

这里,我们还需要稍微修改nginx配置文件:

location ~ \.php$ {
    fastcgi_pass   fpm:9000;
    fastcgi_index  index.php;
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
}

这里,我们仅需把fastcgi_pass改成:fpm:9000

也就是,PHP-FPM的侦听主机改成:Nginx links PHP-FPM容器的别名,在docker-compose.yml文件里面我们设置为fpm

这样Nginx容器启动的时候,就会自动修改自己的/etc/hosts,让fpm指向php-fpm容器的IP。

修改之后,重启容器中的nginx:

$ docker exec dlnmp_nginx_1 nginx -s reload

其中,dlnmp_nginx_1是容器的名称,也可以指定容器的ID。

8 站点根目录写权限

有时候,我们的PHP代码需要某个目录的写权限。

比如,Wordpress的wp-content/uploads目录,只有写权限我们才能正常上传文件。

默认的,容器中的/var/www/html目录属于root,我们需要修改为www-data,PHP才能正常写目录。

先进入到容器中:

$ docker exec -it dlnmp_php-fpm_1 /bin/bash

然后修改目录权限:

$ chown -R www-data:www-data /var/www/html

这样PHP就能正常读写目录了。

9 数据库

9.1 使用MySQL

在docker-compose.yml文件中,我们指定了MySQL数据库root用户的密码为123456

所以,我们就可以在主机中通过:

$ mysql -h 127.0.0.1 -u root -p

输入密码,就可以进入MySQL命令行。

说明:这里MySQL的连接主机不能用localhost,因为MySQL客户端默认使用unix socket方式连接,应该直接用本地IP。

在PHP代码中的使用方式与在主机中使用稍有不同,如下:

$pdo = new PDO('mysql:host=mysql;dbname=site1', 'root', '123456');

其中,host的值就是在docker-compose.yml里面指定的MySQL容器的名称。

这是因为PHP代码是在FPM容器中,FPM容器启动时会自动在/etc/hosts中加上:

172.17.0.2 mysql 11e55f91c4c3 dlnmp_mysql_1

就是说,mysql自动指向了MySQL容器动态生成的IP。

9.2 使用Redis

Redis使用和MySQL类似。

在主机和容器内部都通过地址127.0.0.1,端口6379访问。

PHP则是跨容器访问,host参数用redislinks指定的名称),端口用6379

10 让DNMP随系统启动

到这里我们用docker搭建的Docker (Linux) + Nginx + MySQL + PHP环境已经可以正常使用了。

但每次启动系统都得切换到项目目录,再执行compose up,就略显繁琐。

要是能在系统启动的时候自动启动容器,岂不是更好,下面是具体的方法。

10.1 Ubuntu系统Unity桌面

从Launcher中搜索Startup Applications(启动应用程序),

然后Add一项,名字:Dnmp,命令填:

docker-compose -f /home/gary/dnmp/docker-compose.yml up -d

其中,-f 指定docker-compose.yml文件的位置。

然后保存即可。

10.2 其他Linux系统

其他命令行下的Linux系统,可以直接编辑 /etc/rc.local 文件,加上上面的命令。

 

Docker相关的有用文章:

  1. Docker 常用命令
  2. docker-compose中ports和expose的区别
  3. 手动用Docker一步步部署LNMP完整教程
  4. Docker容器使用十大注意事项

 

141条评论

nirnava says: 回复

如何才能使用phpmyadmin访问mysql数据库呢? 希望大神解答。。

歪麦 says: 回复

安装个Navicat吧,这个管理更方便。

Kerwin says: 回复

docker pull phpmyadmin/phpmyadmin

docker run --name myadmin -d --link dnmp_mysql_1:db -p 8080:80 phpmyadmin/phpmyadmin

phpmyadmin入口: http://domain.com:8080

Lii says: 回复

博主试过用docker搭建Apache和Nginx共存的开发环境吗。 因为我手头项目都是Apache,但是自己的项目想用Nginx。

歪麦 says: 回复

Apache应该也是差不多的,把Nginx容器的构建改成Apache,设置对应的conf配置就可以了

霍霍人 says: 回复

centos7不行呀

歪麦 says: 回复

有什么提示么?

123 says: 回复

W: Failed to fetch http://mirrors.163.com/debian-security/dists/jessie/updates/main/binary-amd64/Packages Hash Sum mismatch

E: Some index files failed to download. They have been ignored, or old ones used instead.

apt-get源还是有错误呢?

歪麦 says: 回复

那修改source.list.jesse,换一个源试试
不同网路的源不一定都是可用的

霍霍人 says: 回复

都试过了。。。。。

霍霍人 says: 回复

中科大的源可以

Nick_Cai says: 回复

请问是用16.04还是14.04的源呢?

ocean says: 回复

你好,我已经sudo -s了,但好像有问题

Starting dnmp_nginx_1 ... done
Attaching to dnmp_redis_1, dnmp_mysql_1, dnmp_php-fpm_1, dnmp_nginx_1
mysql_1 | chown: changing ownership of '/var/lib/mysql/.gitignore': Operation not permitted
mysql_1 | chown: changing ownership of '/var/lib/mysql/': Operation not permitted
nginx_1 | nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)
nginx_1 | 2017/11/21 15:50:19 [emerg] 1#1: open() "/var/log/nginx/error.log" failed (13: Permission denied)
redis_1 | 1:C 21 Nov 15:45:27.963 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 21 Nov 15:45:27.963 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 21 Nov 15:45:27.963 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1 | 1:M 21 Nov 15:45:27.966 * Running mode=standalone, port=6379.
redis_1 | 1:M 21 Nov 15:45:27.967 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1 | 1:M 21 Nov 15:45:27.967 # Server initialized
redis_1 | 1:M 21 Nov 15:45:27.968 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis_1 | 1:M 21 Nov 15:45:27.968 * Ready to accept connections
php-fpm_1 | [21-Nov-2017 15:45:28] NOTICE: fpm is running, pid 1
php-fpm_1 | [21-Nov-2017 15:45:28] NOTICE: ready to handle connections
dnmp_mysql_1 exited with code 1
dnmp_nginx_1 exited with code 1

歪麦 says: 回复

这里提示是权限问题,把当前用户添加到docker组再试

ocean says: 回复

嗯嗯,晚上回家再试试,感谢

Louie says: 回复

我也是这个问题,请问怎么解决啊

ocean says: 回复

你好,bash: gpasswd: command not found,方便加下QQ吗?462091385

歪麦 says: 回复

什么系统,怎么操作的?

Yfu says: 回复

你好,我docker-compose up的时候会有红字报:
debconf: delaying package configuration, since apt-utils is not installed,
然后最后报:
Recreating 1741073fe75c_1741073fe75c_1741073fe75c_1741073fe75c_dnmp_php-fpm_1 ... error

ERROR: for 1741073fe75c_1741073fe75c_1741073fe75c_1741073fe75c_dnmp_php-fpm_1 No such image: sha256:d4e6db5b65a483588679aa3d53464616fcdd0a67960c7ef86c0f96f407538e65

ERROR: for php-fpm No such image: sha256:d4e6db5b65a483588679aa3d53464616fcdd0a67960c7ef86c0f96f407538e65
ERROR: Encountered errors while bringing up the project.

但是apt-utils如何安装呢?

歪麦 says: 回复

apt-utils包含了Ubuntu 16.04 LTS 的发布后的一些新命令,比如apt(Advanced Package Tool),它可以取代 apt-get、apt-cache 等功能。
这里我们用apt-get,可以忽略这个提示,不会影响。

第二个问题,
提示Recreating 1741073fe75c_1741073fe75c_1741073fe75c_1741073fe75c_dnmp_php-fpm_1 ... error,说明之前有build失败过,
用docker images命令可以看到构建失败的镜像,再docker rmi删除掉,重新up

Henry says: 回复

你好,我docker-compose up的时候会有红字报:

error: /usr/src/php/ext/mcrypt does not exist

usage: /usr/local/bin/docker-php-ext-install [-jN] ext-name [ext-name ...]
ie: /usr/local/bin/docker-php-ext-install gd mysqli
/usr/local/bin/docker-php-ext-install pdo pdo_mysql
/usr/local/bin/docker-php-ext-install -j5 gd mbstring mysqli pdo pdo_mysql shmop

if custom ./configure arguments are necessary, see docker-php-ext-configure

Possible values for ext-name:
bcmath bz2 calendar ctype curl dba dom enchant exif fileinfo filter ftp gd gettext gmp hash iconv imap interbase intl json ldap mbstring mysqli oci8 odbc opcache pcntl pdo pdo_dblib pdo_firebird pdo_mysql pdo_oci pdo_odbc pdo_pgsql pdo_sqlite pgsql phar posix pspell readline recode reflection session shmop simplexml snmp soap sockets sodium spl standard sysvmsg sysvsem sysvshm tidy tokenizer wddx xml xmlreader xmlrpc xmlwriter xsl zend_test zip

Some of the above modules are already compiled into PHP; please check
the output of "php -i" to see which modules are already loaded.
ERROR: Service 'php-fpm' failed to build: The command '/bin/sh -c docker-php-ext-install mcrypt' returned a non-zero code: 1

歪麦 says: 回复

2017-11-30发布的PHP7.2.0已经移除了mcrypt了:http://php.net/manual/en/intro.mcrypt.php
如果Dockerfile里面是FROM php:fpm,默认就是PHP7.2.0,要把这两行删掉:

RUN apt-get install -y libmcrypt-dev
RUN docker-php-ext-install mcrypt

就可以了。
建议加密的地方用openssl或Sodium (PHP 7.2.0开始用)扩展代替。

dbquan says: 回复

```
[root@dbquan01 dnmp]# docker-compose up
Building php-fpm
Step 1 : FROM php:fpm
---> e63a6cd92abc
Step 2 : COPY ./files/sources.list.jessie /etc/apt/sources.list
...
ERROR: Service 'php-fpm' failed to build: The command '/bin/sh -c docker-php-ext-install mcrypt' returned a non-zero code: 1
```
请问安装报错如何解决? “error: /usr/src/php/ext/mcrypt does not exist”

歪麦 says: 回复

请看给楼上 @Henry 的回复。

歪麦 says: 回复

对了,PHP7.2.0的docker image是基于Debian stretch版本的,所以需要source.list用stretch源。
以后的版本再做个兼容。

ec says: 回复

我的保存,请问怎么解决,mac上面运行的,可以加下我qq 1046373779 吗,交流下,谢谢

---> Running in 9f91a58a09cf

Notice: Undefined variable: proxy_host in PEAR/Proxy.php on line 163

Notice: Undefined variable: proxy_port in PEAR/Proxy.php on line 163
No releases available for package "pecl.php.net/redis"
install failed
ERROR: Service 'php72' failed to build: The command '/bin/sh -c pecl install redis-3.1.4 && docker-php-ext-enable redis && : && pecl install xdebug-2.6.0alpha1 && docker-php-ext-enable xdebug && : && apt-get install -y libmagickwand-dev && pecl install imagick-3.4.3 && docker-php-ext-enable imagick && : && apt-get install -y libmemcached-dev zlib1g-dev && pecl install memcached-3.0.4 && docker-php-ext-enable memcached' returned a non-zero code: 1
yuxiMBP:dnmp zyx$

歪麦 says: 回复

因为pecl方式安装默认会用https方式,但是pecl.php.net的https通道已经被屏蔽了,所以必须走代理或者用源码方式安装,方法:https://github.com/yeszao/dnmp/issues/10

ec says: 回复

这个错误呢?怎么解决

etting up libhashkit-dev (1.0.18-4.1) ...
Setting up libmemcached-dev (1.0.18-4.1) ...
Processing triggers for libc-bin (2.24-11+deb9u1) ...

Warning: stream_socket_enable_crypto(): SSL: Success in PEAR/Proxy.php on line 101

Warning: stream_socket_enable_crypto(): SSL: Success in /usr/local/lib/php/PEAR/Proxy.php on line 101

Notice: Undefined variable: proxy_host in PEAR/Proxy.php on line 103

Notice: Undefined variable: proxy_port in PEAR/Proxy.php on line 103
No releases available for package "pecl.php.net/memcached"
install failed
ERROR: Service 'php72' failed to build: The command '/bin/sh -c pecl install redis-3.1.4 && docker-php-ext-enable redis && : && pecl install xdebug-2.6.0alpha1 && docker-php-ext-enable xdebug && : && apt-get install -y libmagickwand-dev && pecl install imagick-3.4.3 && docker-php-ext-enable imagick && : && apt-get install -y libmemcached-dev zlib1g-dev && pecl install memcached-3.0.4 && docker-php-ext-enable memcached' returned a non-zero code: 1
yuxiMBP:dnmp zyx$

dnmp says: 回复

Notice: Undefined variable: proxy_port in PEAR/Proxy.php on line 163
No releases available for package "pecl.php.net/redis"
install failed
我看到dockerfile里面写了下面的
## pecl files
## Note: if get error
## `No releases available for package "pecl.php.net/xxx"`
## or
## `Package "xxx" does not have REST xml available`
## Please turn on proxy (the proxy IP may be docker host IP or others).
但是proxy怎么打开呢?

歪麦 says: 回复

这个proxy就是要走代理哦,没有的话就用源码方式安装,方法:https://github.com/yeszao/dnmp/issues/10

Nick says: 回复

dockerfile里面设置了也没有用

Nick_Cai says: 回复

好像在php72的dockerifle 中把#RUN pear config-set http_proxy http://10.0.75.1:1080 这一行注释掉这几个扩展不需要设置代理

歪麦 says: 回复

嗯,可以注释掉,我已经修正了,删了这一行。

yusher says: 回复

我的是mac电脑,,,自己电脑装了个chales抓包工具,,这个工具可以设置代理地址,吧这个代理地址填到php/php72/Dockerfile 文件下的地方 如:RUN pear config-set http_proxy http://192.168.1.178:8888 就可以了

godaddy says: 回复

朋友 交换链接吗

dsf says: 回复

Step 3 : RUN apt-get update && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ && docker-php-ext-install gd && : && apt-get install -y libicu-dev && docker-php-ext-install intl && : && apt-get install -y libxml2-dev && apt-get install -y libxslt-dev && docker-php-ext-install soap && docker-php-ext-install xsl && docker-php-ext-install xmlrpc && docker-php-ext-install wddx && : && apt-get install -y libbz2-dev && docker-php-ext-install bz2 && : && docker-php-ext-install zip && docker-php-ext-install pcntl && docker-php-ext-install pdo_mysql && docker-php-ext-install mysqli && docker-php-ext-install mbstring && docker-php-ext-install exif && docker-php-ext-install bcmath && docker-php-ext-install calendar && docker-php-ext-install sockets && docker-php-ext-install gettext && docker-php-ext-install shmop && docker-php-ext-install sysvmsg && docker-php-ext-install sysvsem && docker-php-ext-install sysvshm && docker-php-ext-install opcache
---> [Warning] IPv4 forwarding is disabled. Networking will not work.
---> Running in b4dc7af9e896

zz的代码世界 says: 回复

nginx:
image: nginx:alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./www/:/var/www/html/:rw
- ./conf/nginx/conf.d:/etc/nginx/conf.d/:ro
- ./conf/nginx/nginx.conf:/etc/nginx/nginx.conf.d:ro
- ./log/nginx/:/var/log/nginx/:rw
links:
- php72:fpm

php72:
build: ./php/php72/
expose:
- "9000"
volumes:
- ./www/:/var/www/html/:rw
- ./conf/php/php.ini:/usr/local/etc/php/php.ini:ro
- ./conf/php/php-fpm.d/www.conf:/usr/local/etc/php-fpm.d/www.conf.d:rw
- ./log/php-fpm/:/var/log/php-fpm/:rw
links:
- mysql:mysql
- redis:redis

mysql:
image: mysql:latest
ports:
- "3306:3306"
volumes:
- ./conf/mysql/my.cnf:/etc/mysql/my.cnf.d:ro
- ./mysql/:/var/lib/mysql/:rw
- ./log/mysql/:/var/log/mysql/:rw
environment:
MYSQL_ROOT_PASSWORD: "123456"

redis:
image: redis:latest
ports:
- "6379:6379"

说明:原yml文件的.cnf执行的话会报错not a directory。然后我统一改成了.cnf.d就可以了,这样修改可以么???

歪麦 says: 回复

文件目录能够mount进去就OK

278945129978 says: 回复

问一下,如果不想把localhost指向site1改怎么修改

歪麦 says: 回复

修改:conf/nginx/conf.d/site1.conf文件,
把:
root /var/www/html/site1;
改成:
root /var/www/html/;

dserers says: 回复

如果不把localhost指向site1,而是指向var/www/html目录改怎么改

samsnow says: 回复

修改宿主机的php.ini没有影响到容器里的phpinfo

歪麦 says: 回复

可能两种情况:1、宿主机的php.ini没有mount到容器里面;2.被php-fpm的配置覆盖。

张鹏 says: 回复

RUN pear config-set http_proxy http://10.0.75.1:1080 这个在那里的呀,找不到这段,dockerfile没有呀

歪麦 says: 回复

有的哟,github完整版本有的

ruanzhq says: 回复

mysql_1 | mysqld: Table 'mysql.plugin' doesn't exist
mysql_1 | 2018-01-28T03:57:00.902395Z 0 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
mysql_1 | 2018-01-28T03:57:00.903005Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
mysql_1 | 2018-01-28T03:57:00.903265Z 0 [Note] InnoDB: Buffer pool(s) load completed at 180128 3:57:00
mysql_1 | 2018-01-28T03:57:00.904340Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
mysql_1 | 2018-01-28T03:57:00.904430Z 0 [Warning] Failed to set up SSL because of the following SSL library error: SSL context is not usable without certificate and private key
mysql_1 | 2018-01-28T03:57:00.904441Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
mysql_1 | 2018-01-28T03:57:00.904485Z 0 [Note] IPv6 is available.
mysql_1 | 2018-01-28T03:57:00.904494Z 0 [Note] - '::' resolves to '::';
mysql_1 | 2018-01-28T03:57:00.904517Z 0 [Note] Server socket created on IP: '::'.
mysql_1 | 2018-01-28T03:57:00.915660Z 0 [Warning] Failed to open optimizer cost constant tables
mysql_1 |
mysql_1 | 2018-01-28T03:57:00.915778Z 0 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist
mysql_1 | 2018-01-28T03:57:00.915846Z 0 [ERROR] Aborting
mysql_1 |
mysql_1 | 2018-01-28T03:57:00.915882Z 0 [Note] Binlog end

dnmp_mysql_1 exited with code 1

mysql 一直无法正常启动 请问是什么原因

歪麦 says: 回复

应该是mysql配置的原因,查一下my.cnf文件,具体原因这个log的error已经注明出来了。

ruanzhq says: 回复

找到原因了 我是之前用你的方法部署了一台机器,现在迁移的时候把mysql的数据直接拷贝过来了,我删掉备份数据就ok了

歪麦 says: 回复

OK

西点培训班 says: 回复

我去 要的就是这个资料,学习了

wufly says: 回复

安装好了之后composer怎么用呢

歪麦 says: 回复

进到容器里面用,也可以在主机上装composer,然后直接在主机更新组件

Aurora says: 回复

本地没有装PHP怎么装composer啊,可以这样吗

歪麦 says: 回复

...需要本地也安装PHP的,composer依赖PHP,官网有段话可以说明:

Composer requires PHP 5.3.2+ to run.

所以建议到容器里面使用composer

hlx says: 回复

Error: Service 'PHP72' failed to build: The command '/bin/sh -c cd /tmp/ && tar -xf zendopcache xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)
returned a non-zero code: 2

PHP72用了PHP54最后的7行就报上面的错了

歪麦 says: 回复

PHP72已经可以直接用opcache了,不需要像PHP54在容器中编译,直接把这些注释掉就可以了。

Shellic says: 回复

大神,win7下安装的docker toolbox,使用时是不是需要先创建ubuntu容器

歪麦 says: 回复

在win10里面用吧~

liaoyizhe says: 回复

作者你好,为什么装了redis之后php72容器里面还要再装一个redis呢?运行到后面直接报错了。
No releases available for package "pecl.php.net/redis"

歪麦 says: 回复

这里装的是PHP的redis扩展哦~

Nick_Cai says: 回复

大神您好,请问下面报错是因为啥? 我的环境win10, 安装过程中是有授权D盘的写的权限,但是我输入开机账号和密码后还是报错
ERROR: for dnmp_nginx_1 Cannot start service nginx: driver failed programming external connectivity on endpoint dnmp_nginx_1 (06c7228d3e1b10565c03b7edef8f620ba4b1abec8564fb81d1d7bc1391d64089): Error starting userland proxy: Bind for 0.0.0.0:80: unexpected error Permission denied

Nick_Cai says: 回复

哦,原来是我的80端口被占用,在docker-compose.yml第四行80:80 改为8080:80 就好了,谢谢大神的分享

歪麦 says: 回复

OK~~

wufly says: 回复

我在.env文件里设置了redis_host=redis,但是会报以下的错误,这是为什么呢?

ConnectionException in AbstractConnection.php line 155:
Connection refused [tcp://redis:tcp://172.17.0.2:6379]

歪麦 says: 回复

检查下PHP容器里面的hosts文件

alan says: 回复

环境变量REDIS_PORT不知道为什么被设置成tcp://172.17.0.2:6379,应该是6379的,在laravel中最终的redis链接变成了[tcp://redis:tcp://172.17.0.2:6379],所以报错,临时解决办法是在config/database.php中的redis配置中不要用env获取环境变量,直接写6379。

xhq says: 回复

我就想问问有没有系统要求,我用的是centos7,难道要ubuntu的吗?

Nick_Cai says: 回复

大神您好,为什么我修改权限不成功呢?然后一直报403,请帮忙看一下非常感谢!
PS D:\phpStudy\PHPTutorial\WWW\dnmp> docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6a6b8131dcab nginx:alpine "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:443->443/tcp, 0.0.0.0:8080->80/tcp dnmp_nginx_1
94f32fce5f43 dnmp_php72 "docker-php-entrypoi…" 2 hours ago Up 2 hours 9000/tcp dnmp_php72_1
9fdb3cbfa5c3 mysql:latest "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:3306->3306/tcp dnmp_mysql_1
447ec6dd634d redis:latest "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:6379->6379/tcp dnmp_redis_1
PS D:\phpStudy\PHPTutorial\WWW\dnmp> docker exec -it dnmp_php-fpm_1 /bin/bash
Error: No such container: dnmp_php-fpm_1
PS D:\phpStudy\PHPTutorial\WWW\dnmp> docker exec -it dnmp_php72_1 /bin/bash
root@94f32fce5f43:/var/www/html# chown -R www-data:www-data /var/www/html
root@94f32fce5f43:/var/www/html# ls -all
total 4
drwxr-xr-x 2 root root 0 Mar 15 00:37 .
drwxr-xr-x 3 root root 4096 Mar 22 22:04 ..
drwxr-xr-x 2 root root 0 Mar 26 02:45 site1
drwxr-xr-x 2 root root 0 Mar 15 00:37 site2
root@94f32fce5f43:/var/www/html# chown -R 777 /var/www/html
root@94f32fce5f43:/var/www/html# ls all
ls: cannot access 'all': No such file or directory
root@94f32fce5f43:/var/www/html# ls -all
total 4
drwxr-xr-x 2 root root 0 Mar 15 00:37 .
drwxr-xr-x 3 root root 4096 Mar 22 22:04 ..
drwxr-xr-x 2 root root 0 Mar 26 02:45 site1
drwxr-xr-x 2 root root 0 Mar 15 00:37 site2
root@94f32fce5f43:/var/www/html#

Nick_Cai says: 回复

我是win10 上面的docker ,win10里的权限都是可读可写可执行

Nick_Cai says: 回复

郁闷laravel安装的时候nginx 要指定到public文件夹下面

歪麦 says: 回复

那就修改nginx配置就可以了的

gaoyf says: 回复

您好,执行 docker-compose up 之后 出现如下红色字体,想请教下是什么问题呢?

config.m4:82: warning: AC_REQUIRE: `AC_HEADER_STDC' was expanded before it was required
config.m4:82: http://www.gnu.org/software/autoconf/manual/autoconf.html#Expanded-Before-Required
../../lib/autoconf/headers.m4:297: _AC_INCLUDES_DEFAULT_REQUIREMENTS is expanded from...
../../lib/autoconf/headers.m4:353: AC_INCLUDES_DEFAULT is expanded from...
../../lib/autoconf/headers.m4:129: _AC_CHECK_HEADER_MONGREL is expanded from...
../../lib/autoconf/headers.m4:67: AC_CHECK_HEADER is expanded from...
../../lib/m4sugar/m4sh.m4:607: AS_FOR is expanded from...
../../lib/autoconf/headers.m4:249: AC_CHECK_HEADERS is expanded from...
libxmlrpc/acinclude.m4:13: XMLRPC_HEADER_CHECKS is expanded from...
libxmlrpc/xmlrpc.m4:1: XMLRPC_CHECKS is expanded from...
config.m4:82: the top level
config.m4:82: warning: AC_REQUIRE: `AC_HEADER_STDC' was expanded before it was required
config.m4:82: http://www.gnu.org/software/autoconf/manual/autoconf.html#Expanded-Before-Required
../../lib/autoconf/headers.m4:297: _AC_INCLUDES_DEFAULT_REQUIREMENTS is expanded from...
../../lib/autoconf/headers.m4:353: AC_INCLUDES_DEFAULT is expanded from...
../../lib/autoconf/headers.m4:129: _AC_CHECK_HEADER_MONGREL is expanded from...
../../lib/autoconf/headers.m4:67: AC_CHECK_HEADER is expanded from...
../../lib/m4sugar/m4sh.m4:607: AS_FOR is expanded from...
../../lib/autoconf/headers.m4:249: AC_CHECK_HEADERS is expanded from...
libxmlrpc/acinclude.m4:13: XMLRPC_HEADER_CHECKS is expanded from...
libxmlrpc/xmlrpc.m4:1: XMLRPC_CHECKS is expanded from...
config.m4:82: the top level

歪麦 says: 回复

容器中编译扩展的提示,如果不是error,可以略过~如果要深究的话,需呀看看上下文的信息。

Jason says: 回复

博主,您好,非常感谢您的教程,我个人受益匪浅,不过我有个小疑问,为什么不将操作系统也docker 化呢?基于云基础,依赖一个ubuntu 系统会受限。

歪麦 says: 回复

毕竟嘛,已经有那么多系统了,windows、linux、macos,linux又有ubuntu、centos、fedora。。。,要是做成系统,那docker不就是再做一个ubuntu了吗?
docker的目的是尽量利用宿主机的资源,兼容这些系统,再任何平台提供一个统一的环境~

summer says: 回复

你好,你说的php版本随意切换,应该怎么切换?欢迎你给我回信!

歪麦 says: 回复

这样来:

$ docker-compose -f docker-compose54.yml up
$ docker-compose -f docker-compose56.yml up
zhangfei says: 回复

docker-compose -f docker-compose54.yml up
这样不行啊,还是直接运行已经安装的php7.2

是否 says: 回复

博主您好,我的报错也是Notice: Undefined variable: proxy_host in PEAR/Proxy.php on line 163

Notice: Undefined variable: proxy_port in PEAR/Proxy.php on line 163
No releases available for package "pecl.php.net/redis"
install failed
ERROR: Service 'php72' failed to build: The command '/bin/sh -c pecl install redis-3.1.4 && docker-php-ext-enable redis && : && pecl install xdebug-2.6.0alpha1 && docker-php-ext-enable xdebug && : && apt-get install -y libmagickwand-dev && pecl install imagick-3.4.3 && docker-php-ext-enable imagick && : && apt-get install -y libmemcached-dev zlib1g-dev && pecl install memcached-3.0.4 && docker-php-ext-enable memcached' returned a non-zero code: 1
看了您之前的回答,我也找不到RUN pear config-set http_proxy http://10.0.75.1:1080这个代码。。。而且也不知道怎么解决这个报错,能麻烦详细点说下如何处理吗,期待中。。。。

是否 says: 回复

看到博主已经把那个删除了,git再拉一遍就好了

张磊 says: 回复

博主 刚刚拉取你的代码。执行docker-compose up

到这一步就停了
mysql_1 | 2018-04-12T05:34:24.662022Z 0 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
mysql_1 | 2018-04-12T05:34:24.662624Z 0 [Warning] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
mysql_1 | 2018-04-12T05:34:24.662664Z 0 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
mysql_1 | 2018-04-12T05:34:24.663223Z 0 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
mysql_1 | 2018-04-12T05:34:24.692841Z 0 [Warning] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
mysql_1 | 2018-04-12T05:34:24.692865Z 0 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
mysql_1 | 2018-04-12T05:34:24.820424Z 0 [Note] Event Scheduler: Loaded 0 events
mysql_1 | 2018-04-12T05:34:24.821021Z 0 [Note] mysqld: ready for connections.
mysql_1 | Version: '5.7.21-log' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)
mysql_1 | 2018-04-12T06:01:19.000392Z 0 [Note] InnoDB: page_cleaner: 1000ms intended loop took 1343534ms. The settings might not be optimal. (flushed=0 and evicted=0, during the time.)

歪麦 says: 回复

并木有错呀,mysql已经可以用了的。

ihero says: 回复

#16: *9 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 172.17.0.1, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://172.17.0.5:9000", host: "localhost"

不知知道什么原因

歪麦 says: 回复

在PHP容器里面,localhost就是容器本身。

larlence says: 回复

我window 的虚拟机里面装docker 部署好项目使用curl会报这个错误怎么解决呢?

cURL error 6: Could not resolve host

歪麦 says: 回复

在docker-compose的PHP容器下加个:extra_hosts

pmls63 says: 回复

socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)
同样出现这个问题,将数据库host修改为mysql使用原生new pdo可以使用,但是像wordpress的框架无法使用

pmls63 says: 回复

请教下博主怎么处理这个问题呢

歪麦 says: 回复

宿主机改用“127.0.0.1:3306”连接(不要用localhost,原因正文有说明),PHP代码用“mysql:3306”连接

pmls63 says: 回复

好的谢谢

sowhy says: 回复

你好,Mac环境下出现这个:
ERROR: for mysql Cannot start service mysql: b'Mounts denied: \r\nThe paths /dnmp/log/mysql and /dnmp/conf/mysql/my.cnf and /dnmp/mysql\r\nare not shared from OS X and are not known to Docker.\r\nYou can configure shared paths from Docker -> Preferences... -> File Sharing.\r\nSee https://docs.docker.com/docker-for-mac/osxfs/#namespaces for more info.\r\n.'
ERROR: Encountered errors while bringing up the project.

请问怎么处理

歪麦 says: 回复

目录权限问题,修改下权限。

chao says: 回复

E:\php\doker\dnmp\dnmp>docker-compose up
Starting dnmp_mysql_1
Starting dnmp_redis_1
Starting dnmp_php72_1
Starting dnmp_nginx_1
Attaching to dnmp_mysql_1, dnmp_redis_1, dnmp_php72_1, dnmp_nginx_1
[36mmysql_1 |[0m
[36mmysql_1 |[0m ERROR: mysqld failed while attempting to check config
[36mmysql_1 |[0m command was: "mysqld --verbose --help"
[33mredis_1 |[0m 1:C 25 Apr 15:16:31.068 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
[36mmysql_1 |[0m
[33mredis_1 |[0m 1:C 25 Apr 15:16:31.068 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=1, just started
[36mmysql_1 |[0m mysqld: Error on realpath() on '/var/lib/mysql-files' (Error 2 - No such file or directory)
[32mphp72_1 |[0m [25-Apr-2018 15:16:34] NOTICE: fpm is running, pid 1
[33mredis_1 |[0m 1:C 25 Apr 15:16:31.068 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
[36mmysql_1 |[0m 2018-04-25T15:16:30.894888Z 0 [ERROR] [MY-010095] [Server] Failed to access directory for --secure-file-priv. Please make sure that directory exists and is accessible by MySQL Server. Supplied value : /var/lib/mysql-files
[32mphp72_1 |[0m [25-Apr-2018 15:16:34] NOTICE: ready to handle connections
[36mmysql_1 |[0m 2018-04-25T15:16:30.896234Z 0 [ERROR] [MY-010119] [Server] Aborting
[33mredis_1 |[0m 1:M 25 Apr 15:16:31.070 * Running mode=standalone, port=6379.
[33mredis_1 |[0m 1:M 25 Apr 15:16:31.070 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
[33mredis_1 |[0m 1:M 25 Apr 15:16:31.070 # Server initialized
[33mredis_1 |[0m 1:M 25 Apr 15:16:31.070 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
[33mredis_1 |[0m 1:M 25 Apr 15:16:31.070 * Ready to accept connections
[36mdnmp_mysql_1 exited with code 1
[0m

你好 大神,为什么mysql这里会报错,没有找到原因,请指点

歪麦 says: 回复

可以搜下你贴出来的日志的error,是不是有配置了个:/var/lib/mysql-files,确保这个目录存在并且可访问哦~

pmls63 says: 回复

请问下,我在centos中部署了一个dnmp,但是我想在本地mac主机上面想访问数据库,或者用navicate连接数据库,会报错
ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(/usr/local/Cellar/mysql/5.7.21/lib/plugin/caching_sha2_password.so, 2): image not found
麻烦作者帮忙看看

没有做任何改动,mysql始终不能启动,也没找到对应的错误问题 says: 回复

mysql_1 |
mysql_1 | ERROR: mysqld failed while attempting to check config
mysql_1 | command was: "mysqld --verbose --help"
mysql_1 |
mysql_1 | mysqld: [Warning] World-writable config file '/etc/mysql/my.cnf' is ignored.
mysql_1 | mysqld: Error on realpath() on '/var/lib/mysql-files' (Error 2 - No such file or directory)
mysql_1 | 2018-04-30T19:08:41.012004Z 0 [ERROR] [MY-010095] [Server] Failed to access directory for --secure-file-priv. Please make sure that directory exists and is accessible by MySQL Server. Supplied value : /var/lib/mysql-files
mysql_1 | 2018-04-30T19:08:41.013312Z 0 [ERROR] [MY-010119] [Server] Aborting
php72_1 | [30-Apr-2018 19:08:41] NOTICE: fpm is running, pid 1
php72_1 | [30-Apr-2018 19:08:41] NOTICE: ready to handle connections
php70_mysql_1 exited with code 1

123 says: 回复

我把mysql换成5.6版本了

Cwift says: 回复

应该是mysql的镜像版本更新了,我是这么做的:创建 /var/lib/mysql-files/ 目录,修改docker-compose.yml中的mysql的配置文件,增加下面这行:
- ./mysql-files/:/var/lib/mysql-files/:rw
然后重启mysql:
docker-compose up -d --no-deps --build mysql

歪麦 says: 回复

确实是,mysql有更新了最新版本,也可以调整my.cnf配置:
secure_file_priv=""

Cwift says: 回复

貌似还是不行,又报别的错误了...

marion says: 回复

ERROR: for php72 Cannot start service php72: Cannot link to a non running container: /dnmp_mysql_1 AS /dnmp_php72_1/dnmp_mysql_1
ERROR: Encountered errors while bringing up the project.

docker-compose mysql报错 says: 回复

ERROR: for dnmp_mysql_1 Cannot start service mysql: b'OCI runtime create failed: container_linux.go:348: starting container process caused "process_linux.go:402: container init caused \\"rootfs_linux.go:58: mounting \\\\\\"/Users/rainsen/dnmp/conf/mysql/my.cnf\\\\\\" to rootfs \\\\\\"/var/lib/docker/overlay2/a5217b43bbf51778f959777f8577292e66709dce4a60fb99e83d9c0dc80cfd5b/merged\\\\\\" at \\\\\\"/var/lib/docker/overlay2/a5217b43bbf51778f959777f8577292e66709dce4a60fb99e83d9c0dc80cfd5b/merged/etc/mysql/my.cnf\\\\\\" caused \\\\\\"not a directory\\\\\\"\\"": unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type'

壹枝雪糕 says: 回复

Starting dnmp_mysql_1 ...
Starting dnmp_mysql_1 ... done
Starting dnmp_php72_1 ... error

ERROR: for dnmp_php72_1 Cannot start service php72: b'Cannot link to a non running container: /dnmp_mysql_1 AS /dnmp_php72_1/mysql'

ERROR: for php72 Cannot start service php72: b'Cannot link to a non running container: /dnmp_mysql_1 AS /dnmp_php72_1/mysql'
ERROR: Encountered errors while bringing up the project.
请问这是什么错误呢?

歪麦 says: 回复

重新去github拉一份吧,mysql有更新了最新版本,调整my.cnf配置就可以了:
secure_file_priv=""

小欧 says: 回复

你好 我本机是win10 安装了virtual box,virtual box 安装了centos7 ,然后centos7里安装docker docker-composer ;
我把代码clone在win10中文件夹,然后文件夹通过virtual box 共享到centos7,在运行up后,访问返回 403 Forbidden。
但是如果我把代码clone到centos7中,然后再运行up后,就可以正常访问,请问怎么解决这个权限问题呢?

歪麦 says: 回复

在Win10里面,打开docker界面,把docker-compose.yml所在的盘符共享给docker。

小欧 says: 回复

我的docker是安装在virtual box里的centos7虚拟机的,win10中代码所在的code文件已经通过virtual box共享给了centos7了。

bill says: 回复

你好,我第一次执行:docker-compose up
退出后,停止容器,
第二次执行:docker-compose -f docker-compose56.yml up

发现就不构建了,直接用php7的那个了

请问一下这是为什么呢

歪麦 says: 回复

已经构建的话,就会直接使用了的。
在后面加个--build就会根据yml重新构建,下次直接up就可以了。

bill says: 回复

就是我第一次用docker-compose.yml构建php7版本,也想构建个php5.6的版本,就是执行这样的命令就可以了?
docker-compose -f docker-compose56.yml --build up

歪麦 says: 回复

可以的。

天蓝 says: 回复

请问容器启动后,怎么让8.0.11 MySQL 配置远程登录呢? 以前常惯的 grant all privileges on *.* to root@"%" identified by "123456" with grant option; 发现不好使了,会报错

歪麦 says: 回复

这个命令执行之后,有FLUSH PRIVILEGES;么?
或者my.conf是否已经配置了bind-address = 127.0.0.1

bill says: 回复

请问,怎么让8.0.11 MySQL 配置远程登录呢?我docker加载了phpmyadmin后,一直都不能登录上,提示如下:

mysqli_real_connect(): php_network_getaddresses: getaddrinfo failed: Name does not resolve

mysqli_real_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name does not resolve

天蓝 says: 回复

有FLUSH PRIVILEGES; 不过还是直接语法错误,应该是8.0版本跟5.版本有区别吧。 后面我是根据网上的来执行,稀里糊涂解决了:

user mysql;
delete from user where Host = '%' and User = 'root';
update user set Host='%' where User='root';
select Host, User from user;

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';

ALTER USER 'root'@'%' IDENTIFIED BY '新密码';
flush privileges;

bill says: 回复

你的php版本是5.6?

依然灬灰 says: 回复

Attaching to dnmp_redis_1, dnmp_php72_1, dnmp_nginx_1
redis_1 | 1:C 17 May 12:32:34.474 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 17 May 12:32:34.474 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 17 May 12:32:34.474 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1 | 1:M 17 May 12:32:34.476 * Running mode=standalone, port=6379.
redis_1 | 1:M 17 May 12:32:34.476 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1 | 1:M 17 May 12:32:34.476 # Server initialized
redis_1 | 1:M 17 May 12:32:34.476 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis_1 | 1:M 17 May 12:32:34.477 * Ready to accept connections
php72_1 | [17-May-2018 12:32:48] NOTICE: fpm is running, pid 1
php72_1 | [17-May-2018 12:32:48] NOTICE: ready to handle connections
这个问题需要如何解决

shell says: 回复

你好,我用你的这个成功搭建好了环境
但是发现当挂载的是文件的时候,比如my.cnf 在宿主机修改后,docker里面并没有变化
我现在只能是删除重新生成容器,大神有别的方法吗?

歪麦 says: 回复

改了my.cnf之后,要重启容器:docker-compose restart mysql

shell says: 回复

谢谢博主^_^

用“mysql”链接数据库 says: 回复

博主: 在centos7中使用最新的dnmp搭建环境,数据库使用“mysql”链接不上,什么原因呢?还是有别的操作方法?求解!

歪麦 says: 回复

有具体的日志吗?进入php容器,ping mysql的结果是什么呢?

木木 says: 回复

php容器与数据库mysql无法通讯,提示报错:
SQLSTATE[HY000] [2002] Connection refused

歪麦 says: 回复

首先,用docker ps看看mysql容器是否启动,以及端口映射是否正确。
然后检查是否是连接ip、端口、密码的问题。

青波 says: 回复

地址一栏为mysql,不是127.0.0.1

史莱姆 says: 回复

楼主你好,启动的时候报错:dnmp-php | [31-May-2018 09:48:53] ERROR: failed to open configuration file '/usr/local/etc/php-fpm.d/www.conf': Permission denied (13) 有什么解决办法吗

歪麦 says: 回复

权限原因,修改下权限:chmod a+r conf/php-fpm/www.conf

marion says: 回复

请问如何你能重启php-fpm

歪麦 says: 回复

一般我重启容器连带一起重启PHP-FPM

phper says: 回复

怎么安装swoole 扩展,通过peal 安装老报错,希望博主帮忙解决一下

歪麦 says: 回复

那通过源码来安装把,最稳妥了,具体可参考文件:dnmp/php/php54/Dockerfile

这个文件最后有通过源码安装zendopcache的方式,swoole惨照这个步骤就可以了。

phper says: 回复

5.6 build 提示问题
The following packages have unmet dependencies:
libfreetype6-dev : Depends: zlib1g-dev but it is not going to be installed or
libz-dev
libpng12-dev : Depends: zlib1g-dev but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
ERROR: Service 'php5.6' failed to build: The command '/bin/sh -c apt-get update && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ && docker-php-ext-install gd && : && apt-get install -y libicu-dev && docker-php-ext-install intl && : && apt-get install -y libxml2-dev && apt-get install -y libxslt-dev && docker-php-ext-install soap && docker-php-ext-install xsl && docker-php-ext-install xmlrpc && docker-php-ext-install wddx && : && apt-get install -y libbz2-dev && docker-php-ext-install bz2 && : && docker-php-ext-install zip && docker-php-ext-install pcntl && docker-php-ext-install pdo_mysql && docker-php-ext-install mysqli && docker-php-ext-install mbstring && docker-php-ext-install exif && docker-php-ext-install bcmath && docker-php-ext-install calendar && docker-php-ext-install sockets && docker-php-ext-install gettext && docker-php-ext-install shmop && docker-php-ext-install sysvmsg && docker-php-ext-install sysvsem && docker-php-ext-install sysvshm && docker-php-ext-install opcache && : && apt-get install -y libmcrypt-dev && docker-php-ext-install mcrypt && : && apt-get install -y curl && apt-get install -y libcurl3 && apt-get install -y libcurl4-openssl-dev && docker-php-ext-install curl && : && apt-get install -y inetutils-ping && apt-get install -y net-tools && : && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer && :RUN pecl install redis-3.1.4 && docker-php-ext-enable redis && : && pecl install xdebug-2.5.5 && docker-php-ext-enable xdebug && : && apt-get install -y libmagickwand-dev && pecl install imagick-3.4.3 && docker-php-ext-enable imagick && : && apt-get install -y libmemcached-dev zlib1g-dev && pecl install memcached-2.2.0 && docker-php-ext-enable memcached' returned a non-zero code: 100

歪麦 says: 回复

apt-get install update后面加一条命令:

&& apt-get install -y zlib1g-dev
小帅 says: 回复

dnmp-mysql | mbind: Operation not permitted
dnmp-mysql | mysqld: File '/var/log/dnmp/mysql.slow.log' not found (OS errno 13 - Permission denied)
dnmp-mysql | 2018-06-09T17:53:23.111890Z 0 [ERROR] [MY-011263] [Server] Could not use /var/log/dnmp/mysql.slow.log for logging (error 13 - Permission denied). Turning logging off for the server process. To turn it on again: fix the cause, then either restart the query logging by using "SET GLOBAL SLOW_QUERY_LOG=ON" or restart the MySQL server.

1,mbind错误是docker跑mysql的正常现象对吧?
2,mysql.slow.log一直无法加载,已经修改宿主config关闭selinux了,并且chmod 755 log设定好了目录权限

歪麦 says: 回复

1. mbind正常现象
2. 在my.cnf里面把手动指定的mysql.slow.log那一行注释,让MySQL自动生成。

小帅 says: 回复

宿主# mysql -h127.0.0.1 -uroot -p
Enter password:
ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib64/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory
似乎是mysql8默认采用caching_sha2_password导致的

但更奇怪的事情是3天前在另一台服务器上建立的dnmp就可以连接,一样的宿主环境,都是centos7.5,一样版本的docker和docker-composer,检查过mysql镜像也都是最新的8.0,有点懵逼了。。。

ak says: 回复

docker-compose up
ERROR: Couldn't connect to Docker daemon at http+docker://localhost - is it running?

If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable.

"DOCKER_HOST "是需要把它弄成环境变量吗?使用的是centos7.5

歪麦 says: 回复

1. 确定当前用户已经加入docker用户组
2. 停止所有容器,重启docker进程,再启动容器。

Vincent says: 回复

你好在 docker-compose up 这一步报错
ERROR: for dnmp-nginx Cannot start service nginx: driver failed programming external connectivity on endpoint dnmp-nginx (ca6d71aad83570cd058dfcf89502875516349a432f7353341bfdf8b8b900b671): Error starting userland proxy: Bind for 0.0.0.0:80: unexpected error (Failure EADDRINUSE)

ERROR: for nginx Cannot start service nginx: driver failed programming external connectivity on endpoint dnmp-nginx (ca6d71aad83570cd058dfcf89502875516349a432f7353341bfdf8b8b900b671): Error starting userland proxy: Bind for 0.0.0.0:80: unexpected error (Failure EADDRINUSE)

Turandot says: 回复

你好!新手请教几个问题
1、安装完毕后提示如下:
$ docker-compose up
Starting dnmp-redis ... done
Starting dnmp-nginx ... done
Starting dnmp-mysql ... done
Starting dnmp-php ... done
Attaching to dnmp-redis, dnmp-php, dnmp-nginx, dnmp-mysql
dnmp-redis | 1:C 16 Jun 03:03:00.657 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
dnmp-redis | 1:C 16 Jun 03:03:00.657 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=1, just started
dnmp-redis | 1:C 16 Jun 03:03:00.657 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
dnmp-redis | 1:M 16 Jun 03:03:00.658 * Running mode=standalone, port=6379.
dnmp-redis | 1:M 16 Jun 03:03:00.659 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
dnmp-redis | 1:M 16 Jun 03:03:00.659 # Server initialized
dnmp-redis | 1:M 16 Jun 03:03:00.659 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
dnmp-redis | 1:M 16 Jun 03:03:00.659 * DB loaded from disk: 0.000 seconds
dnmp-redis | 1:M 16 Jun 03:03:00.659 * Ready to accept connections
dnmp-nginx | 2018/06/16 03:03:01 [warn] 1#1: the "ssl" directive is deprecated, use the "listen ... ssl" directive instead in /etc/nginx/conf.d/site2.conf:21
dnmp-nginx | nginx: [warn] the "ssl" directive is deprecated, use the "listen ... ssl" directive instead in /etc/nginx/conf.d/site2.conf:21
dnmp-php | [16-Jun-2018 03:03:01] NOTICE: fpm is running, pid 1
dnmp-php | [16-Jun-2018 03:03:01] NOTICE: ready to handle connections
dnmp-mysql | 2018-06-16T03:03:02.241382Z 0 [Warning] [MY-010097] [Server] Insecure configuration for --secure-file-priv: Current value does not restrict location of generated files. Consider setting it to a valid, non-empty path.
dnmp-mysql | 2018-06-16T03:03:02.241501Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.11) starting as process 1
dnmp-mysql | mbind: Operation not permitted
dnmp-mysql | mbind: Operation not permitted
dnmp-mysql | 2018-06-16T03:03:03.272785Z 0 [System] [MY-010229] [Server] Starting crash recovery...
dnmp-mysql | 2018-06-16T03:03:03.273361Z 0 [System] [MY-010232] [Server] Crash recovery finished.
dnmp-mysql | 2018-06-16T03:03:03.380862Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
dnmp-mysql | 2018-06-16T03:03:03.435771Z 0 [Warning] [MY-010315] [Server] 'user' entry 'mysql.infoschema@localhost' ignored in --skip-name-resolve mode.
dnmp-mysql | 2018-06-16T03:03:03.435916Z 0 [Warning] [MY-010315] [Server] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
dnmp-mysql | 2018-06-16T03:03:03.436144Z 0 [Warning] [MY-010315] [Server] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
dnmp-mysql | 2018-06-16T03:03:03.436830Z 0 [Warning] [MY-010315] [Server] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
dnmp-mysql | 2018-06-16T03:03:03.437289Z 0 [Warning] [MY-010323] [Server] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
dnmp-mysql | 2018-06-16T03:03:03.437396Z 0 [Warning] [MY-010323] [Server] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
dnmp-mysql | 2018-06-16T03:03:03.438227Z 0 [Warning] [MY-010311] [Server] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
dnmp-mysql | 2018-06-16T03:03:03.481453Z 0 [Warning] [MY-010330] [Server] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
dnmp-mysql | 2018-06-16T03:03:03.482301Z 0 [Warning] [MY-010330] [Server] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
dnmp-mysql | 2018-06-16T03:03:03.502486Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.11' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
这算成功部署了吗?这么多警告正常吗?还有怎么再进入命令行模式呢?这段信息之后似乎没有结束,除了ctrl c停止之外没法进行其他操作。。

2、在Mac上,用dnmp的模式和mamp有什么区别呢?开发网站的话,文件操作是在shell中进行还是直接在挂载的host上操作?和生产环境怎么同步呢?我以前都是xampp或者mamp然后ftp上传的。。。

新手,见笑了

发表评论

电子邮件地址不会被公开。 必填项已用*标注

昵称 *