docker容器之间如何互相访问

摘要:docker容器之间如何互相访问

docker network 命令介绍

1
2
3
4
5
6
7
Commands:
create Create a network(创建一个网络)
connect Connect a container to a network(将容器连接至网络)
disconnect Disconnect a container from a network(将容器从某网络断开)
inspect Display detailed information on one or more networks(显示一个或多个网络的详细信息)
ls List networks(列出所有网络)
rm Remove one or more networks(删除一个或多个网络)

操作步骤

  1. 查看当前的网络

    1
    2
    3
    4
    5
    6
    [root@host ~]# docker network ls
    NETWORK ID NAME DRIVER SCOPE
    3dd4643bb158 bridge bridge local
    748b765aca52 host host local
    4d59a0cd3ff4 none null local
    [root@host ~]
  2. 创建一个bridge的网络(如不加参数,默认创建的就是bridge类型的网络)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    [root@host ~]# docker network create my_net
    a80ae06b65918f5e653faae643af6dbcbe2f4607053211a0528d24f62e46f649
    [root@host ~]# docker network ls
    NETWORK ID NAME DRIVER SCOPE
    3dd4643bb158 bridge bridge local
    748b765aca52 host host local
    a80ae06b6591 my_net bridge local
    4d59a0cd3ff4 none null local
    [root@host ~]
  3. 查看该网络的详细信息

    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
    [root@host ~]# docker network inspect my_net
    [
    {
    "Name": "my_net",
    "Id": "a80ae06b65918f5e653faae643af6dbcbe2f4607053211a0528d24f62e46f649",
    "Scope": "local",
    "Driver": "bridge",
    "EnableIPv6": false,
    "IPAM": {
    "Driver": "default",
    "Options": {},
    "Config": [
    {
    "Subnet": "172.18.0.0/16",
    "Gateway": "172.18.0.1/16"
    }
    ]
    },
    "Internal": false,
    "Containers": {},
    "Options": {},
    "Labels": {}
    }
    ]
    [root@host ~]
  4. 启动容器时指定网络
    注:需要使用–name为容器指定一个名字

    1
    2
    3
    [root@host ~]# docker run --name boot1 -d --network my_net bbacc5de5941
    7ca3834887355e3ecb4a2ae484694c2dbc0944f12e23498fd4b03237c4c35632
    [root@host ~]#
  5. 将已启动的应用连接至网络
    注:需要使用–name为容器指定一个名字

    1
    2
    3
    4
    [root@host ~]# docker run --name boot2 -d b8ea3d603e18
    d281bfeb900325724968ec89f7e4623f9add844d101cf4773f3b14c36253f301
    [root@host ~]# docker network connect my_net boot2
    [root@host ~]#
  6. 再启动一个centos7容器,同时连接到该网络

    1
    2
    [root@host ~]# docker run -it --name my_centos --network my_net centos:7
    [root@b4f27e7db4c0 /]
  7. 在centos7容器中访问另外的两个容器。
    注:这里boot1、boot2是两个spring-boot项目,端口均为8080。

    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
    [root@b4f27e7db4c0 /]# ping -c 4 boot1
    PING boot1 (172.18.0.2) 56(84) bytes of data.
    64 bytes from boot1.my_net (172.18.0.2): icmp_seq=1 ttl=64 time=0.055 ms
    64 bytes from boot1.my_net (172.18.0.2): icmp_seq=2 ttl=64 time=0.089 ms
    64 bytes from boot1.my_net (172.18.0.2): icmp_seq=3 ttl=64 time=0.097 ms
    64 bytes from boot1.my_net (172.18.0.2): icmp_seq=4 ttl=64 time=0.084 ms

    --- boot1 ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3111ms
    rtt min/avg/max/mdev = 0.055/0.081/0.097/0.017 ms
    [root@b4f27e7db4c0 /]# ping -c 4 boot2
    PING boot2 (172.18.0.3) 56(84) bytes of data.
    64 bytes from boot2.my_net (172.18.0.3): icmp_seq=1 ttl=64 time=0.065 ms
    64 bytes from boot2.my_net (172.18.0.3): icmp_seq=2 ttl=64 time=0.090 ms
    64 bytes from boot2.my_net (172.18.0.3): icmp_seq=3 ttl=64 time=0.078 ms
    64 bytes from boot2.my_net (172.18.0.3): icmp_seq=4 ttl=64 time=0.101 ms

    --- boot2 ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3085ms
    rtt min/avg/max/mdev = 0.065/0.083/0.101/0.016 ms
    [root@b4f27e7db4c0 /]# curl http://boot1:8080/
    Hello Docker World[root@b4f27e7db4c0 /]#
    [root@b4f27e7db4c0 /]# curl http://boot2:8080/
    Hello Docker helloaaasdfasdf[root@b4f27e7db4c0 /]#
    [root@b4f27e7db4c0 /]#
  8. 查看网络详细信息

    注:按组合键ctrl+P+Q退出my_centos容器

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
[root@b4f27e7db4c0 /]# [root@host ~]#
[root@host ~]# docker network inspect my_net
[
{
"Name": "my_net",
"Id": "a80ae06b65918f5e653faae643af6dbcbe2f4607053211a0528d24f62e46f649",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1/16"
}
]
},
"Internal": false,
"Containers": {
"7ca3834887355e3ecb4a2ae484694c2dbc0944f12e23498fd4b03237c4c35632": {
"Name": "boot1",
"EndpointID": "10a5dad08d25fb5fbefdf3240b8b94edcf76e6e40ef8f1bb8fe029d288338e75",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
},
"b4f27e7db4c01105ec2d59b34cf7971929d4c8563c3d3c4b6ff8bb61d031fe34": {
"Name": "my_centos",
"EndpointID": "9cbb7f1c9747787e70c0f94cc4924c987b084553426fbfa2b62713c178614498",
"MacAddress": "02:42:ac:12:00:04",
"IPv4Address": "172.18.0.4/16",
"IPv6Address": ""
},
"d281bfeb900325724968ec89f7e4623f9add844d101cf4773f3b14c36253f301": {
"Name": "boot2",
"EndpointID": "19b58907f5209b98f316c83dddba08c0eacc38a677162767250dbdf4fbb9f3fa",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
[root@host ~]#