Docker中的容器網(wǎng)絡(luò)以及其配置說明
Docker中的容器網(wǎng)絡(luò)及其配置
docker容器網(wǎng)絡(luò)
Docker在安裝后自動提供3種網(wǎng)絡(luò), 可以使用 docker network ls 命令查看
[root@Master ~]# docker network ls NETWORK ID NAME DRIVER SCOPE 1924254a040a bridge bridge local 13346f3bc032 host host local c41bdf0688fe none null local
Docker使用 Linux 橋接, 在宿主機虛擬一個 Docker 容器網(wǎng)橋(docker0) , Docker 啟動一個容器時會根據(jù) Docker 網(wǎng)橋的網(wǎng)段分配給容器一個 IP 地址, 稱為 Container-IP, 同時 Docker 網(wǎng)橋是每個容器的默認網(wǎng)關(guān)。因為在同一宿主機內(nèi)的容器都接入同一個網(wǎng)橋, 這樣容器之間就能夠通過容器的 Container-IP 直接通信。
docker的4種網(wǎng)絡(luò)模式
| 網(wǎng)絡(luò)模式 | 配置 | 說明 |
|---|---|---|
| host | –network host | 容器和宿主機共享Network namespace |
| container | –network container:NAMEORID | 容器和另外一個容器共享Network namespace |
| none | –network none | 容器有獨立的Network namespace,但并沒有對其進行任何網(wǎng)絡(luò)設(shè)置,如分配veth pair和網(wǎng)橋連接,配置IP等 |
| bridge | –network | bridge默認模式 |


bridge 模式
當(dāng) Docker 進程啟動時, 會在主機上創(chuàng)建一個名為 docker0 的虛擬網(wǎng)橋, 此主機上啟動的 Docker 容器會連接到這個虛擬網(wǎng)橋上。虛擬網(wǎng)橋的工作方式和物理交換機類似, 這樣主機上的所有容器就通過交換機連在了一個二層網(wǎng)絡(luò)中。
從 docker0 子網(wǎng)中分配一個IP給容器使用, 并設(shè)置 docker0 的IP地址為容器的默認網(wǎng)關(guān)。在主機上創(chuàng)建一對虛擬網(wǎng)卡 veth pair設(shè)備,Docker將 veth pair 設(shè)備的一端放在新創(chuàng)建的容器中,并命名為 eth0(容器的網(wǎng)卡),另一端放在主機中,以 vethxxx 這樣類似的名字命名,并將這個網(wǎng)絡(luò)設(shè)備加入到 docker0 網(wǎng)橋中??梢酝ㄟ^ brctl show 命令查看。
bridge 模式是 docker的默認網(wǎng)絡(luò)模式,不寫 --network 參數(shù),就是 bridge 模式。使用 docker run -p 時,docker實際是在 iptables 做了 DNAT 規(guī)則,實現(xiàn)端口轉(zhuǎn)發(fā)功能??梢允褂?iptables -t nat -vnL 查看。
bridge 模式如下圖所示:

假設(shè)上圖的 Container2 中運行了一個nginx,大家來想幾個問題:
- 同主機間兩個容器間是否可以直接通信?比如在 Container1 上能不能直接訪問到 Container2 的 nginx 站點?
- 在宿主機上能否直接訪問到 Container2 的 nginx戰(zhàn)點?
- 在另一臺主機上如何訪問 node1 上的這個nginx站點呢? DNAT發(fā)布?
Docker網(wǎng)橋是宿主機虛擬出來的,并不是真實存在的網(wǎng)絡(luò)設(shè)備,外部網(wǎng)絡(luò)是無法尋址到的,這也意味著外部網(wǎng)絡(luò)無法通過直接通過 Container-IP訪問到容器。如果容器希望外部訪問能夠訪問到,可以通過映射容器端口到宿主主機(端口映射),即docker run 創(chuàng)建容器時候通過 -p 或 -P參數(shù)來啟用,訪問容器的時候就通過【宿主機 IP】:【容器端口】訪問容器。
container模式
這個模式指定新創(chuàng)建的容器和已經(jīng)存在的一個容器共享一個 Network Namespace,而不是和宿主機共享。新創(chuàng)建的容器不會創(chuàng)建自己的網(wǎng)卡、配置自己的IP,而是和一個指定的容器共享IP、端口范圍等。同樣,兩個容器除了網(wǎng)絡(luò)方面,其它的如文件系統(tǒng)、進程列表等還是隔離的。兩個容器的進程可以通過IO網(wǎng)卡設(shè)備通信。

