参考文献

Docker网络

查看网络信息

1
2
3
4
5
6
7
8
# docker network ls
NETWORK ID NAME DRIVER SCOPE
7731f800c2eb bridge bridge local
27625663ead5 docker_default bridge local
4b09ca6ec339 host host local
ec7e23d9e6bb mongodb_default bridge local
23e5b7358579 none null local
52dda41afae6 prod_default bridge local

查看某个网络的详细信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# docker network inspect 7731f800c2eb
[
{
"Name": "bridge",
"Id": "7731f800c2eb086a0db06948a3d1c1e666d08f2920926474f793b617f036dbe8",
"Created": "2022-09-20T15:21:13.055212124+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": {
"24016356bbc3ec0d57474b2030885737fcafed3175df916f2c7ce533b8feb801": {
"Name": "xxx-mini",
"EndpointID": "b1421068887e0844953a244d2587938c11bbd7d4200d8b3f100548b61ca4fcff",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
},
"4691f17d99669713f66ba85ed0623417e03ac0e64aeeb7da5904747e3b128cb3": {
"Name": "xxx1",
"EndpointID": "d9fedee3c83f97f93c535de869a62a14d865e8b8faa9c9d709fe14e06884ebff",
"MacAddress": "02:42:ac:11:00:04",
"IPv4Address": "172.17.0.4/16",
"IPv6Address": ""
},
"864872133d3d5279c8254ae2ff3ad3369571c7c85d49c41b89d8dd67d1c8e1f5": {
"Name": "xxx",
"EndpointID": "4e37c171127ec3a3eb35578ca18cfd5870a29d059872dfafa550cfe39ccff0e6",
"MacAddress": "02:42:ac:11:00:06",
"IPv4Address": "172.17.0.6/16",
"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中两个不同网络中的容器互通呢?

  • 使用docker network connect

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    # docker network --help

    Usage: docker network COMMAND

    Manage networks

    Commands:
    connect Connect a container to a network
    create Create a network
    disconnect Disconnect a container from a network
    inspect Display detailed information on one or more networks
    ls List networks
    prune Remove all unused networks
    rm Remove one or more networks

    Run 'docker network COMMAND --help' for more information on a command.
    # docker network connect --help

    Usage: docker network connect [OPTIONS] NETWORK CONTAINER

    Connect a container to a network

    Options:
    --alias strings Add network-scoped alias for the container
    --driver-opt strings driver options for the network
    --ip string IPv4 address (e.g., 172.30.100.104)
    --ip6 string IPv6 address (e.g., 2001:db8::33)
    --link list Add link to another container
    --link-local-ip strings Add a link-local address for the container
  • 将容器container-x加入到网络network-y

    1
    docker network connect network-y container-x