【Docker系列】Docker Compose 网络

准备环境

创建一个network文件夹,里面放 docker-compose.yml文件

使用老师准备的好镜像net-box:latest

version: "3.8"

services: 
  box1: 
    image: xiaopeng163/net-box:latest
    command: /bin/sh -c "while true;do sleep 3600;done"
    
  box2: 
    image: xiaopeng163/net-box:latest
    command: /bin/sh -c "while true;do sleep 3600;done"

执行,查看docker-compose默认创建的网络

PS F:\docker\network> docker-compose pull
Pulling box1 ... done
Pulling box2 ... done
PS F:\docker\network> docker-compose up -d
Creating network "network_default" with the default driver
Creating network_box1_1 ... done
Creating network_box2_1 ... done
PS F:\docker\network> docker network ls
NETWORK ID     NAME                     DRIVER    SCOPE
fd874a89c549   bridge                   bridge    local
beb5c11f8216   host                     host      local
9419c5cc9528   network_default          bridge    local
da113904c63c   none                     null      local

默认创建一个网络network_defaultnetwork前缀是文件夹的名字,default默认名称

查看network_default网络,两个容器box1,box2在这个网络上,ip地址是172.21.0.2/16,172.21.0.3/16

PS F:\docker\network> docker network inspect network_default
[
    {
        "Name": "network_default",
        "Id": "9419c5cc95288ab0f055d34ddb850c4ccd5dda17b04ee0f9b87a96dad24b6b80",
        "Created": "2022-01-21T16:12:13.0676126Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.21.0.0/16",
                    "Gateway": "172.21.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "cc04559d14c2fcf984cf1ac44a592f0f7f97af48b326fd1a6bec8cf8dfb7421a": {
                "Name": "network_box1_1", # 容器box1
                "EndpointID": "3dec346ab8db601c3aede98ff0c9b6e052a2e6b6b6d0c8d822f35f75f3145706",
                "MacAddress": "02:42:ac:15:00:02",
                "IPv4Address": "172.21.0.2/16",
                "IPv6Address": ""
            },
            "fdcf1fc7717ec0371e974bebf85f449e9cd6f087c00733a3c2591703310f71bd": {
                "Name": "network_box2_1",  # 容器box2
                "EndpointID": "da84bdabbda3851a2db919b7e3bf9774643b1b2c0e9a6a197b3c26bef6e505cc",
                "MacAddress": "02:42:ac:15:00:03",
                "IPv4Address": "172.21.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "default",
            "com.docker.compose.project": "network",
            "com.docker.compose.version": "1.29.2"
        }
    }
]

查看两个容器的网络状态

两个容器相互连通

PS F:\docker\network> docker-compose ps
     Name                   Command               State   Ports
---------------------------------------------------------------
network_box1_1   /bin/sh -c while true;do s ...   Up
network_box2_1   /bin/sh -c while true;do s ...   Up
PS F:\docker\network> docker container exec -it network_box1_1 sh
/omd # ping network_box2_1
PING network_box2_1 (172.21.0.3): 56 data bytes
64 bytes from 172.21.0.3: seq=0 ttl=64 time=0.107 ms
64 bytes from 172.21.0.3: seq=1 ttl=64 time=0.060 ms
64 bytes from 172.21.0.3: seq=2 ttl=64 time=0.062 ms
^C
--- network_box2_1 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.060/0.076/0.107 ms
/omd # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: sit0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN qlen 1000
    link/sit 0.0.0.0 brd 0.0.0.0
35: eth0@if36: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
    link/ether 02:42:ac:15:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.21.0.2/16 brd 172.21.255.255 scope global eth0
       valid_lft forever preferred_lft forever
/omd #

可以直接可以ping,docker-compose.yml文件里面容器名称,box1,box2

/omd # ping box1
PING box1 (172.21.0.2): 56 data bytes
64 bytes from 172.21.0.2: seq=0 ttl=64 time=0.037 ms
64 bytes from 172.21.0.2: seq=1 ttl=64 time=0.046 ms
^C
--- box1 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.037/0.041/0.046 ms
/omd # ping box2
PING box2 (172.21.0.3): 56 data bytes
64 bytes from 172.21.0.3: seq=0 ttl=64 time=0.068 ms
64 bytes from 172.21.0.3: seq=1 ttl=64 time=0.062 ms
^C
--- box2 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.062/0.065/0.068 ms
/omd #

查看DNS
dig命令

SERVER: 127.0.0.11#53(127.0.0.11) 内置的DNS Server

/omd # dig

; <<>> DiG 9.16.6 <<>>
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 29311
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: 7a15f2f0489fc203 (echoed)
;; QUESTION SECTION:
;.                              IN      NS

;; Query time: 3190 msec
;; SERVER: 127.0.0.11#53(127.0.0.11)
;; WHEN: Fri Jan 21 16:21:53 UTC 2022
;; MSG SIZE  rcvd: 40

/omd # more /etc/resolv.conf
nameserver 127.0.0.11
options ndots:0

当时主机上是ping不同的,因为用的不是一个DNS

想要ping百度,容器的DSN是找不到百度的,需要进入本机的DNS找,一层层往上找DNS

/omd # ping www.baidu.com
PING www.baidu.com (110.242.68.4): 56 data bytes
64 bytes from 110.242.68.4: seq=0 ttl=37 time=9.406 ms
64 bytes from 110.242.68.4: seq=1 ttl=37 time=9.472 ms
^C
--- www.baidu.com ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 9.398/9.448/9.516 ms
/omd #

