上一篇的手工操作多容器运行nginx+php,很麻烦,该怎么办?Docker Compose 跟上
先删掉上篇创建的容器和网络,如若不然,完成本章会产生冲突
docker stop nginx
docker stop fpm
docker network rm lnmp
先将上节手工运行的nginx容器命令拿过来作为编写docker-compose.yml文件的参考:
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
继续编辑前面章节创建的 mycompose/docker-compose.yml文件
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启动项目
docker-compose up -d
查看启动状态
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
*/
我们可以看到启动了三个容器
先访问apache映射的8080端口
http://服务器ip:8080/index.php
再访问nginx映射的8081端口
http://服务器ip:8081/index.php
ok!
完工