首頁 > 軟體

docker網路設定過程詳解介紹

2021-12-08 19:01:20

Linux核心實現名稱空間的建立

ip netns命令

可以藉助ip netns命令來完成對 Network Namespace 的各種操作。ip netns命令來自於iproute安裝包,一般系統會預設安裝,如果沒有的話,請自行安裝。

注意:ip netns命令修改網路設定時需要 sudo 許可權。

可以通過ip netns命令完成對Network Namespace 的相關操作,可以通過ip netns help檢視命令幫助資訊:

[[email protected] ~]# ip netns help
Usage:  ip netns list
        ip netns add NAME
        ip netns attach NAME PID
        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 [target-nsid POSITIVE-INT] [nsid POSITIVE-INT]
NETNSID := auto | POSITIVE-INT
[[email protected] ~]# ip netns list
[[email protected] ~]# ip netns add ns0
[[email protected] ~]# ip netns list
ns0
[[email protected] ~]# ls /var/run/netns
ns0
[[email protected] ~]# ip netns add ns0
Cannot create namespace file "/var/run/netns/ns0": File exists
[[email protected] ~]# touch /var/run/netns/ns1
[[email protected] ~]# ip netns list
Error: Peer netns reference is invalid.
Error: Peer netns reference is invalid.
ns1
ns0
[[email protected] ~]# ip netns del ns1
[[email protected] ~]# ip netns list
ns0

操作Network Namespace

ip命令提供了ip netns exec子命令可以在對應的 Network Namespace 中執行命令。

檢視新建立 Network Namespace 的網路卡資訊

[[email protected] ~]# 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
可以看到,新建立的Network Namespace中會預設建立一個lo迴環網路卡,此時網路卡處於關閉狀態。此時,嘗試去 ping 該lo迴環網路卡,會提示Network is unreachable
[[email protected] ~]# ip netns exec ns0 ping 127.0.0.1
connect: 網路不可達
通過下面的命令啟用lo迴環網路卡
[[email protected] ~]# ip netns exec ns0 ip link set lo up
[[email protected] ~]# 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.024 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.019 ms

轉移裝置

我們可以在不同的 Network Namespace 之間轉移裝置(如veth)。由於一個裝置只能屬於一個 Network Namespace ,所以轉移後在這個 Network Namespace 內就看不到這個裝置了。

其中,veth裝置屬於可轉移裝置,而很多其它裝置(如lo、vxlan、ppp、bridge等)是不可以轉移的。

veth pair

veth pair 全稱是 Virtual Ethernet Pair,是一個成對的埠,所有從這對埠一 端進入的封包都將從另一端出來,反之也是一樣。

引入veth pair是為了在不同的 Network Namespace 直接進行通訊,利用它可以直接將兩個 Network Namespace 連線起來。

建立veth pair

[[email protected] ~]# 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 fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:d7:ce:07 brd ff:ff:ff:ff:ff:ff
    inet 192.168.197.131/24 brd 192.168.197.255 scope global dynamic noprefixroute ens33
       valid_lft 1522sec preferred_lft 1522sec
    inet6 fe80::1f2f:a9f9:78a7:7cc/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:f1:42:78 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:f1:42:78 brd ff:ff:ff:ff:ff:ff
5: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:b8:38:aa:7d 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:b8ff:fe38:aa7d/64 scope link 
       valid_lft forever preferred_lft forever
7: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether ca:56:6c:07:de:af brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::c856:6cff:fe07:deaf/64 scope link 
       valid_lft forever preferred_lft forever
[[email protected] ~]# ip link add type veth
[[email protected] ~]# 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 fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:d7:ce:07 brd ff:ff:ff:ff:ff:ff
    inet 192.168.197.131/24 brd 192.168.197.255 scope global dynamic noprefixroute ens33
       valid_lft 1499sec preferred_lft 1499sec
    inet6 fe80::1f2f:a9f9:78a7:7cc/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:f1:42:78 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:f1:42:78 brd ff:ff:ff:ff:ff:ff
