docker network基礎

wadeson發表於2017-11-12

前面介紹了nginx與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取一個別名)

現在使用另外一種方式替代--link來達到容器間的通訊:docker network

檢視local的網路資訊:

[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啟動容器提供服務:

[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是容器名(這裡管理的是容器級別)

由於能夠ping通php,所以在nginx中配置的:

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實現了容器間的訪問

 

上面在容器啟動的時候使用的是選項--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還是啟動報錯

相關文章