host 模式
如果啟動容器的時候使用host模式,那么這個容器將不會獲得一個獨立的 Network Namespace,而是和宿主機共用一個 Network Namespace。容器將不會虛擬出自己的網(wǎng)卡,配置自己的IP等,而是使用宿主機的IP和端口。但是,容器的其它方面,如文件系統(tǒng)、進程列表等還是和宿主機隔離的。
使用host模式的容器可以直接使用宿主機的IP地址與外界通信,容器內(nèi)部的服務(wù)端口也可以使用宿主機的端口,不需要進行NAT,host最大的優(yōu)勢就是網(wǎng)絡(luò)性能比較好,但是 docker host上已經(jīng)使用的端口就不能再使用了,網(wǎng)絡(luò)的隔離性不好。
Host模式如下圖所示:

none 模式
使用none模式,Docker容器擁有自己的 Network Namespace,但是,并不為 Docker容器進行任何網(wǎng)絡(luò)配置。也就是說,這個Docker容器沒有網(wǎng)卡、IP、路由等信息。需要我們自己為Docker容器添加網(wǎng)卡、配置IP等。
這種網(wǎng)絡(luò)模式下 容器只有IO回環(huán)網(wǎng)絡(luò),沒有其它網(wǎng)卡。none模式可以再容器創(chuàng)建時通過 --network none來指定。這種類型的網(wǎng)絡(luò)沒有辦法聯(lián)網(wǎng),封閉的網(wǎng)絡(luò)能很好的保證容器的安全性。
應(yīng)用場景
- 啟動一個容器處理數(shù)據(jù),比如轉(zhuǎn)換數(shù)據(jù)格式
- 一些后臺的計算和處理任務(wù)
none 模式如下圖所示:

[root@Master ~]# docker network inspect bridge #查看bridge網(wǎng)絡(luò)的詳細配置
[
{
"Name": "bridge",
"Id": "78cf421b5c970fe40d93582e377187001a352e649ed7edd1e7fcd2f8fb28b8a8",
"Created": "2024-07-01T19:59:06.063808169+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": {
"1144c912e93254a920172d234311826e8a60f21b475032c6a19d78336597004a": {
"Name": "rancher-2.6.5",
"EndpointID": "b4ddc0d583862afa0da44e3f29f32456d965c6eeb8dbb9e3cfaa30bb3b78e10a",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
}
},
"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": {}
}
]
docker 容器網(wǎng)絡(luò)配置
Linux 內(nèi)核實現(xiàn)名稱空間的創(chuàng)建
ip netns 命令
可以借助 ip netns 命令來完成對 Network Namespace 的各種操作。ip netns 命令來自于 iproute 安裝包,一般系統(tǒng)會默認安裝,如果沒有的話,請自行安裝。
注意:ip netns 命令修改網(wǎng)絡(luò)配置時需要 sudo 權(quán)限。
可以通過 ip netns 命令完成對 Network Namespace 的相關(guān)操作,可以通過 ip netns help 查看命令幫助信息:
[root@Master ~]# ip netns help
Usage: ip netns list
ip netns add NAME
ip netns set NAME NETNSID
ip [-all] netns delete [NAME]
ip netns identify [PID]
ip netns pids NAME
ip [-all] netns exec [NAME] cmd ...
ip netns monitor
ip netns list-id
默認情況下,Linux系統(tǒng)中是沒有任何 Network Namespace的,所以 ip netns list 命令不會返回任何信息。
[root@Master ~]# ip netns list [root@Master ~]#
創(chuàng)建 Network Namespace
通過命令創(chuàng)建一個名為ns0的命名空間:
[root@Master ~]# ip netns list [root@Master ~]# ip netns add ns0 [root@Master ~]# ip netns list ns0
新創(chuàng)建的 Network Namespace 會出現(xiàn)在 /var/run/netns/目錄下。如果相同名字的 namespace已經(jīng)存在,命令會報 Cannot create namespace file “/var/run/netns/ns0”: File exists的錯誤。
[root@Master netns]# cd /var/run/netns/ [root@Master netns]# ll 總用量 0 -r--r--r-- 1 root root 0 7月 2 09:07 ns0 [root@Master netns]# ip netns add ns0 Cannot create namespace file "/var/run/netns/ns0": File exists [root@Master netns]#
對于每個 Network Namespace 來說,它會有自己獨立的網(wǎng)卡、路由表、ARP表、iptables等和網(wǎng)絡(luò)相關(guān)的資源。
操作 Network Namespace
ip 命令提供了 ip netns exec 子命令可以在對應(yīng)的 Network Namespace 中執(zhí)行命令。
查看新創(chuàng)建 Network Namespace 的網(wǎng)卡信息
[root@Master ~]# ip netns exec ns0 ip addr
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
可以看到,新創(chuàng)建的 Network Namespace 中會默認創(chuàng)建一個 lo 回環(huán)網(wǎng)卡,此時網(wǎng)卡處于關(guān)閉狀態(tài)。若嘗試ping 該 lo 回環(huán)網(wǎng)卡,會提示 Network unreachable
[root@Master ~]# ip netns exec ns0 ping 127.0.0.1 connect: 網(wǎng)絡(luò)不可達 127.0.0.1是默認回環(huán)網(wǎng)卡
通過下面的命令啟動 lo 回環(huán)網(wǎng)卡:
[root@Master ~]# ip netns exec ns0 ip link set lo up [root@Master ~]# ip netns exec ns0 ping 127.0.0.1 PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.467 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.033 ms 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.042 ms 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.042 ms 64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.042 ms 64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.035 ms ^C --- 127.0.0.1 ping statistics --- 6 packets transmitted, 6 received, 0% packet loss, time 5004ms rtt min/avg/max/mdev = 0.033/0.110/0.467/0.159 ms
轉(zhuǎn)移設(shè)備
我們可以在不同的 Network Namespace 之間轉(zhuǎn)移設(shè)備(如 veth)。由于一個設(shè)備只能屬于一個 Network Namespace,所以轉(zhuǎn)移后在這個 Network Namespace 內(nèi)就看不到這個設(shè)備了。
其中,veth 設(shè)備屬于可轉(zhuǎn)移設(shè)備,而很多其它設(shè)備(如:lo、vxlan、ppp、bridge等)是不可以轉(zhuǎn)移的。
veth pair
veth pair 全稱是 Virtual Ethernet Pair,是一個成對的端口,所有從這對端口一端進入的數(shù)據(jù)包都將從另一端出來,反之也是一樣。
引入 veth pair 是為了在不同的 Network Namespace 直接進行通信,利用它可以直接將兩個 Network Namespace 連接起來。