5: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:b8:38:aa:7d 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:b8ff:fe38:aa7d/64 scope link 
       valid_lft forever preferred_lft forever
7: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether ca:56:6c:07:de:af brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::c856:6cff:fe07:deaf/64 scope link 
       valid_lft forever preferred_lft forever
10: [email protected]: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether a2:0f:68:39:2c:fd brd ff:ff:ff:ff:ff:ff
11: [email protected]: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether e6:e9:55:96:60:35 brd ff:ff:ff:ff:ff:ff

可以看到,此時系統中新增了一對veth pair,將veth0和veth1兩個虛擬網路卡連線了起來,此時這對 veth pair 處於」未啟用「狀態。

實現Network Namespace間通訊

[[email protected] ~]#  ip netns list
ns0
[[email protected] ~]# ip netns add ns1
[[email protected] ~]# ip link set veth0 netns ns0
[[email protected] ~]# ip link set veth1 netns ns1
[[email protected] ~]# ip netns exec ns0 ip link set veth0 up
[[email protected] ~]# ip netns exec ns0 ip addr add 192.168.2.1/24 dev veth0
[[email protected] ~]# ip netns exec ns1 ip link set veth1 up
[[email protected] ~]# ip netns exec ns1  ip addr add 192.168.2.2/24 dev veth1
[[email protected] ~]# ip netns exec ns0 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
10: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether a2:0f:68:39:2c:fd brd ff:ff:ff:ff:ff:ff link-netns ns1
    inet 192.168.2.1/24 scope global veth0
       valid_lft forever preferred_lft forever
    inet6 fe80::a00f:68ff:fe39:2cfd/64 scope link 
       valid_lft forever preferred_lft forever
[[email protected] ~]# 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
11: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether e6:e9:55:96:60:35 brd ff:ff:ff:ff:ff:ff link-netns ns0
    inet 192.168.2.2/24 scope global veth1
       valid_lft forever preferred_lft forever
    inet6 fe80::e4e9:55ff:fe96:6035/64 scope link 
       valid_lft forever preferred_lft forever
[[email protected] ~]# ip netns exec ns1 ping 192.168.2.1
PING 192.168.2.1 (192.168.2.1) 56(84) bytes of data.
64 bytes from 192.168.2.1: icmp_seq=1 ttl=64 time=0.037 ms
64 bytes from 192.168.2.1: icmp_seq=2 ttl=64 time=0.028 ms

可以看到,veth pair成功實現了兩個不同Network Namespace之間的網路互動。

veth裝置重新命名

[[email protected] ~]# ip netns exec ns0 ip link set veth0 down
[[email protected] ~]# ip netns exec ns0 ip link set veth0 down
[[email protected] ~]# ip netns exec ns0 ip link set veth0 down
[[email protected] ~]# ip netns exec ns0 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
10: [email protected]: <BROADCAST,MULTICAST> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether a2:0f:68:39:2c:fd brd ff:ff:ff:ff:ff:ff link-netns ns1
    inet 192.168.2.1/24 scope global veth0
       valid_lft forever preferred_lft forever
[[email protected] ~]# ip netns exec ns1 ip link set veth1 down 
[[email protected] ~]# ip netns exec ns1 ip link  set dev veth1 name eth0
[[email protected] ~]# ip netns exec ns1 ip link set eth0
[[email protected] ~]# 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
11: [email protected]: <BROADCAST,MULTICAST> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether e6:e9:55:96:60:35 brd ff:ff:ff:ff:ff:ff link-netns ns0
    inet 192.168.2.2/24 scope global eth0
       valid_lft forever preferred_lft forever

四種網路模式設定

bridge模式

