前面介紹了nginx與php兩個容器間是如何進行通訊的:php
[root@docker ~]# docker run -d --name=php -v /www:/usr/local/nginx/html php
[root@docker ~]# docker run -d --name=nginx --link=php:php -v /www:/usr/local/nginx/html -p 81:80 nginx
[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
經過容器間的選項--link指定容器名稱進行不一樣容器間的通訊(--link container_name或者將container_name取一個別名)html
如今使用另一種方式替代--link來達到容器間的通訊:docker networknginx
查看local的網絡信息:docker
[root@docker ~]# docker network ls NETWORK ID NAME DRIVER SCOPE 5133ec415c3c bridge bridge local f359ca4e2d39 host host local 8d68673c045c none null local
如今建立一個網絡名爲my_net且driver爲bridge的網絡:(默認建立的就是bridge)網絡
[root@docker ~]# docker network create my_net 67e29f0e4a77c79144efc337a081a889188b5b8e289968f22be6e4ddd9b80610 [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
利用--network啓動容器提供服務:tcp
[root@docker ~]# docker run -d --name=php --network my_net --network-alias php -v /www:/usr/local/nginx/html php 6b493cbe8207dee4cb4d5945cfce305dba96914083bd7f46841b0b42376bcb99 [root@docker ~]# docker run -d --name=nginx --network my_net --network-alias nginx -v /www:/usr/local/nginx/html -p 80:80 nginx 5ab220196b52bb768bef508433f0b920eecee70c3ee47880ebc5e2a74b5ee254
經過選項--network-alias將取名的my_net起了一個別名函數
[root@docker ~]# docker exec -it nginx ping php PING php (172.18.0.2) 56(84) bytes of data. 64 bytes from php.my_net (172.18.0.2): icmp_seq=1 ttl=64 time=0.079 ms 64 bytes from php.my_net (172.18.0.2): icmp_seq=2 ttl=64 time=0.090 ms
這裏爲啥可以ping經過php這個容器呢,由於這兩個容器在同一個網絡m'y_net內,而nginx裏面ping的php是容器名(這裏管理的是容器級別)ui
因爲可以ping通php,因此在nginx中配置的:this
server { listen 80; root /usr/local/nginx/html; index index.htm index.html index.php; location ~ \.php$ { root /usr/local/nginx/html; fastcgi_pass php:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
因此配置文件中的php不會致使nginx啓動失敗,經過network的driver bridge實現了容器間的訪問spa
上面在容器啓動的時候使用的是選項--network,而在compose的配置文件中則是networks,如今經過配置文件來進行闡述該參數的做用:
[root@docker lnmp]# cat lnmp.yml version: '3' services: nginx: image: nginx container_name: lnmp-nginx depends_on: - php ports: - "80:80" networks: - "net1" volumes: - "/www:/usr/local/nginx/html" php: image: php container_name: lnmp-php expose: - "9000" networks: - "net1" volumes: - "/www:/usr/local/nginx/html" networks: net1: driver: bridge
由上述文件中能夠知道networks定義了一個名稱爲net1的網絡,因爲networks是top-level(頂層級別,因此須要在頂層設置),而在建立的網絡時候須要指定driver(單一網絡使用bridge,swarm集羣使用overlay),並且driver內容不能省略,在nginx和php兩個service中使用了同一網絡net1,如今將服務啓動:
[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
[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實現了互聯通訊
[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
在nginx服務中可以訪問php的服務,在compose編排中級別的訪問是對service級別的訪問,因此在nginx服務配置文件中對應的php可以實現,不會致使nginx啓動失敗
及在compose的配置文件設置並對應的是service級別,當使用參數links也是配置的php服務,使得nginx服務可以ping通php服務並實現訪問通訊
[root@docker lnmp]# cat lnmp.yml version: '3' services: nginx: image: nginx container_name: lnmp-nginx depends_on: - php ports: - "80:80" links: - php:php volumes: - "/www:/usr/local/nginx/html" php: image: php container_name: lnmp-php expose: - "9000" volumes: - "/www:/usr/local/nginx/html"
將networks參數改成了links參數,使得兩個服務可以繼續互聯訪問
[root@docker lnmp]# docker-compose -f lnmp.yml up Creating network "lnmp_default" with the default driver Creating lnmp-php ... Creating lnmp-php ... done Creating lnmp-nginx ... Creating lnmp-nginx ... done Attaching to lnmp-php, lnmp-nginx
並且啓動的時候建立的默認的網絡
[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_default (172.19.0.2): icmp_seq=1 ttl=64 time=0.091 ms 64 bytes from lnmp-php.lnmp_default (172.19.0.2): icmp_seq=2 ttl=64 time=0.107 ms
--links也是可以使得服務間進行訪問的
如今經過案例講解參數external_links的做用:external_links相似於links針對的也是service級別,可是external_links對應的是外部的service而不是同一compose配置文件中的service
[root@docker lnmp]# cat lnmp.yml version: '3' services: nginx: image: nginx container_name: lnmp-nginx depends_on: - php ports: - "80:80" networks: - "net1" volumes: - "/www:/usr/local/nginx/html" external_links: - php1:php php: image: php container_name: lnmp-php expose: - "9000" networks: - "net1" volumes: - "/www:/usr/local/nginx/html" networks: net1: driver: bridge
上面能夠看見external_links配置了php1的服務,而且將php1的服務也取別名爲了php,而後編寫另外一個compose配置文件,並配置服務名爲php1的服務:
[root@docker lnmp]# cat php1.yml version: '3' services: php1: image: php container_name: lnmp-php1 expose: - "9000" networks: - "net1" volumes: - "/www:/usr/local/nginx/html" networks: net1: driver: bridge
結合兩個compose配置文件,能夠知道有nginx、php、php1的服務,而且php1的服務也有php的別名,如今依次開啓php1,和nginx與php:
[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
[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
查看啓動的容器狀態:
[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
如今將nginx一塊兒的php的這個服務中止掉,看下nginx服務是否將會被中止:
[root@docker lnmp]# docker-compose -f lnmp.yml stop php Stopping lnmp-php ... done
而在nginx這裏的輸出以下:
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
而php服務對應的容器lnmp-php也中止了,可是nginx的服務卻沒有中止,爲何呢?那是由於php1的服務並無中止,而php1的服務也是別名php服務的,而external_links就是實現這種狀況的
使一個compose中的服務能與另外一個compose中的服務可以互聯通訊,compose就是service級別的實現,配置文件中對應的都是service級別,而不是容器名
而在另外一個compose中也必須定義與之相同的網絡名稱,也就是說配置external_links時,二者服務間的網絡名稱必須一致,否則依舊不能進行互相訪問
當使用了external_links時,必須啓動另外一個compose的service,若是僅僅啓動的是包含此函數的compose那麼service將會啓動報錯
[root@docker lnmp]# cat lnmp.yml_external_links version: '3' services: nginx: image: nginx container_name: lnmp-nginx depends_on: - php ports: - "80:80" networks: - "net1" volumes: - "/www:/usr/local/nginx/html" external_links: - php1:php php: image: php container_name: lnmp-php expose: - "9000" networks: - "net1" volumes: - "/www:/usr/local/nginx/html" networks: net1: driver: bridge
[root@docker lnmp]# docker-compose -f lnmp.yml up --build 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-nginx | nginx: [emerg] host not found in upstream "php" in /usr/local/nginx/conf/vhost/www.conf:7 lnmp-nginx exited with code 1
參數external_links鏈接的外部php服務並無啓動,因此nginx仍是啓動報錯