查看docker 网络时会发现docker默认有三个网络,一个是bridge,一个是host,一个是none。
bridge在前面的文章中已经说过,这次说host和none。
[root@vol ~]# docker network list
NETWORK ID NAME DRIVER SCOPE
0ee165ccab6f bridge bridge local
baa1cdd2d1e4 host host local
2cb2a0e5dad5 none null local
我们可以新建容器,然后指定host和none来看看到底有何区别
none
[root@vol ~]# docker run -d --name test1 --network none busybox /bin/sh -c "while true; do sleep 3600;done"
a9afeda6fe4c3dc6f9da6ae2dceb9bf040513df9bfb619cb46dbd5a19aaff616
[root@vol ~]# docker network inspect none
[
{
"Name": "none",
"Id": "2cb2a0e5dad5d270690be48b19e61969fc507a328015577fbc75bf52685652b7",
"Created": "2019-10-09T19:30:16.157801938+08:00",
"Scope": "local",
"Driver": "null",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": []
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"a9afeda6fe4c3dc6f9da6ae2dceb9bf040513df9bfb619cb46dbd5a19aaff616": {
"Name": "test1",
"EndpointID": "45991b1e08b48def2f9b34a7f740c8adfa526df9f510e84acaff31ed90e36030",
"MacAddress": "",
"IPv4Address": "",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
[root@vol ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a9afeda6fe4c busybox "/bin/sh -c 'while t…" 29 seconds ago Up 28 seconds test1
[root@vol ~]# docker exec -it test1 /bin/sh
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
/ # exit
可以看到这个容器里只有本地回环口,没有其他的网络,也就是说明这个容器是一个孤立的,除了来到这台机,然后运行docker exec 能进入容器后,没有其他方式能访问。用于那种保密级别很高,不能放到互联网的应用,例如我有个保存密码的服务,就可以使用这个。
host
如下可以看到,host网络,容器里面看到的ip跟本机看到的ip是一样的,也就是说容器跟主机共享一个网络,这个是有些缺陷的,因为容器跟主机共享网络的话,就会有端口冲突的问题,可以起两个nginx试试,应该是起不来的。
[root@vol ~]# docker run -d --name test2 --network host busybox /bin/sh -c "whil e true; do sleep 3600;done"
8aa6b1d7c8c668057461905d561a696c9baad9f3694f9b7031181a6e415e7be0
[root@vol ~]# docker network inspect host
[
{
"Name": "host",
"Id": "baa1cdd2d1e4547a51719a75ed6b76d3e91c67261c454d6bf730fd72bf90b4d5" ,
"Created": "2019-10-09T19:30:16.169125514+08:00",
"Scope": "local",
"Driver": "host",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": []
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"8aa6b1d7c8c668057461905d561a696c9baad9f3694f9b7031181a6e415e7be0": {
"Name": "test2",
"EndpointID": "12ee3a06dc99949005777cf8b76a618f81cf17e39e10103cc 5e58b36564d4efd",
"MacAddress": "",
"IPv4Address": "",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
[root@vol ~]# docker exec -it test2 /bin/sh
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq qlen 1000
link/ether 00:50:56:87:bd:a7 brd ff:ff:ff:ff:ff:ff
inet 172.31.17.54/16 brd 172.31.255.255 scope global ens160
valid_lft forever preferred_lft forever
inet6 fe80::33db:6382:9c3a:12e8/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::a780:a19:68f2:9347/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::a62f:dd94:b9a2:3027/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue
link/ether 02:42:9d:e3:47:69 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:9dff:fee3:4769/64 scope link
valid_lft forever preferred_lft forever
/ # exit
[root@vol ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group defaul t qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group de fault qlen 1000
link/ether 00:50:56:87:bd:a7 brd ff:ff:ff:ff:ff:ff
inet 172.31.17.54/16 brd 172.31.255.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet6 fe80::33db:6382:9c3a:12e8/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::a780:a19:68f2:9347/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::a62f:dd94:b9a2:3027/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOW N group default
link/ether 02:42:9d:e3:47:69 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:9dff:fee3:4769/64 scope link
valid_lft forever preferred_lft forever
测试起两个nginx容器
## 新建nginx容器,使用host网络
[root@vol ~]# docker run -d --name test3 --network host nginx
7d148274090490840ba059e460c4833a90371093697f2a9102c2ae96757fc840
##nginx容器成功跑起来
[root@vol ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7d1482740904 nginx "nginx -g 'daemon of…" 3 seconds ago Up 2 seconds test3
8aa6b1d7c8c6 busybox "/bin/sh -c 'while t…" 12 hours ago Up 12 hours test2
a9afeda6fe4c busybox "/bin/sh -c 'while t…" 12 hours ago Up 12 hours test1
##再新建一个nginx容器,使用host网络
[root@vol ~]# docker run -d --name test4 --network host nginx
b99bb31a5ec5385a5d814b2798e99ac7b69d63092c2ae2bed23cefbed65e17b1
##发现test4没有跑起来
[root@vol ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7d1482740904 nginx "nginx -g 'daemon of…" About a minute ago Up About a minute test3
8aa6b1d7c8c6 busybox "/bin/sh -c 'while t…" 12 hours ago Up 12 hours test2
a9afeda6fe4c busybox "/bin/sh -c 'while t…" 12 hours ago Up 12 hours test1
##查看所有容器,发现test4退出了
[root@vol ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b99bb31a5ec5 nginx "nginx -g 'daemon of…" 20 seconds ago Exited (1) 17 seconds ago test4
7d1482740904 nginx "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes test3
8aa6b1d7c8c6 busybox "/bin/sh -c 'while t…" 12 hours ago Up 12 hours test2
a9afeda6fe4c busybox "/bin/sh -c 'while t…" 12 hours ago Up 12 hours test1
##查看日志,发现是端口冲突了
[root@vol ~]# docker logs test4
2020/03/06 02:52:50 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2020/03/06 02:52:50 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2020/03/06 02:52:50 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2020/03/06 02:52:50 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2020/03/06 02:52:50 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2020/03/06 02:52:50 [emerg] 1#1: still could not bind()
nginx: [emerg] still could not bind()
由上可知,host网络会产生端口冲突问题。