[[email protected] ~]# docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
3aab638df1a9: Pull complete 
Digest: sha256:52817dece4cfe26f581c834d27a8e1bcc82194f914afe6d50afad5a101234ef1
Status: Downloaded newer image for busybox:latest
docker.io/library/busybox:latest
[[email protected] ~]# docker run -it --name b1 --rm busybox # --rm 刪除或停止容器後自動銷燬容器
/ # 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
12: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
    link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
/ # exit
[[email protected] ~]# docker run -it --name b1 --network bridge --rm busybox
/ # 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
14: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
    link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
/ # exit

none模式

[[email protected] ~]# docker run -it --name b2 --rm --network none busybox
/ # 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
使用none模式,Docker容器擁有自己的Network Namespace,但是,並不為Docker容器進行任何網路設定。也就是說,這個Docker容器沒有網路卡、IP、路由等資訊。需要我們自己為Docker容器新增網路卡、設定IP等。

container模式

[[email protected] ~]# docker run -it --name b3 --rm busybox
/ #  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
16: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
    link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
新開一個終端
[[email protected] ~]# docker run -it --name b4 --rm --network container:b3 busybox
/ # 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
16: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
    link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
 在b3上建立一個檔案
/ # ls
bin   dev   etc   home  proc  root  sys   tmp   usr   var
/ # mkdir yy
在b4上檢視
/ # ls
bin   dev   etc   home  proc  root  sys   tmp   usr   var
在b3上建立網站
/ # echo "aaa." > bbb/index.html
/ # httpd -h bbb/
/ # netstat -antl
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 :::80                   :::*                    LISTE

# 在b4上存取
/ # wget -qO - 172.17.0.2
aaa
/ #
# 由此可見,container模式下的容器間關係就相當於一臺主機上的兩個不同程序

這個模式指定新建立的容器和已經存在的一個容器共用一個 Network Namespace,而不是和宿主機共用。新建立的容器不會建立自己的網路卡,設定自己的 IP,而是和一個指定的容器共用 IP、埠範圍等。同樣,兩個容器除了網路方面,其他的如檔案系統、程序列表等還是隔離的。兩個容器的程序可以通過 lo 網路卡裝置通訊。

host模式
啟動容器時直接指明模式為host

[[email protected] ~]# docker run -it --name b5 --rm --network host busybox
/ # 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel qlen 1000
    link/ether 00:0c:29:d7:ce:07 brd ff:ff:ff:ff:ff:ff
    inet 192.168.197.131/24 brd 192.168.197.255 scope global dynamic noprefixroute ens33
       valid_lft 1741sec preferred_lft 1741sec
    inet6 fe80::1f2f:a9f9:78a7:7cc/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue qlen 1000
    link/ether 52:54:00:f1:42:78 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 qlen 1000
    link/ether 52:54:00:f1:42:78 brd ff:ff:ff:ff:ff:ff
5: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue 
    link/ether 02:42:b8:38:aa:7d 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:b8ff:fe38:aa7d/64 scope link 
       valid_lft forever preferred_lft forever
7: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue master docker0 
    link/ether ca:56:6c:07:de:af brd ff:ff:ff:ff:ff:ff
    inet6 fe80::c856:6cff:fe07:deaf/64 scope link 
       valid_lft forever preferred_lft forever
/ # mkdir www
/ # echo "ccc" > www/index.html
/ # httpd -h www/
/ # 
[[email protected] ~]# 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 fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:d7:ce:07 brd ff:ff:ff:ff:ff:ff
    inet 192.168.197.131/24 brd 192.168.197.255 scope global dynamic noprefixroute ens33
       valid_lft 1730sec preferred_lft 1730sec
    inet6 fe80::1f2f:a9f9:78a7:7cc/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:f1:42:78 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:f1:42:78 brd ff:ff:ff:ff:ff:ff
5: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:b8:38:aa:7d 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:b8ff:fe38:aa7d/64 scope link 
       valid_lft forever preferred_lft forever
