docker容器之间如何互相访问
摘要:docker容器之间如何互相访问
docker network 命令介绍
1 | Commands: |
操作步骤
查看当前的网络
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 ~]创建一个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 ~]查看该网络的详细信息
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 ~]启动容器时指定网络
注:需要使用–name为容器指定一个名字1
2
3[root@host ~]# docker run --name boot1 -d --network my_net bbacc5de5941
7ca3834887355e3ecb4a2ae484694c2dbc0944f12e23498fd4b03237c4c35632
[root@host ~]#将已启动的应用连接至网络
注:需要使用–name为容器指定一个名字1
2
3
4[root@host ~]# docker run --name boot2 -d b8ea3d603e18
d281bfeb900325724968ec89f7e4623f9add844d101cf4773f3b14c36253f301
[root@host ~]# docker network connect my_net boot2
[root@host ~]#再启动一个centos7容器,同时连接到该网络
1
2[root@host ~]# docker run -it --name my_centos --network my_net centos:7
[root@b4f27e7db4c0 /]在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 /]#查看网络详细信息
注:按组合键
ctrl+P+Q
退出my_centos容器
1 | [root@b4f27e7db4c0 /]# [root@host ~]# |