网络模式

网桥bridge

查看系统中docker使用的网络: docker0
[root@localhost ~]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:61:01:cf:98  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.128.141  netmask 255.255.255.0  broadcast 192.168.128.255
        inet6 fe80::6f6d:5f8b:a124:49e  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:bd:1f:32  txqueuelen 1000  (Ethernet)
        RX packets 212  bytes 25734 (25.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 212  bytes 22224 (21.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
查看docker使用的网络
[root@localhost ~]# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
bcc4eeb09d4a   bridge    bridge    local
322f2fb19d26   host      host      local
0b9fb39c164f   none      null      local
查看docker网桥详细信息
[root@localhost ~]# docker network inspect bcc4eeb09d4a
[
    {
        "Name": "bridge",
        "Id": "bcc4eeb09d4ad48c3c94827504de32a129a19e877fd890f84abf428d4e6d669e",
        "Created": "2022-02-23T23:36:37.182104362-08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1" // 网关
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "618e08ceb4218df27e67841811c2facb173d8401ed0349238e8fd7673a35463c": {
                "Name": "nginx",
                "EndpointID": "bdf6b1fcdb1b5c4739d407dc59e95ebc8d32f3045cd854cf8e85beea7c6cb320",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16", // nginx容器使用的ip
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0", // 默认链接网络
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]

创建

创建网桥, 可通过指定参数设置网关与子网掩码

docker network create --gatway 172.17.0.1 --subnet 172.18.0.0/16 testbridge

[root@localhost ~]# docker network create -d brigdge mybridge
424352b93f1fcbaa4705c2dee98e3439833f3bd71ea96d320ebcbd23d674d3d1
查看网桥使用网段 网关:172.18.0.1
[root@localhost ~]# docker network inspect 424352b93f1f
[
    {
        "Name": "mybridge",
        "Id": "424352b93f1fcbaa4705c2dee98e3439833f3bd71ea96d320ebcbd23d674d3d1",
        "Created": "2022-02-24T00:35:04.543596793-08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "6801a5e2daa9dd6c3cc8a07972994e6d8bda991ebc6a454177cf4f3663eadee9": {
                "Name": "mynginx",
                "EndpointID": "01c2a8aacf3eee2b027bc0ff3fe1009c72e9295b8f175a78c6c745f96a5be676",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

创建容器指定网络

--network 网络名 : 启动容器指定网络
[root@localhost ~]# docker container run --name mynginx -dit -p 80:80 --network mybridge nginx
6801a5e2daa9dd6c3cc8a07972994e6d8bda991ebc6a454177cf4f3663eadee9
查看容器信息(部分) ip地址为: 172.18.0.2
[root@localhost ~]# docker container inspect 6801a5e2daa9
[
    {
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "04e5e10702dc6f0416bf5ce4db304b1c7dbabf439f805ebdb472555b1a692e8d",
            "Networks": {
                "mybridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "6801a5e2daa9"
                    ],
                    "NetworkID": "424352b93f1fcbaa4705c2dee98e3439833f3bd71ea96d320ebcbd23d674d3d1",
                    "EndpointID": "01c2a8aacf3eee2b027bc0ff3fe1009c72e9295b8f175a78c6c745f96a5be676",
                    "Gateway": "172.18.0.1",
                    "IPAddress": "172.18.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:12:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

自定义网桥

Docker为自定义网桥提供了DNS解析功能,在同一自定义网桥下,容器可以通过IP和容器的名字访问,默认网桥下只能通过IP访问

容器名称网桥名称IP地址
nginx1mybridge(自定义)172.18.0.3
nginx2mybridge(自定义)172.18.0.4
nginx3bridge(默认)172.17.0.3
nginx4bridge(默认)172.17.0.4
创建容器 nginx1, nginx2 指定网络模式为自定义网桥
docker container run --name nginx1 -dit -p 8081:80 --network mybridge nginx
docker container run --name nginx2 -dit -p 8082:80 --network mybridge nginx
创建容器 nginx3, nginx4 使用默认网桥
docker container run --name nginx3 -dit -p 8083:80 nginx
docker container run --name nginx4 -dit -p 8084:80 nginx
自定义网桥中, nginx1 访问 nginx2 与nginx4容器名称
# curl nginx2
Hello Qvbilam2

# curl 172.18.0.4
Hello Qvbilam2

# curl nginx4
curl: (6) Could not resolve host: nginx4

# curl 172.17.0.4
curl: (28) Failed to connect to 172.17.0.4 port 80: Connection timed out
默认网桥中, nginx3 访问nginx2 与 nginx4容器名称
# curl nginx2
curl: (6) Could not resolve host: nginx2

# curl 172.18.0.4
curl: (28) Failed to connect to 172.18.0.4 port 80: Connection timed out

# curl nginx4
curl: (6) Could not resolve host: nginx4

# curl 172.17.0.4
Hello Qvbilam4

本机host

与宿主机共享

无none

独立的网络, 没有任何网路设置

容器切换网络

链接

docker network connect 网络名 容器名
[root@localhost ~]# docker network connect mybridge nginx
查看容器使用的网络
[root@localhost ~]# docker container inspect nginx
[
    {
        "Id": "618e08ceb4218df27e67841811c2facb173d8401ed0349238e8fd7673a35463c",
        "Created": "2022-02-24T08:02:03.172251051Z",
        "NetworkSettings": {
            "Networks": {
                "bridge": {
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                },
                "mybridge": {
                    "Gateway": "172.18.0.1",
                    "IPAddress": "172.18.0.3",
                }
            }
        }
    }
]

关闭

docker network disconnect 网络名 容器名
[root@localhost ~]# docker network disconnect mybridge nginx
Last modification:March 27th, 2022 at 04:55 pm