7: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether ca:56:6c:07:de:af brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::c856:6cff:fe07:deaf/64 scope link 
       valid_lft forever preferred_lft forever
[[email protected] ~]# curl 192.168.197.131
ccc

容器的常用操作

[[email protected] ~]# docker run -it --name b6 --rm busybox
/ # hostname
ef156ee78b70
/ # exit
[[email protected] ~]# docker run -it --name b6 --hostname yanghaixx busybox
/ # hostname 
yanghaixx
/ # 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.3      yanghaixx
/ # cat /etc/resolv.conf 
# Generated by NetworkManager
search localdomain
nameserver 192.168.197.2
/ # ping baidu.com
PING baidu.com (220.181.38.148): 56 data bytes
64 bytes from 220.181.38.148: seq=0 ttl=127 time=25.224 ms
64 bytes from 220.181.38.148: seq=1 ttl=127 time=24.795 ms
[[email protected] ~]# docker run -it --rm  --name b7 --dns 8.8.8.8 --hostname yanghaixx busybox
/ # cat /etc/resolv.conf 
search localdomain
nameserver 8.8.8.8

/ # exit .
[[email protected] ~]# docker run -it --name b7 --rm --hostname yanghaixx --add-host baidu.com:0.0.0.0  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
0.0.0.0 baidu.com
172.17.0.3      yanghaixx

埠對映
執行docker run的時候有個-p選項,可以將容器中的應用埠對映到宿主機中,從而實現讓外部主機可以通過存取宿主機的某埠來存取容器內應用的目的。

-p選項能夠使用多次,其所能夠暴露的埠必須是容器確實在監聽的埠。

-p選項的使用格式:

-p
將指定的容器埠對映至主機所有地址的一個動態埠
動態埠指的是隨機埠,具體的對映結果可使用docker port命令檢視。
[[email protected] ~]# docker run -d --name web1 --rm -p 80 yanghaixx/nginx:v2
62029a5977a06e07ab5b40c78ce60e9cc519364512bc2e6d5785afd7dc1e090f
[[email protected] ~]#  docker port web1
80/tcp -> 0.0.0.0:49153
80/tcp -> :::49153
[[email protected] ~]# curl 192.168.197.131:49153
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

將容器埠對映到宿主機的指定埠

[[email protected] ~]# docker run -itd --name web2 --rm -p 2525:80 yanghaixx/nginx:v2
62a93a8ef40eec981b2d1682da92523027999991b0294782be38d4565b210ff8
[[email protected] ~]# docker port web2
80/tcp -> 0.0.0.0:2525
80/tcp -> :::2525
[[email protected] ~]# curl 192.168.197.131:2525
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

自定義docker0橋的網路屬性資訊

自定義docker0橋的網路屬性資訊需要修改/etc/docker/daemon.json組態檔

[[email protected] ~]# vim /etc/docker/daemon.json
[[email protected] ~]# cat /etc/docker/daemon.json
{
        "registry-mirrors": ["https://q9u587gb.mirror.aliyuncs.com"]
        "bip": "192.168.2.1/24"
}
See "systemctl status docker.service" and "journalctl -xe" for details.
[[email protected] ~]# 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 fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:d7:ce:07 brd ff:ff:ff:ff:ff:ff
    inet 192.168.197.131/24 brd 192.168.197.255 scope global dynamic noprefixroute ens33
       valid_lft 1690sec preferred_lft 1690sec
    inet6 fe80::1f2f:a9f9:78a7:7cc/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:f1:42:78 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:f1:42:78 brd ff:ff:ff:ff:ff:ff
5: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:97:2a:95:42 brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.1/24 brd 192.168.2.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:97ff:fe2a:9542/64 scope link 
       valid_lft forever preferred_lft forever

docker建立自定義橋