自定义网络

docker-compose.yml文件:

version: "3.8"

services: 
  box1: 
    image: xiaopeng163/net-box:latest
    command: /bin/sh -c "while true;do sleep 3600;done"
    networks:
      - mynetwork1
    
  box2: 
    image: xiaopeng163/net-box:latest
    command: /bin/sh -c "while true;do sleep 3600;done"
    networks:
      - mynetwork1
      - mynetwork2
   
networks:
  mynetwork1: 
  mynerwork2:

输出:

PS F:\docker\network> docker-compose up -d
Creating network "network_mynetwork1" with the default driver
Creating network "network_mynetwork2" with the default driver
Recreating network_box1_1 ... done
Recreating network_box2_1 ... done
PS F:\docker\network>

自动创建两个网络network_mynetwork1network_mynetwork2 默认的dirver

PS F:\docker\network> docker network ls
NETWORK ID     NAME                     DRIVER    SCOPE
fd874a89c549   bridge                   bridge    local
beb5c11f8216   host                     host      local
8951aab48be4   network_mynetwork1       bridge    local
b7b92098628a   network_mynetwork2       bridge    local
da113904c63c   none                     null      local
PS F:\docker\network>

network_mynetwork1有两个容器连在上面

PS F:\docker\network> docker network inspect network_mynetwork1
[
    {
        "Name": "network_mynetwork1",
        "Id": "8951aab48be4a39984ed097dcf0781872a5d37f00f09c785075531acfc0d351f",
        "Created": "2022-01-21T16:28:45.3669689Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.22.0.0/16",
                    "Gateway": "172.22.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "0fa98dd0fdaca47a9fc9c7193c524afc32466fb164794115295b08d1f6f4b5d8": {
                "Name": "network_box2_1",
                "EndpointID": "f4724a4b0e539d98b1bc9be5be8c57cd120a646cffcb6a716da9a9c6ba800190",
                "MacAddress": "02:42:ac:16:00:02",
                "IPv4Address": "172.22.0.2/16",
                "IPv6Address": ""
            },
            "51521d22e5e80066fb637ff2b73fbf17ba784aea079d36aa11c2fe1a41939a14": {
                "Name": "network_box1_1",
                "EndpointID": "e9bf65e2c5c132ddc2a80d892994305a05ac789314059ddd8eec044bd56856e5",
                "MacAddress": "02:42:ac:16:00:03",
                "IPv4Address": "172.22.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "mynetwork1",
            "com.docker.compose.project": "network",
            "com.docker.compose.version": "1.29.2"
        }
    }
]

network_mynetwork2有一个容器连在上面

PS F:\docker\network> docker network inspect network_mynetwork2
[
    {
        "Name": "network_mynetwork2",
        "Id": "b7b92098628afd412652513c2b2e1f42565ed62924b3842ccf1a0fe23b3e7ff0",
        "Created": "2022-01-21T16:28:45.4016942Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.23.0.0/16",
                    "Gateway": "172.23.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "0fa98dd0fdaca47a9fc9c7193c524afc32466fb164794115295b08d1f6f4b5d8": {
                "Name": "network_box2_1",
                "EndpointID": "fea91083d027f3765817251d0a9e2b15ee846edb579ec2044682ed9c40ad5da1",
                "MacAddress": "02:42:ac:17:00:02",
                "IPv4Address": "172.23.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "mynetwork2",
            "com.docker.compose.project": "network",
            "com.docker.compose.version": "1.29.2"
        }
    }
]
PS F:\docker\network>

进入容器ip a命令可以看到box2的网络连接了两个eth,box1连接一个eth

/omd # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: sit0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN qlen 1000
    link/sit 0.0.0.0 brd 0.0.0.0
41: eth1@if42: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
    link/ether 02:42:ac:17:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.23.0.2/16 brd 172.23.255.255 scope global eth1
       valid_lft forever preferred_lft forever
43: eth0@if44: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
    link/ether 02:42:ac:16:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.22.0.2/16 brd 172.22.255.255 scope global eth0
       valid_lft forever preferred_lft forever
/omd #

每个自定义网络可以添加配置

version: "3.8"

services: 
  box1: 
    image: xiaopeng163/net-box:latest
    command: /bin/sh -c "while true;do sleep 3600;done"
    networks:
      - mynetwork1
    
  box2: 
    image: xiaopeng163/net-box:latest
    command: /bin/sh -c "while true;do sleep 3600;done"
    networks:
      - mynetwork1
      - mynetwork2
   
networks:
  mynetwork1: 
    ipam: 
      driver: default
  mynetwork2: 
    ipam: 
      driver: default

官方文档network-configuration-reference参数:https://docs.docker.com/compose/compose-file/compose-file-v3/#network-configuration-reference

ipam

指定自定义 IPAM 配置。这是一个具有多个属性的对象,每个属性都是可选的:

driver:自定义 IPAM 驱动程序,而不是默认的。
config:具有零个或多个配置块的列表,每个配置块包含以下任何键:
subnet: CIDR 格式的子网,代表一个网段

一个完整的例子:

ipam:
  driver: default
  config:
    - subnet: 172.28.0.0/16

你可能感兴趣的:(Docker系列,docker,网络,容器,docker-compose)