版权所有,未经许可,禁止转载
章节
Docker提供了内部网络功能,容器与容器,容器与宿主机之间可以组建网络通信。如果在宿主机上执行ifconfig,可看到Docker网卡配置信息,这个是在主机上安装Docker时创建的。
[root@qikegu /]# ifconfig
...
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
inet6 fe80::42:49ff:fe2a:8f69 prefixlen 64 scopeid 0x20<link>
ether 02:42:49:2a:8f:69 txqueuelen 0 (Ethernet)
RX packets 13643 bytes 556006 (542.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 25918 bytes 34465275 (32.8 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
...
Docker网卡是Docker容器和Linux主机之间的桥梁。现在让我们看看Docker中与网络相关的一些命令。
列出所有Docker网络
使用以下命令可以列出Docker的全部网络:
docker network ls
示例
[root@qikegu /]# docker network ls
NETWORK ID NAME DRIVER SCOPE
a01805a42152 bridge bridge local
60f77bd6d128 deploly_default bridge local
257076d69def host host local
8643edd58814 none null local
检查Docker网络
如果想查看Docker网络的更多细节,可以使用以下命令:
docker network inspect networkname/networkID
示例
[root@qikegu /]# docker network inspect bridge
[
{
"Name": "bridge",
"Id": "a01805a42152776b39aac8a89a4d06a4c3996a1721ccf1a62415e26dc2a2adaf",
"Created": "2019-05-21T10:56:35.367205598+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": {},
"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": {}
}
]
现在让我们运行一个容器,然后再次查看bridge网络详情,看看会发生什么。
让我们用下面的命令启动一个Ubuntu容器
[root@qikegu ~]# docker run -it --rm ubuntu /bin/bash
root@f724b706913b:/#
现在,通过下面的命令查看bridge网络详情,可以看到容器加入了bridge网络。
[root@qikegu /]# docker network inspect bridge
[
{
...
"Containers": {
"f724b706913b30f77d024be9a0c5c19ece87ca806dc4237480243ea90213a149": {
"Name": "modest_elbakyan",
"EndpointID": "457288f3d811802c91ef769ff3c6aa1b8a86cf11d5bd3db2313593cfb93e67a8",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
}
},
...
}
]
容器启动时,如果没有指定网络,默认会加入bridge网络。
创建自己的网络
可以在启动容器之前,在Docker中创建一个网络:
docker network create --driver drivername name
drivername
- 网络驱动程序的名称name
- 网络的名称
该命令将输出新网络的长ID。
示例
创建网络:
[root@qikegu /]# docker network create --driver bridge my_network
f8e40a0a5cb547bed363b3fee5eb53bb8730e1b9404f9687a5a6c0674f1b70d7
现在,您可以在启动容器时加入新的网络。让我们用下面的命令启动一个Ubuntu容器
[root@qikegu ~]# docker run -it --network=my_network --rm ubuntu /bin/bash
root@a4735bf2379e:/#
现在,当您通过以下命令查看my_network网络详情时,可以看到ubuntu容器加入了网络(名字:relaxed_feynman)。
[root@qikegu /]# docker network inspect my_network
[
{
"Name": "my_network",
"Id": "f8e40a0a5cb547bed363b3fee5eb53bb8730e1b9404f9687a5a6c0674f1b70d7",
"Created": "2019-05-21T20:26:27.129007295+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.20.0.0/16",
"Gateway": "172.20.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"a4735bf2379e2e0b6c0083c3e438c876204f53abbbce49de2672eddc2400cb98": {
"Name": "relaxed_feynman",
"EndpointID": "2bb5e011bea7a42aea61272e23cd993c1408b5ce95f030639c7159a52f13063f",
"MacAddress": "02:42:ac:14:00:02",
"IPv4Address": "172.20.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
[root@qikegu /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a4735bf2379e ubuntu "/bin/bash" 5 minutes ago Up 5 minutes relaxed_feynman
可以看到ubuntu容器已经加入到my_network网络了。