創(chuàng)建 veth pair
[root@Master ~]# ip link add type veth
[root@Master ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:6e:81:b7 brd ff:ff:ff:ff:ff:ff
inet 192.168.75.100/24 brd 192.168.75.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::11a5:f410:75e5:dc1f/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:2a:a0:38:5a 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:2aff:fea0:385a/64 scope link
valid_lft forever preferred_lft forever
4: docker_gwbridge: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:f7:0c:15:a6 brd ff:ff:ff:ff:ff:ff
inet 172.18.0.1/16 brd 172.18.255.255 scope global docker_gwbridge
valid_lft forever preferred_lft forever
8: veth780a161@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
link/ether 3e:6b:9c:85:f4:b0 brd ff:ff:ff:ff:ff:ff link-netnsid 1
inet6 fe80::3c6b:9cff:fe85:f4b0/64 scope link
valid_lft forever preferred_lft forever
9: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 02:ba:c5:28:3b:cb brd ff:ff:ff:ff:ff:ff
10: veth1@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 52:8a:6f:41:d6:aa brd ff:ff:ff:ff:ff:ff可以看到,此時系統(tǒng)中新增了一對 veth pair,將 veth0 和 veth1 兩個虛擬網(wǎng)卡連接起來,此時這對 veth pair 處于 “未啟動” 狀態(tài)。
實現(xiàn) Network Namespace 間通信
下面我們利用 veth pair 實現(xiàn)兩個不同的 Network Namespace 之間的通信。剛才我們已經(jīng)創(chuàng)建了一個名為ns0的 Network Namespace,下面再創(chuàng)建一個 Network Namespace,命名為ns1
[root@Master ~]# ip netns add ns1 [root@Master ~]# ip netns list ns1 ns0
然后我們將 veth0 加入到 ns0,將 veth1 加入到 ns1
[root@Master ~]# ip link set veth0 netns ns0 [root@Master ~]# ip link set veth1 netns ns1
然后我們分別為這對 veth pair 配置上 ip 地址,并啟用它們
[root@Master ~]# ip netns exec ns0 ip link set veth0 up [root@Master ~]# ip netns exec ns0 ip addr add 192.0.0.1/24 dev veth0 [root@Master ~]# ip netns exec ns1 ip link set veth1 up [root@Master ~]# ip netns exec ns1 ip addr add 192.0.0.2/24 dev veth1
查看這對 veth pair 的狀態(tài)
[root@Master ~]# ip netns exec ns1 ip a
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
10: veth1@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 52:8a:6f:41:d6:aa brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 192.0.0.2/24 scope global veth1
valid_lft forever preferred_lft forever
inet6 fe80::508a:6fff:fe41:d6aa/64 scope link
valid_lft forever preferred_lft forever
從上面可以看出,我們已經(jīng)成功啟用了這個 veth pair,并為每個 veth 設(shè)備分配了對應(yīng)的 ip 地址。我們嘗試在 ns1 中訪問 ns0 中的 ip 地址
[root@Master ~]# ip netns exec ns1 ping 192.0.0.1 PING 192.0.0.1 (192.0.0.1) 56(84) bytes of data. 64 bytes from 192.0.0.1: icmp_seq=1 ttl=64 time=0.726 ms 64 bytes from 192.0.0.1: icmp_seq=2 ttl=64 time=0.109 ms 64 bytes from 192.0.0.1: icmp_seq=3 ttl=64 time=0.037 ms 64 bytes from 192.0.0.1: icmp_seq=4 ttl=64 time=0.118 ms 64 bytes from 192.0.0.1: icmp_seq=5 ttl=64 time=0.262 ms 64 bytes from 192.0.0.1: icmp_seq=6 ttl=64 time=0.064 ms ^C --- 192.0.0.1 ping statistics --- 6 packets transmitted, 6 received, 0% packet loss, time 5001ms rtt min/avg/max/mdev = 0.037/0.219/0.726/0.237 ms [root@Master ~]# ip netns exec ns0 ping 192.0.0.2 PING 192.0.0.2 (192.0.0.2) 56(84) bytes of data. 64 bytes from 192.0.0.2: icmp_seq=1 ttl=64 time=0.378 ms 64 bytes from 192.0.0.2: icmp_seq=2 ttl=64 time=0.090 ms 64 bytes from 192.0.0.2: icmp_seq=3 ttl=64 time=0.131 ms 64 bytes from 192.0.0.2: icmp_seq=4 ttl=64 time=0.140 ms 64 bytes from 192.0.0.2: icmp_seq=5 ttl=64 time=0.138 ms 64 bytes from 192.0.0.2: icmp_seq=6 ttl=64 time=0.130 ms ^C --- 192.0.0.2 ping statistics --- 6 packets transmitted, 6 received, 0% packet loss, time 5001ms rtt min/avg/max/mdev = 0.090/0.167/0.378/0.096 ms
可以看到,veth pair 成功實現(xiàn)了兩個不同 Network Namespace 之間的網(wǎng)絡(luò)交互。
四種網(wǎng)絡(luò)模式配置
bridge 模式配置
[root@Master ~]# docker run -it --name ti --rm busybox
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
5cc84ad355aa: Pull complete
Digest: sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
Status: Downloaded newer image for busybox:latest
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:03
inet addr:172.17.0.3 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:656 (656.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
在創(chuàng)建容器時添加 --network bridge 與不加 --network選項效果是一致的
[root@Master ~]# docker run -it --name ti --network bridge --rm busybox
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:03
inet addr:172.17.0.3 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:6 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:516 (516.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
/ # none 模式配置
[root@Master ~]# docker run -it --name t1 --network none --rm busybox
/ # ifconfig
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
/ # container 模式配置
啟動第一個容器
[root@Master ~]# docker run -dit --name b3 busybox
7af0fc0bafe31a7195e1a3a5c194f5cd1d38700fe8d9369f4070c4f683070c1d
[root@Master ~]# docker exec -it b3 /bin/sh
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:03
inet addr:172.17.0.3 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:656 (656.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
/ #
啟動第二個容器
[root@Master ~]# docker run -it --name b2 --rm busybox
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:04
inet addr:172.17.0.4 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:656 (656.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
/ # 可以看到名為 b2 的容器 IP地址是172.17.0.4 ,與第一個容器的IP地址不是一樣的,也就是說并沒有共享網(wǎng)絡(luò),此時如果我們將第二個容器的啟動方式改變一下,就可以使名為b2的容器IP與b3容器IP一致,也即共享IP,但不共享文件系統(tǒng)。
[root@Master ~]# docker run -it --name b2 --rm --network container:b3 busybox
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:03
inet addr:172.17.0.3 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:656 (656.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
/ #
此時我們在b3容器上創(chuàng)建一個目錄
[root@Master ~]# docker exec -it b3 /bin/sh / # mkdir /tmp/data / # ls /tmp/ data / #
到b2容器上檢查/tmp目錄會發(fā)現(xiàn)并沒有這個目錄,因為文件系統(tǒng)是處于隔離狀態(tài),僅僅是共享了網(wǎng)絡(luò)而已。
[root@Master ~]# docker run -it --name b2 --rm --network container:b3 busybox / # ls /tmp/ / # / #
在b2容器上部署一個站點
[root@Master ~]# docker run -it --name b2 --rm --network container:b3 busybox / # ls /tmp/ / # / # echo 'hello world' > /tmp/index.html / # ls /tmp/ index.html / # httpd -h /tmp / # netstat -antl Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 :::80 :::* LISTEN / #
在 b3 容器上用本地地址去訪問此站點
/ # wget -O - -q 172.17.0.3:80 hello world / #
host 模式配置
啟動容器時直接指明模式為host
[root@Master ~]# docker run -it --name b2 --rm --network host busybox
/ # ifconfig
docker0 Link encap:Ethernet HWaddr 02:42:00:67:A1:74
inet addr:172.17.0.1 Bcast:172.17.255.255 Mask:255.255.0.0
inet6 addr: fe80::42:ff:fe67:a174/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:33140 errors:0 dropped:0 overruns:0 frame:0
TX packets:37022 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:5200713 (4.9 MiB) TX bytes:55906242 (53.3 MiB)
docker_gwbridge Link encap:Ethernet HWaddr 02:42:E9:16:D2:A1
inet addr:172.18.0.1 Bcast:172.18.255.255 Mask:255.255.0.0
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
ens33 Link encap:Ethernet HWaddr 00:0C:29:6E:81:B7
inet addr:192.168.75.100 Bcast:192.168.75.255 Mask:255.255.255.0
inet6 addr: fe80::11a5:f410:75e5:dc1f/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:184930 errors:0 dropped:0 overruns:0 frame:0
TX packets:229473 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:66831055 (63.7 MiB) TX bytes:54247615 (51.7 MiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
veth500b5c6 Link encap:Ethernet HWaddr BA:D6:E4:4F:B2:13
inet6 addr: fe80::b8d6:e4ff:fe4f:b213/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:33140 errors:0 dropped:0 overruns:0 frame:0
TX packets:37030 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:5664673 (5.4 MiB) TX bytes:55906898 (53.3 MiB)
vethf7d0281 Link encap:Ethernet HWaddr 9E:D9:64:0B:01:A4
inet6 addr: fe80::9cd9:64ff:fe0b:1a4/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:656 (656.0 B)
/ #
此時如果我們在這個容器中啟動一個http站點,我們就可以直接用宿主機的IP直接在瀏覽器中訪問這個容器中的站點了。
容器的常用操作
查看容器的主機名
[root@Master ~]# docker run -it --name t1 --network bridge --rm busybox / # hostname beff0ad654d4 / #
在容器啟動時注入主機名
[root@Master ~]# docker run -it --name t1 --network bridge --hostname ljl --rm busybox / # hostname ljl / # cat /etc/hosts 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 172.17.0.4 ljl / # cat /etc/resolv.conf # Generated by NetworkManager nameserver 223.5.5.5 / # ping www.baidu.com PING www.baidu.com (39.156.66.18): 56 data bytes 64 bytes from 39.156.66.18: seq=0 ttl=127 time=10.446 ms 64 bytes from 39.156.66.18: seq=1 ttl=127 time=18.426 ms 64 bytes from 39.156.66.18: seq=2 ttl=127 time=13.849 ms 64 bytes from 39.156.66.18: seq=3 ttl=127 time=11.226 ms 64 bytes from 39.156.66.18: seq=4 ttl=127 time=9.571 ms ^C --- www.baidu.com ping statistics --- 5 packets transmitted, 5 packets received, 0% packet loss round-trip min/avg/max = 9.571/12.703/18.426 ms / #
手動指定容器要使用的DNS
[root@Master ~]# docker run -it --name t3 --network bridge --hostname ljl --dns 114.114.114.114 --rm busybox / # cat /etc/resolv.conf nameserver 114.114.114.114 / # nslookup -type=a www.baidu.com Server: 114.114.114.114 Address: 114.114.114.114:53 Non-authoritative answer: Name: www.baidu.com Address: 39.156.66.14 Name: www.baidu.com Address: 39.156.66.18 / #
手動往 /etc/hosts 文件中注入主機名到IP地址的映射
[root@Master ~]# docker run -it --name t1 --network bridge --hostname ljl --add-host www.a.com:1.1.1.1 --rm busybox / # cat /etc/hosts 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 1.1.1.1 www.a.com 172.17.0.3 ljl / #
開放容器端口
執(zhí)行 docker run 的時候有個 -p 選項,可以將容器中的應(yīng)用端口映射到宿主機中,從而實現(xiàn)讓外部主機可以通過訪問宿主機的某端口來訪問容器內(nèi)應(yīng)用的目的。
-p 選項能夠使用多次,其所能夠暴露的端口必須是容器確實在監(jiān)聽的端口。
-p 選項的使用格式:
-p containerPort
將指定的容器端口映射至主機所有地址的一個動態(tài)端口
- -p hostPort:containerPort
- 將容器端口 containerPort 映射至指定的主機端口 hostPort
- -p ip ;containerPort
- 將指定的容器端口 containerPort 映射至主機指定ip的動態(tài)端口
- -p ip :hostPort:containerPort
- 將指定的容器端口 containerPort 映射至主機指定ip的端口 hostPort
動態(tài)端口指定的是隨機端口,具體的映射結(jié)果可以使用 docker port 命令查看。
[root@Master ~]# docker run -dit --name web1 -p 192.168.75.100::80 httpd Unable to find image 'httpd:latest' locally latest: Pulling from library/httpd a2abf6c4d29d: Pull complete dcc4698797c8: Pull complete 41c22baa66ec: Pull complete 67283bbdd4a0: Pull complete d982c879c57e: Pull complete Digest: sha256:0954cc1af252d824860b2c5dc0a10720af2b7a3d3435581ca788dff8480c7b32 Status: Downloaded newer image for httpd:latest ba9fa833a86cfd08d8d6456bada7da971b921caf856047af4dc8ee7e4ac8af34 [root@Master ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ba9fa833a86c httpd "httpd-foreground" 7 seconds ago Up 6 seconds 192.168.75.100:32768->80/tcp web1 1942273f5b57 busybox "sh" About an hour ago Up About an hour t1 1144c912e932 rancher/rancher:v2.6.5 "entrypoint.sh" 6 months ago Up 22 hours 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp rancher-2.6.5 [root@Master ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 192.168.75.100:32768 *:* LISTEN 0 128 *:111 *:* LISTEN 0 128 [::]:80 [::]:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 100 [::1]:25 [::]:* LISTEN 0 128 [::]:443 [::]:* LISTEN 0 128 [::]:111 [::]:* [root@Master ~]#
以上命令執(zhí)行會一直占用著前端,我們新開一個終端連接連接來看一下容器的80端口被映射到了宿主機的什么端口上
[root@Master ~]# docker port web1 80/tcp -> 192.168.75.100:32768
由此可見,容器的80端口被暴露到了宿主機的 32768 端口上,此時我們在宿主機上訪問一下這個端口看是否能訪問到容器內(nèi)的站點。
[root@Master ~]# curl http://192.168.75.100:32768 <html><body><h1>It works!</h1></body></html>
iptables防火墻規(guī)則將隨容器的創(chuàng)建自動生成,隨容器的刪除自動刪除規(guī)則。
[root@Master ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 252 packets, 16128 bytes)
pkts bytes target prot opt in out source destination
10 520 DOCKER all -- * * 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 37 packets, 2803 bytes)
pkts bytes target prot opt in out source destination
1 60 DOCKER all -- * * 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL
Chain POSTROUTING (policy ACCEPT 38 packets, 2863 bytes)
pkts bytes target prot opt in out source destination
12192 780K MASQUERADE all -- * !docker0 172.17.0.0/16 0.0.0.0/0
0 0 MASQUERADE all -- * !docker_gwbridge 172.18.0.0/16 0.0.0.0/0
0 0 MASQUERADE tcp -- * * 172.17.0.2 172.17.0.2 tcp dpt:443
0 0 MASQUERADE tcp -- * * 172.17.0.2 172.17.0.2 tcp dpt:80
0 0 MASQUERADE tcp -- * * 172.17.0.4 172.17.0.4 tcp dpt:80
Chain DOCKER (2 references)
pkts bytes target prot opt in out source destination
0 0 RETURN all -- docker0 * 0.0.0.0/0 0.0.0.0/0
0 0 RETURN all -- docker_gwbridge * 0.0.0.0/0 0.0.0.0/0
0 0 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:443 to:172.17.0.2:443
0 0 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 to:172.17.0.2:80
1 60 DNAT tcp -- !docker0 * 0.0.0.0/0 192.168.75.100 tcp dpt:32768 to:172.17.0.4:80
[root@Master ~]# 將容器端口映射到指定IP的隨機端口
[root@Master ~]# docker run -dit --name web1 -p 192.168.75.100::80 httpd 201c8aac8bb57a3f111965fdc112385b87e64f22531489d8d7b3631279aeb822 [root@Master ~]#
查看端口映射情況
[root@Master ~]# docker port web1 80/tcp -> 192.168.75.100:32769 [root@Master ~]#
自定義 docker0 網(wǎng)橋的網(wǎng)絡(luò)屬性信息
自定義 docker0 網(wǎng)橋的網(wǎng)絡(luò)屬性信息需要修改 /etc/docker/daemon.json 配置文件
[root@Master ~]# cd /etc/docker
[root@Master docker]# cat daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"],
"registry-mirrors": ["https://registry.docker-cn.com","https://s3d6l2fh.mirror.aliyuncs.com"],
"bip": "192.168.1.5/24"
}
[root@Master docker]# systemctl daemon-reload
[root@Master docker]# systemctl restart docker
[root@Master docker]#
[root@Master docker]# vim /lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375
[root@Master ~]# systemctl daemon-reload
[root@Master ~]# systemctl restart docker在客戶端上像 dockerd 直接傳遞 "-H|–host"選項指定要控制哪臺主機上的 docker 容器
[root@Master docker]# docker -H 192.168.75.100:2375 ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1144c912e932 rancher/rancher:v2.6.5 "entrypoint.sh" 6 months ago Up 9 seconds 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp rancher-2.6.5
創(chuàng)建新網(wǎng)絡(luò)
[root@Master docker]# docker network create ljl -d bridge 05e8748ed4b1ee29a743c9be2799a735685cbd7d8e03e64bcd95df7eb441016f [root@Master docker]# docker network ls NETWORK ID NAME DRIVER SCOPE b7d9764c841e bridge bridge local cffaf2d0d5bb docker_gwbridge bridge local 13346f3bc032 host host local 05e8748ed4b1 ljl bridge local c41bdf0688fe none null local [root@Master docker]#
創(chuàng)建一個額外的自定義橋,區(qū)別于 docker0
[root@Master docker]# docker network create -d bridge --subnet "192.168.2.0/24" --gateway "192.168.2.1" br0 9565553236d3467668fce59761244051b5f53a409f9e9ce9722baaf08b99ff3d [root@Master docker]# docker network ls NETWORK ID NAME DRIVER SCOPE 9565553236d3 br0 bridge local b7d9764c841e bridge bridge local cffaf2d0d5bb docker_gwbridge bridge local 13346f3bc032 host host local 05e8748ed4b1 ljl bridge local c41bdf0688fe none null local [root@Master docker]#
使用新創(chuàng)建的自定義橋來創(chuàng)建容器
[root@Master docker]# docker run -it --name b1 --network br0 busybox
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:C0:A8:02:02
inet addr:192.168.2.2 Bcast:192.168.2.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:12 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1032 (1.0 KiB) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
/ #
再創(chuàng)建一個容器,使用默認的bridge橋
[root@Master ~]# docker run --name b2 -it busybox
/ # ls
bin dev etc home proc root sys tmp usr var
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:C0:A8:01:02
inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:7 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:586 (586.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
/ # 總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
docker+gitlab+gitlab-runner部署詳解
這篇文章主要介紹了docker+gitlab+gitlab-runner部署詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
2023年windows?DockerDeskTop最新款4.18.0?全程保姆級安裝圖文教程
這篇文章主要介紹了2023年windows?DockerDeskTop最新款4.18.0?全程保姆級安裝圖文教程,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
docker容器中cron任務(wù)未執(zhí)行的原因分析及解決
在Ubuntu?22.04鏡像中cron任務(wù)執(zhí)行失敗,可能與pam權(quán)限或文件權(quán)限設(shè)置有關(guān),解決方法包括動態(tài)加載任務(wù)至crontab及將文件權(quán)限設(shè)為600(容器默認umask=0022),后者因權(quán)限沖突導(dǎo)致執(zhí)行失敗,推薦此方案2025-08-08
使用Docker compose編排Laravel應(yīng)用的方法
本篇文章主要介紹了使用Docker compose編排Laravel應(yīng)用的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
docker容器通信參數(shù)使用及l(fā)ink參數(shù)介紹
這篇文章主要介紹了docker容器通信參數(shù)使用及l(fā)ink參數(shù)介紹,link的原理就是在/etc/hosts里面添加了一個alias的名稱,本文通過案例分析給大家詳細講解,需要的朋友可以參考下2022-09-09

