In general, we will install Nginx, PHP are installed in the same container, in order to expand convenience, we hope that nginx container and PHP container separate. Then you can use the Docker link command to do this.
Required Image:
- Nginx 1.12.2
- PHP (including php-fpm7.1) 7.1
Here the Nginx image selection daocloud.io/library/nginx:1.12.2-alpine
, PHP image compiled by itself (the selection is Https://github.com/52fhy/docker-images php56-fpm-centos68-withext
and php71-fpm-centos68-phalcon-withext
).
Practice
Run PHP-FPM:
docker run -d --restart=always --name yphp -v /work/:/work/ "/work/yphp/php/etc/":/usr/local/php/etc/ php71-fpm-centos68-phalcon-withext
--restart=always
Enables the container to boot with the Docker service itself. -v
Specifies the directory mapping relationship for the host and container.
This is /work/yphp
from https://github.com/52fhy/yphp.
/work
Subdirectories included in the contents:
/work |-- www |---yphp
/www
is the project directory, yphp
is the Nginx and PHP configuration directory, mapped to the container, convenient for local modification.
Run Nginx:
docker run -d --restart=always --name some-nginx -p 80:80 --link yphp:yphp -v /work/:/work/ -v /work/yphp/nginx/conf/:/etc/nginx/ -v /work/yphp/nginx/logs/:/etc/nginx/logs/ daocloud.io/library/nginx:1.12.2-alpine
It nginx:1.12.2-alpine
is chosen because the image is very small.
Commands are not many, but there are a lot of things to watch out for, which may cause the service to be unhealthy:
1, for PHP-FPM container, php-fpm www.conf configuration file, the IP of the monitoring can not be 127.0.0.1, must be 0.0.0.0; This will cause the subsequent nginx to appear 404 (see the problem).
;listen = 127.0.0.1:9000listen = 0.0.0.0:9000
2, the Nginx container must be --link
connected to the PHP-FPM container.
3. The WWW directory of the Nginx container and the PHP-FPM container must be consistent.
4, Nginx container configuration Fastcgi_pass need to modify the --link
name (the actual host name, will be in the Nginx container /etc/hosts
added a host record).
5, if the Nginx container needs to support more port access, then use -p
the scope representation, example: -p 80-90:80-90
. Or more than one -p
parameter.
6, the -v
parameter specifies the local mount directory (in the example /work
) subdirectory (for example /www
) in the host cannot be a soft link, otherwise it will cause nginx 404, because the soft link in the container is invalid. But /work
it can be a soft link itself.
Nginx Configuration:
Hello.cc.conf
server { listen 80; server_name hello.cc; access_log logs/access.log; error_log logs/error.log; root /work/yphp/php/; index index.php index.html index.htm; location ~ \.php$ { #fastcgi_pass 127.0.0.1:9000; fastcgi_pass yphp:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }}
Notice that the fastcgi_pass
changes have been made here.
Run multiple versions of PHP
docker run -d --restart=always --name yphp56 -p 9000:9000 -v /work/:/work/ "/work/yphp/php/etc56/":/usr/local/php/etc/ php56-fpm-centos68-withext docker run -d --restart=always --name yphp -p 9001:9000 -v /work/:/work/ "/work/yphp/php/etc/":/usr/local/php/etc/ php71-fpm-centos68-phalcon-withextdocker run -d --restart=always --name some-nginx -p 80-90:80-90 --link yphp56 --link yphp -v /work/:/work/ -v /work/yphp/nginx/conf/:/etc/nginx/ -v /work/yphp/nginx/logs/:/etc/nginx/logs/ daocloud.io/library/nginx:1.12.2-alpine
For --link
the Nginx container used, the parameters of the PHP-FPM container -p
do not actually produce a function, can omit to write.
This is the /work
same directory for the host.
docker ps
whether the view state is used is UP
:
$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES221c6ffb19dd daocloud.io/library/nginx:1.12.2-alpine "nginx -g 'daemon of…" 12 minutes ago Up 2 seconds 0.0.0.0:80-90->80-90/tcp some-nginx02a4f9d804f7 php71-fpm-centos68-phalcon-withext "/run.sh" 24 minutes ago Up 3 seconds 80/tcp, 0.0.0.0:9001->9000/tcp yphpa9decc06558e php56-fpm-centos68-withext "/run.sh" 25 minutes ago Up 3 seconds 80/tcp, 0.0.0.0:9000->9000/tcp yphp56
The description is normal.
If your Mac or Linux is installed locally and you have Docker (not toolbox), 127.0.0.1
you can see that Nginx is already running. 127.0.0.1 hello.cc
/etc/hosts
After you add to (non-Windows) Access http://hello.cc/you can see the phpinfo information. Try to modify /work/yphp/nginx/conf/vhost/hello.cc.conf
the port in the fastcgi_pass
9001, and then restart the Nginx container or restart the Nginx container, you will find that the PHP version shown in Phpinfo becomes PHP7.1.
# 重启nginx容器docker restart some-nginx# 重启nginx容器里的nginx服务docker exec some-nginx nginx -s reload
Reference
1, Docker container connection problem: Nginx and php-fpm--link after the normal parsing of PHP program, solve
https://segmentfault.com/q/1010000006148224/a-1020000013359135
2. Build a simple static website on Docker using Alpine Linux-csdn Blog
http://blog.csdn.net/becivells/article/details/51599186
3, Docker Multi-container connection-take nginx+php as an example-Program ape Growth Program-Segmentfault think No
1190000002949036
4, Nginx call remote PHP-FPM-finger & fleeting-Blog Park
Https://www.cnblogs.com/feiyafeiblog/p/6938515.html
Quickly build a PHP development environment with Docker link