[root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 58280fe851f9 nginx "/usr/local/nginx/..." 15 seconds ago Up 14 seconds 0.0.0.0:81->80/tcp nginx 9ea150c35587 php "/usr/local/php/sb..." 36 seconds ago Up 35 seconds 9000/tcp php
[root@docker ~]# docker network ls NETWORK ID NAME DRIVER SCOPE 5133ec415c3c bridge bridge local f359ca4e2d39 host host local 8d68673c045c none null local
[root@docker ~]# docker network ls NETWORK ID NAME DRIVER SCOPE 5133ec415c3c bridge bridge local f359ca4e2d39 host host local 67e29f0e4a77 my_net bridge local 8d68673c045c none null local
[root@docker lnmp]# docker-compose -f lnmp.yml up Creating network "lnmp_net1" with driver "bridge" Creating lnmp-php ... Creating lnmp-php ... done Creating lnmp-nginx ... Creating lnmp-nginx ... done Attaching to lnmp-php, lnmp-nginx
可以看出在启动服务的时候创建了服务级别的网络lnmp_net1
1 2 3 4 5 6 7
[root@docker lnmp]# docker network ls NETWORK ID NAME DRIVER SCOPE 5133ec415c3c bridge bridge local f359ca4e2d39 host host local 29d798852b52 lnmp_net1 bridge local 67e29f0e4a77 my_net bridge local 8d68673c045c none null local
服务nginx和php在网络lnmp_net1实现了互联通信
1 2 3
[root@docker lnmp]# docker-compose -f lnmp.yml exec nginx ping php PING php (172.19.0.2) 56(84) bytes of data. 64 bytes from lnmp-php.lnmp_net1 (172.19.0.2): icmp_seq=1 ttl=64 time=0.060 ms
[root@docker lnmp]# docker-compose -f php1.yml up Creating network "lnmp_net1" with driver "bridge" Creating lnmp-php1 ... Creating lnmp-php1 ... done Attaching to lnmp-php1
1 2 3 4 5 6 7
[root@docker lnmp]# docker-compose -f lnmp.yml up --build WARNING: Found orphan containers (lnmp-php1) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up. Creating lnmp-php ... Creating lnmp-php ... done Creating lnmp-nginx ... Creating lnmp-nginx ... done Attaching to lnmp-php, lnmp-nginx
查看启动的容器状态:
1 2 3 4 5
[root@docker www]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a089cd261279 nginx "/usr/local/nginx/..." 25 seconds ago Up 25 seconds 0.0.0.0:80->80/tcp lnmp-nginx 4ff4bf7c1bf9 php "/usr/local/php/sb..." 25 seconds ago Up 25 seconds 9000/tcp lnmp-php 4e39cdf62bf1 php "/usr/local/php/sb..." 51 seconds ago Up 51 seconds 9000/tcp lnmp-php1
lnmp-php exited with code 0 [root@docker www]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a089cd261279 nginx "/usr/local/nginx/..." 2 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp lnmp-nginx 4ff4bf7c1bf9 php "/usr/local/php/sb..." 2 minutes ago Exited (0) 52 seconds ago lnmp-php