What should I do when the previous manual operation of running nginx+php in multiple containers was cumbersome?Docker Compose follows
Delete the containers and networks you created in the previous section first. If you do not, there will be conflicts when you complete this chapter
docker stop nginx docker stop fpm docker network rm lnmp
Take the nginx container command run manually in the previous section as a reference for writing a docker-compose.yml file:
docker run -d --network lnmp --ip 192.169.0.3 --link fpm:php --name nginx --rm -p 80:80 -v ~/www:/usr/share/nginx/html -v ~/nginx.conf:/etc/nginx/nginx.conf nginx:1.15.0-alpine
Continue editing the mycompose/docker-compose.yml file created in the previous section
version: "3" services: fpm: container_name: fpm image: "php:7.1-fpm-alpine3.8" volumes: - ~/www:/php networks: lamp: ipv4_address: 192.158.0.2 httpd: container_name: httpd image: "httpd:2.4-alpine" ports: - 8080:80 links: - fpm:php volumes: - ~/www:/usr/local/apache2/htdocs - ~/httpd.conf:/usr/local/apache2/conf/httpd.conf networks: lamp: ipv4_address: 192.158.0.3 nginx: container_name: nginx image: "nginx:1.15.0-alpine" ports: - 8081:80 links: - fpm:php volumes: - ~/www:/usr/share/nginx/html - ~/nginx.conf:/etc/nginx/nginx.conf networks: lamp: ipv4_address: 192.158.0.4 networks: lamp: driver: bridge ipam: config: - subnet: 192.158.0.0/16
docker-compose startup project
docker-compose up -d
View Startup Status
docker-compose ps /** Name Command State Ports -------------------------------------------------------------------- fpm docker-php-entrypoint php-fpm Up 9000/tcp httpd httpd-foreground Up 0.0.0.0:8080->80/tcp nginx nginx -g daemon off; Up 0.0.0.0:8081->80/tcp */
We can see that three containers are started
Access apache mapped port 8080 first
http://Server ip:8080/index.php
Re-access port 8081 mapped by nginx
http://Server ip:8081/index.php
ok!
To be finished
Original Link: http://www.mi360.cn/articles/31