- docker network create
- docker network connect
- docker network ls
- docker network rm
- docker network disconnect
- docker network inspect
创建网络
zane@zane-V:~$ docker network create simple-network
8bf58f43c56622d1100f7da9ef6506e45a4aa68556b586311f3756130c311d75
zane@zane-V:~$ docker network inspect simple-network
[
{
"Name": "simple-network",
"Id": "8bf58f43c56622d1100f7da9ef6506e45a4aa68556b586311f3756130c311d75",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.20.0.0/16",
"Gateway": "172.20.0.1/16"
}
]
},
"Internal": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
不像bridge网络,直接可以创建。创建overlay网络需要一些条件,比如:
- 进入一个键值存储。引擎支持Consul,Etcd,ZooKeeper.
- 与键值存储连接的主机集群。
- 在群集中的每个主机上正确配置的deamon引擎
支持overlay网络的docker选项:
- --cluster-store
- --cluster-store-opt
- --cluster-advertise
使用--subnet选项直接指定子网络,在bridge网络中只可以指定一个子网络,而在overlay网络中支持多个子网络。
除了--subnet,还可以指定:--gateway,--ip-range,--aux-address选项。
$ docker network create -d overlay \
--subnet=192.168.0.0/ \
--subnet=192.170.0.0/ \
--gateway=192.168.0.100 \
--gateway=192.170.0.100 \
--ip-range=192.168.1.0/ \
--aux-address="my-router=192.168.1.5"
--aux-address="my-switch=192.168.1.6" \
--aux-address="my-printer=192.170.1.5"
--aux-address="my-nas=192.170.1.6" \
my-multihost-network
如何要创建自己定制的网络,docker也是支持很多选项的。
可以指定网络的端口号:
$ docker run -d -P --name redis --network my-network redis
bafb0c808c53104b2c90346f284bda33a69beadcab4fc83ab8f2c5a4410cd129 $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bafb0c808c53 redis "/entrypoint.sh redis" seconds ago Up seconds 172.23.0.1:->/tcp redis
连接容器
可以连接已存在的容器到一个或者多个网络中。一个容器可以连接到多个不同网络驱动的网络中。
当连接一旦建立,容器便可以可其他的容器通讯,通过IP 或者 容器名称。
基本容器网络实例:
1.创建两个容器,container1 和 container2
$ docker run -itd --name=container1 busybox
18c062ef45ac0c026ee48a83afa39d25635ee5f02b58de4abc8f467bcaa28731 $ docker run -itd --name=container2 busybox
498eaaaf328e1018042c04b2de04036fc04719a6e39a097a4f4866043a2c2152
2.创建一个隔离,bridge网络来测试
zane@zane-V:~$ docker network create -d bridge --subnet 172.25.0.0/ isolated_nw
a8208641505d2d8fc37bf7cbd1027c01f0def461815786e076ef4ae65b7b2f9b
3.连接container2到这个网络,然后验证一下:
zane@zane-V:~$ docker network connect isolated_nw container2 zane@zane-V:~$ docker network inspect isolated_nw
[
{
"Name": "isolated_nw",
"Id": "a8208641505d2d8fc37bf7cbd1027c01f0def461815786e076ef4ae65b7b2f9b",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.25.0.0/16"
}
]
},
"Internal": false,
"Containers": {
"e9bce535ae32945f5e43340facdb6c16c93d92119e85b61c6cb7a5379a0caf63": {
"Name": "container2",
"EndpointID": "ef7244d32484407c3ec4aa30b7bdb0a6cbe3dbbfedc03e5c856ad20a08af172f",
"MacAddress": "02:42:ac:19:00:02",
"IPv4Address": "172.25.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
注意container2,自动分配到了IP地址。此时container1,仍然连接在默认的bridge网络。
4.启动第三个container,但是这是使用--ip 选项指定它的IP地址,
zane@zane-V:~$ docker run --network=isolated_nw --ip=172.25.3.3 -itd --name=container3 busybox
adf68dd9e09c101e507e2106901d25270f71375f07ac7f61db21707c27075c0d
5.检查container3使用的是哪个网络:
"Networks": {
"isolated_nw": {
"IPAMConfig": {
"IPv4Address": "172.25.3.3"
},
"Links": null,
"Aliases": [
"adf68dd9e09c"
],
"NetworkID": "a8208641505d2d8fc37bf7cbd1027c01f0def461815786e076ef4ae65b7b2f9b",
"EndpointID": "71d5d272d056b6111a83f0843a10d1944f1648f34d5099258d5865d053a939b0",
"Gateway": "172.25.0.1",
"IPAddress": "172.25.3.3",
"IPPrefixLen": ,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": ,
"MacAddress": "02:42:ac:19:03:03"
}
}
}
6.检查container2使用的是哪个网络:
"Networks": {
"isolated_nw": {
"Aliases": [
"e9bce535ae32"
],
"EndpointID": "ef7244d32484407c3ec4aa30b7bdb0a6cbe3dbbfedc03e5c856ad20a08af172f",
"Gateway": "172.25.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": ,
"IPAMConfig": {},
"IPAddress": "172.25.0.2",
"IPPrefixLen": ,
"IPv6Gateway": "",
"Links": null,
"MacAddress": "02:42:ac:19:00:02",
"NetworkID": "a8208641505d2d8fc37bf7cbd1027c01f0def461815786e076ef4ae65b7b2f9b"
}
},
注意:container2 在两个网络中间,它加入了默认bridge网络,当你在创建它的时候,然后又连接它到了isolation_nw.
一个容器可以连接到多个网络中
7.使用docker attach 命令连接一个正在运行的容器,然后查看
zane@zane-V:~$ docker attach container2
/ # ifconfig -a
eth1 Link encap:Ethernet HWaddr ::AC:::
inet addr:172.25.0.2 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80:::acff:fe19:/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (11.5 KiB) TX bytes: (648.0 B) eth2 Link encap:Ethernet HWaddr ::AC:::
inet addr:172.17.0.3 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80:::acff:fe11:/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (3.7 KiB) TX bytes: (648.0 B) lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::/ Scope:Host
UP LOOPBACK RUNNING MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (0.0 B) TX bytes: (0.0 B)
8.可以通过容器名称来相互连接
/ # ping -w container3
PING container3 (172.25.3.3): data bytes
bytes from 172.25.3.3: seq= ttl= time=0.077 ms
bytes from 172.25.3.3: seq= ttl= time=0.049 ms
bytes from 172.25.3.3: seq= ttl= time=0.047 ms
bytes from 172.25.3.3: seq= ttl= time=0.054 ms
但是注意:默认bridge网络中是不支持容器名称通信的:
虽然container1 和 container2 都在bridge网络中,但是他们是不支持 容器名称通信的。
但是肯定支持IP地址通信。
zane@zane-V:~$ docker attach container2
/ # ping container3
PING container3 (172.25.3.3): data bytes
bytes from 172.25.3.3: seq= ttl= time=0.042 ms
bytes from 172.25.3.3: seq= ttl= time=0.050 ms
bytes from 172.25.3.3: seq= ttl= time=0.063 ms
^C
--- container3 ping statistics ---
packets transmitted, packets received, % packet loss
round-trip min/avg/max = 0.042/0.051/0.063 ms / # ping -w container1
ping: bad address 'container1' / # ping -w 172.17.0.2
PING 172.17.0.2 (172.17.0.2): data bytes
bytes from 172.17.0.2: seq= ttl= time=0.104 ms
bytes from 172.17.0.2: seq= ttl= time=0.052 ms
bytes from 172.17.0.2: seq= ttl= time=0.127 ms
bytes from 172.17.0.2: seq= ttl= time=0.057 ms --- 172.17.0.2 ping statistics ---
packets transmitted, packets received, % packet loss
round-trip min/avg/max = 0.052/0.085/0.127 ms
注意退出attach 时,使用ctr-p + ctr-q.
如果使用ctr-d 则会stop container.
9.此时container3是不能通过ip地址和container1通信的。
zane@zane-V:~$ docker attach container3
/ # ping -w 172.17.0.2
PING 172.17.0.2 (172.17.0.2): data bytes --- 172.17.0.2 ping statistics ---
packets transmitted, packets received, % packet loss
链接容器而不使用用户定义的网络
上面的实验我们知道,用户自定义的网络,是可以相互解析容器名的,也就是可以用容器名来相互同行。
而默认网络中是不可以的。那如果想让默认网络中,也可以使用容器名进行通信呢?
使用link的特性。这是唯一推荐使用link的场景。应该使用自定义的网络来替代它的。
在默认网络中使用link增加了一下特性:
- 解析容器名到IP地址
- 定义网络别名
- --link=CONTAINER-NAME:ALIAS
- 增强网络连接的安全性
- 环境变量注入
断开容器连接
1.断开container2和isolated_nw的连接,然后
zane@zane-V:~$ docker network disconnect isolated_nw container2
2.删除网络
zane@zane-V:~$ docker network rm simple-network
simple-network
总结
- 创建网络
- docker network create simple-network
- overlay网络条件
- 进入一个键值存储
- 与键值存储连接的主机集群
- 在集群中每个主机上正确配置deamon引擎
- 支持overlay网络的docker选项
- --cluser-store
- --cluster-store-opt
- --cluster-advertise
- 指定子网络,网关,地址范围
- 将容器添加到网络中
- docker network connect isolated_nw container2
- 一个容器可以连接到多个网络中
- 连接一个正在运行的容器
- docker attach
- attach 的退出
- ctr p + ctr q
- 默认bridge网络不支持,容器名称通信,其他网络支持;
- 使用link 来支持默认网络的容器名称通信
- 断开连接
- docker network disconnect isolated_nw container2
- 删除网络
- docker network rm simple-network
- 检测网络
- docker network inspect isolated_nw