[[email protected] ~]# vim /etc/docker/daemon.json
[[email protected] ~]# cat /etc/docker/daemon.json
{
        "registry-mirrors": ["https://q9u587gb.mirror.aliyuncs.com"]
        "bip": "192.168.2.1/24"
}
See "systemctl status docker.service" and "journalctl -xe" for details.
[[email protected] ~]# 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 fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:d7:ce:07 brd ff:ff:ff:ff:ff:ff
    inet 192.168.197.131/24 brd 192.168.197.255 scope global dynamic noprefixroute ens33
       valid_lft 1690sec preferred_lft 1690sec
    inet6 fe80::1f2f:a9f9:78a7:7cc/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:f1:42:78 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:f1:42:78 brd ff:ff:ff:ff:ff:ff
5: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:97:2a:95:42 brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.1/24 brd 192.168.2.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:97ff:fe2a:9542/64 scope link 
       valid_lft forever preferred_lft forever

使用新建立的自定義橋來建立容器:

[[email protected] ~]# docker run -itd --name web --network br0 --rm yanghaixx/nginx:v2
aec728f703567b36016cf09cde1ffd1036673d7c2d961cae0b81cd09721d5cb9
[[email protected] ~]# docker exec -it web /bin/bash
[[email protected] /]# 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
31: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:01:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.1.2/24 brd 172.17.1.255 scope global eth0
       valid_lft forever preferred_lft forever

再建立一個容器,使用預設的bridge橋:

[[email protected] ~]# docker run -itd --name web02 --rm luojiatian1904/nginx:v2
4ee412be6d20b96a18f0b6fc0062beefc2192d906b212134b362ec41be5144ff
[[email protected] ~]# docker exec -it web02 /bin/bash
[[email protected] /]# 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
33: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:02:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.2.2/24 brd 192.168.2.255 scope global eth0
       valid_lft forever preferred_lft forever
[[email protected] /]# 

試想一下,此時的b2與b1能否互相通訊?如果不能該如何實現通訊

# 執行兩個容器在不同網段
[[email protected] ~]# docker run -itd --name web --network br0 --rm yanghaixx/nginx:v2
aec728f703567b36016cf09cde1ffd1036673d7c2d961cae0b81cd09721d5cb9
[[email protected] ~]# docker exec -it web /bin/bash
[[email protected] /]# 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
31: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:01:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.1.2/24 brd 172.17.1.255 scope global eth0
       valid_lft forever preferred_lft forever

[[email protected] ~]# docker run -itd --name web02 --rm yanghaixx/nginx:v2
4ee412be6d20b96a18f0b6fc0062beefc2192d906b212134b362ec41be5144ff
[[email protected] ~]# docker exec -it web02 /bin/bash
[[email protected] /]# 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
33: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:02:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.2.2/24 brd 192.168.2.255 scope global eth0
       valid_lft forever preferred_lft forever


[[email protected] ~]# docker ps -a
CONTAINER ID   IMAGE                     COMMAND                  CREATED         STATUS         PORTS     NAMES
4ee412be6d20   luojiatian1904/nginx:v2   "/usr/local/nginx/sb…"   2 minutes ago   Up 2 minutes             web02
aec728f70356   luojiatian1904/nginx:v2   "/usr/local/nginx/sb…"   2 minutes ago   Up 2 minutes             web

將br0網路 (C1) 連線到c2(一個容器執行兩個橋)

[[email protected] ~]# docker network connect  br0 aec728f70356(web容器ID)
[[email protected] /]# 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
33: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:02:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.2.2/24 brd 192.168.2.255 scope global eth0
       valid_lft forever preferred_lft forever
35: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:01:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.1.3/24 brd 172.17.1.255 scope global eth1
       valid_lft forever preferred_lft forever

[[email protected] /]# ping 172.17.1.2
PING 172.17.1.2 (172.17.1.2) 56(84) bytes of data.
64 bytes from 172.17.1.2: icmp_seq=1 ttl=64 time=0.095 ms

到此這篇關於docker網路設定過程詳解介紹的文章就介紹到這了,更多相關docker網路設定內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com