docker容器网络配置与测试

docker容器网络配置

Linux内核实现名称空间的创建

ip netns命令

可以借助ip netns命令来完成对 Network Namespace 的各种操作。ip netns命令来自于iproute安装包,一般系统会默认安装,如果没有的话,请自行安装。

注意:ip netns命令修改网络配置时需要 sudo 权限。

可以通过ip netns命令完成对Network Namespace 的相关操作,可以通过ip netns help查看命令帮助信息:

[root@localhost ~]# 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

默认情况下,Linux系统中是没有任何 Network Namespace的,所以ip netns list命令不会返回任何信息。
###创建Network Namespace
通过命令创建一个名为ns0的命名空间:

[root@localhost ~]# ip netns add ns0
[root@localhost ~]# ip netns list
ns0

新创建的 Network Namespace 会出现在/var/run/netns/目录下。如果相同名字的 namespace 已经存在,命令会报Cannot create namespace file “/var/run/netns/ns0”: File exists的错误。

[root@localhost ~]# ls /var/run/netns/
ns0
[root@localhost ~]# ip netns add ns0
Cannot create namespace file "/var/run/netns/ns0": File exists

对于每个 Network Namespace 来说,它会有自己独立的网卡、路由表、ARP 表、iptables 等和网络相关的资源。

操作Network Namespace

[root@localhost ~]# ip netns exec ns0 ip addr
1: lo:  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

[root@localhost ~]# ip netns exec ns0 ping 127.0.0.1
connect: Network is unreachable

通过下面的命令启用lo回环网卡:

[root@localhost ~]# ip netns exec ns0 ip link set lo up
[root@localhost ~]# 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.099 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.025 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.023 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.023 ms
^C
--- 127.0.0.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3079ms
rtt min/avg/max/mdev = 0.023/0.042/0.099/0.033 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 连接起来。
docker容器网络配置与测试_第1张图片

创建veth pair

[root@localhost ~]#  ip link add type veth
[root@localhost ~]# ip a
1: lo:  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:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:08:71:10 brd ff:ff:ff:ff:ff:ff
    inet 192.168.181.159/24 brd 192.168.181.255 scope global dynamic noprefixroute ens33
       valid_lft 1396sec preferred_lft 1396sec
    inet6 fe80::20c:29ff:fe08:7110/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
3: virbr0:  mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:40:02:68 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: docker0:  mtu 1500 qdisc noqueue state DOWN group default
    link/ether 02:42:d6:e5:1c:1d 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
5: veth0@veth1:  mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether d2:1b:f1:ca:62:e5 brd ff:ff:ff:ff:ff:ff
6: veth1@veth0:  mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether c6:6d:87:d3:8b:c3 brd ff:ff:ff:ff:ff:ff


可以看到,此时系统中新增了一对veth pair,将veth0和veth1两个虚拟网卡连接了起来,此时这对 veth pair 处于”未启用“状态。

###实现Network Namespace间通信
下面我们利用veth pair实现两个不同的 Network Namespace 之间的通信。刚才我们已经创建了一个名为ns0的 Network Namespace,下面再创建一个信息Network Namespace,命名为ns1

[root@localhost ~]# ip netns add ns1
[root@localhost ~]# ip netns list
ns1
ns0

然后我们将veth0加入到ns0,将veth1加入到ns1

[root@localhost ~]# ip link set veth0 netns ns0
[root@localhost ~]# ip link set veth1 netns ns1

然后我们分别为这对veth pair配置上ip地址,并启用它们

[root@localhost ~]# ip link set veth0 netns ns0
[root@localhost ~]# ip link set veth1 netns ns1
[root@localhost ~]# ip netns exec ns0 ip link set veth0 up
[root@localhost ~]# ip netns exec ns0 ip addr add 10.0.0.1/24 dev veth0
[root@localhost ~]# ip netns exec ns1 ip link set lo up
[root@localhost ~]# ip netns exec ns1 ip link set veth1 up
[root@localhost ~]# ip netns exec ns1 ip addr add 10.0.0.2/24 dev veth1

查看这对veth pair的状态

[root@localhost ~]# ip netns exec ns0 ip a
1: lo:  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
5: veth0@if6:  mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether d2:1b:f1:ca:62:e5 brd ff:ff:ff:ff:ff:ff link-netns ns1
    inet 10.0.0.1/24 scope global veth0
       valid_lft forever preferred_lft forever
    inet6 fe80::d01b:f1ff:feca:62e5/64 scope link
       valid_lft forever preferred_lft forever

[root@localhost ~]# ip netns exec ns1 ip a
1: lo:  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
6: veth1@if5:  mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether c6:6d:87:d3:8b:c3 brd ff:ff:ff:ff:ff:ff link-netns ns0
    inet 10.0.0.2/24 scope global veth1
       valid_lft forever preferred_lft forever
    inet6 fe80::c46d:87ff:fed3:8bc3/64 scope link
       valid_lft forever preferred_lft forever

从上面可以看出,我们已经成功启用了这个veth pair,并为每个veth设备分配了对应的ip地址。我们尝试在ns1中访问ns0中的ip地址:

[root@localhost ~]# ip netns exec ns1 ping 10.0.0.1
PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data.
64 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time=0.117 ms
64 bytes from 10.0.0.1: icmp_seq=2 ttl=64 time=0.033 ms
64 bytes from 10.0.0.1: icmp_seq=3 ttl=64 time=0.028 ms
64 bytes from 10.0.0.1: icmp_seq=4 ttl=64 time=0.029 ms
^C
--- 10.0.0.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3110ms
rtt min/avg/max/mdev = 0.028/0.051/0.117/0.038 ms

可以看到,veth pair成功实现了两个不同Network Namespace之间的网络交互。
###veth设备重命名

[root@localhost ~]# ip netns exec ns0 ip link set veth0 down
[root@localhost ~]# ip netns exec ns0 ip link set dev veth0 name eth0
[root@localhost ~]#  ip netns exec ns0 ifconfig -a
eth0: flags=4098  mtu 1500
        inet 10.0.0.1  netmask 255.255.255.0  broadcast 0.0.0.0
        ether d2:1b:f1:ca:62:e5  txqueuelen 1000  (Ethernet)
        RX packets 18  bytes 1412 (1.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 18  bytes 1412 (1.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 8  bytes 672 (672.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 672 (672.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]# ip netns exec ns0 ip link set eth0 up

四种网络模式配置

bridge模式配置

[root@localhost ~]# docker run -it --name t1 --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:02
          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:47 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:6324 (6.1 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)

/ # exit
[root@localhost ~]# docker container ls -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

在创建容器时添加–network bridge与不加–network选项效果是一致的

[root@localhost ~]# docker run -it --name t1 --network bridge --rm busybox
/ # ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02
          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:21 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:2680 (2.6 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)

/ # exit

none模式配置

[root@localhost ~]# docker run -it --name z1 --network none --rm busybox
/ # ifconfig -a
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)

/ # exit

container模式配置

启动第一个容器

[root@localhost ~]# docker run -it --name b1 --rm busybox
/ # ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02
          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:18 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:2310 (2.2 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)

/ #

启动第二个容器

[root@localhost ~]# docker run -it --name b2 --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:17 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:2120 (2.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)

/ #

可以看到名为b2的容器IP地址是10.0.0.3,与第一个容器的IP地址不是一样的,也就是说并没有共享网络,此时如果我们将第二个容器的启动方式改变一下,就可以使名为b2的容器IP与B1容器IP一致,也即共享IP,但不共享文件系统。

[root@localhost ~]# docker run -it --name b2 --rm --network container:b1 busybox
/ # ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02
          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:32 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:3636 (3.5 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)

此时我们在b1容器上创建一个目录

/ # mkdir /tmp/data
/ # ls /tmp
data

到b2容器上检查/tmp目录会发现并没有这个目录,因为文件系统是处于隔离状态,仅仅是共享了网络而已。

/ # ls /tmp

在b2容器上部署一个站点

/ # 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

在b1容器上用本地地址去访问此站点

/ # wget -O - -q 127.0.0.1:80
hello world

由此可见,container模式下的容器间关系就相当于一台主机上的两个不同进程

host模式配置

启动容器时直接指明模式为host

[root@localhost ~]# docker run -it --name b2 --rm --network host busybox
/ # ifconfig
docker0   Link encap:Ethernet  HWaddr 02:42:D6:E5:1C:1D
          inet addr:172.17.0.1  Bcast:172.17.255.255  Mask:255.255.0.0
          inet6 addr: fe80::42:d6ff:fee5:1c1d/64 Scope:Link
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:35 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:4816 (4.7 KiB)

ens33     Link encap:Ethernet  HWaddr 00:0C:29:08:71:10
          inet addr:192.168.181.159  Bcast:192.168.181.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe08:7110/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:688834 errors:0 dropped:0 overruns:0 frame:0
          TX packets:291030 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:943340219 (899.6 MiB)  TX bytes:17737522 (16.9 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:60 errors:0 dropped:0 overruns:0 frame:0
          TX packets:60 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:9736 (9.5 KiB)  TX bytes:9736 (9.5 KiB)

virbr0    Link encap:Ethernet  HWaddr 52:54:00:40:02:68
          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.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:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

此时如果我们在这个容器中启动一个http站点,我们就可以直接用宿主机的IP直接在浏览器中访问这个容器中的站点了。
###容器的常用操作
###查看容器的主机名

[root@localhost ~]# docker run -it --name t1 --network bridge --rm busybox
/ # hostname
e55825f71965
/ #

在容器启动时注入主机名

[root@localhost ~]# docker run -it --name t1 --network bridge --hostname zhan --rm busybox
/ # hostname
zhan
/ # 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.2      zhan
/ # cat /etc/resolv.conf
# Generated by NetworkManager
search localdomain
nameserver 192.168.181.2
/ # ping www.baidu.com
PING www.baidu.com (182.61.200.7): 56 data bytes
64 bytes from 182.61.200.7: seq=0 ttl=127 time=72.555 ms
64 bytes from 182.61.200.7: seq=1 ttl=127 time=36.426 ms
64 bytes from 182.61.200.7: seq=2 ttl=127 time=145.014 ms
64 bytes from 182.61.200.7: seq=3 ttl=127 time=34.870 ms
^C
--- www.baidu.com ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 34.870/72.216/145.014 ms
/ #

手动指定容器要使用的DNS

[root@localhost ~]# docker run -it --name t1 --network bridge --hostname zhan --dns 114.114.114.114 --rm busybox
/ # cat /etc/resolv.conf
search localdomain
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:
www.baidu.com   canonical name = www.a.shifen.com
Name:   www.a.shifen.com
Address: 182.61.200.6
Name:   www.a.shifen.com
Address: 182.61.200.7

/ #

手动往/etc/hosts文件中注入主机名到IP地址的映射

[root@localhost ~]# docker run -it --name t1 --network bridge --hostname zhan --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.2      zhan
/ #

开放容器端口

执行docker run的时候有个-p选项,可以将容器中的应用端口映射到宿主机中,从而实现让外部主机可以通过访问宿主机的某端口来访问容器内应用的目的。

-p选项能够使用多次,其所能够暴露的端口必须是容器确实在监听的端口。

-p选项的使用格式:

-p
将指定的容器端口映射至主机所有地址的一个动态端口

四个-p简述和测试:
-p 
将指定的容器端口映射至主机所有地址的一个动态端口
-p :
将容器端口映射至指定的主机端口
-p ::
将指定的容器端口映射至主机指定的动态端口
-p ::
将指定的容器端口映射至主机指定的端口

-p :
将容器端口映射至指定的主机端口

[root@localhost ~]# docker run -dit --name wb1 -p 80:80 httpd
67a27046484216c776adc5b48970a27538072d5eee2ea659e8c7d5867547d453
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS         PORTS                               NAMES
67a270464842   httpd     "httpd-foreground"   5 seconds ago   Up 4 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   wb1
[root@localhost ~]# ss -antl
State     Recv-Q    Send-Q         Local Address:Port         Peer Address:Port    Process
LISTEN    0         128                127.0.0.1:6010              0.0.0.0:*
LISTEN    0         128                127.0.0.1:6011              0.0.0.0:*
LISTEN    0         128                  0.0.0.0:111               0.0.0.0:*
LISTEN    0         128                  0.0.0.0:80                0.0.0.0:*
LISTEN    0         32             192.168.122.1:53                0.0.0.0:*
LISTEN    0         128                  0.0.0.0:22                0.0.0.0:*
LISTEN    0         5                  127.0.0.1:631               0.0.0.0:*
LISTEN    0         128                    [::1]:6010                 [::]:*
LISTEN    0         128                    [::1]:6011                 [::]:*
LISTEN    0         128                     [::]:111                  [::]:*
LISTEN    0         128                     [::]:80                   [::]:*
LISTEN    0         128                     [::]:22                   [::]:*
LISTEN    0         5                      [::1]:631                  [::]:*

-p ::
将指定的容器端口映射至主机指定的动态端口

[root@localhost ~]# docker run -dit --name wb1 -p 192.168.181.159::80 httpd
a3714d142db360efc509d94af76596013495ca738957eb30736e67cb8ec61f67
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS         PORTS                           NAMES
a3714d142db3   httpd     "httpd-foreground"   9 seconds ago   Up 8 seconds   192.168.181.159:49153->80/tcp   wb1
[root@localhost ~]# ss -antl
State     Recv-Q    Send-Q         Local Address:Port         Peer Address:Port    Process
LISTEN    0         128                127.0.0.1:6010              0.0.0.0:*
LISTEN    0         128                127.0.0.1:6011              0.0.0.0:*
LISTEN    0         128          192.168.181.159:49153             0.0.0.0:*
LISTEN    0         128                  0.0.0.0:111               0.0.0.0:*
LISTEN    0         32             192.168.122.1:53                0.0.0.0:*
LISTEN    0         128                  0.0.0.0:22                0.0.0.0:*
LISTEN    0         5                  127.0.0.1:631               0.0.0.0:*
LISTEN    0         128                    [::1]:6010                 [::]:*
LISTEN    0         128                    [::1]:6011                 [::]:*
LISTEN    0         128                     [::]:111                  [::]:*
LISTEN    0         128                     [::]:22                   [::]:*
LISTEN    0         5                      [::1]:631                  [::]:*

docker容器网络配置与测试_第2张图片

-p ::
将指定的容器端口映射至主机指定的端口

[root@localhost ~]# docker run -dit --name wb1 -p 192.168.181.159:82:80 httpd
c40298b723477e1d8ea21f5fc2ac2487ce160a63bf23d60cd5b3f1c1c16fb714
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS          PORTS                        NAMES
c40298b72347   httpd     "httpd-foreground"   41 seconds ago   Up 40 seconds   192.168.181.159:82->80/tcp   wb1
[root@localhost ~]# ss -antl
State     Recv-Q    Send-Q         Local Address:Port         Peer Address:Port    Process
LISTEN    0         128                127.0.0.1:6010              0.0.0.0:*
LISTEN    0         128                127.0.0.1:6011              0.0.0.0:*
LISTEN    0         128                  0.0.0.0:111               0.0.0.0:*
LISTEN    0         128          192.168.181.159:82                0.0.0.0:*
LISTEN    0         32             192.168.122.1:53                0.0.0.0:*
LISTEN    0         128                  0.0.0.0:22                0.0.0.0:*
LISTEN    0         5                  127.0.0.1:631               0.0.0.0:*
LISTEN    0         128                    [::1]:6010                 [::]:*
LISTEN    0         128                    [::1]:6011                 [::]:*
LISTEN    0         128                     [::]:111                  [::]:*
LISTEN    0         128                     [::]:22                   [::]:*
LISTEN    0         5                      [::1]:631                  [::]:*

docker容器网络配置与测试_第3张图片

动态端口指的是随机端口,具体的映射结果可使用docker port命令查看。

[root@localhost ~]# docker run -d  --name web --rm -p 80 httpd
2182e4e31bebdc8a7d9931efcf19beaf83391136cf866b329c5f5475f5539c3b

以上命令执行后会一直占用着前端,我们新开一个终端连接来看一下容器的80端口被映射到了宿主机的什么端口上

[root@localhost ~]# docker port web
80/tcp -> 0.0.0.0:49154
80/tcp -> :::49154

由此可见,容器的80端口被暴露到了宿主机的32769端口上,此时我们在宿主机上访问一下这个端口看是否能访问到容器内的站点

[root@localhost ~]# curl http://127.0.0.1:49154

It works!

ptables防火墙规则将随容器的创建自动生成,随容器的删除自动删除规则。

将容器端口映射到指定IP的随机端口

[root@localhost ~]# docker run --name web1 --rm -p 192.168.181.159::80 nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/08/09 09:27:41 [notice] 1#1: using the "epoll" event method
2022/08/09 09:27:41 [notice] 1#1: nginx/1.21.5
2022/08/09 09:27:41 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/08/09 09:27:41 [notice] 1#1: OS: Linux 4.18.0-365.el8.x86_64
2022/08/09 09:27:41 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/08/09 09:27:41 [notice] 1#1: start worker processes
2022/08/09 09:27:41 [notice] 1#1: start worker process 31
2022/08/09 09:27:41 [notice] 1#1: start worker process 32
2022/08/09 09:27:41 [notice] 1#1: start worker process 33
2022/08/09 09:27:41 [notice] 1#1: start worker process 34


在另一个终端上查看端口映射情况

[root@localhost ~]# docker port web1
80/tcp -> 192.168.181.159:49153

将容器端口映射到宿主机的指定端口

root@localhost ~]# docker run --name web1 --rm -p 80:80 nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/08/09 09:29:34 [notice] 1#1: using the "epoll" event method
2022/08/09 09:29:34 [notice] 1#1: nginx/1.21.5
2022/08/09 09:29:34 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/08/09 09:29:34 [notice] 1#1: OS: Linux 4.18.0-365.el8.x86_64
2022/08/09 09:29:34 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/08/09 09:29:34 [notice] 1#1: start worker processes
2022/08/09 09:29:34 [notice] 1#1: start worker process 31
2022/08/09 09:29:34 [notice] 1#1: start worker process 32
2022/08/09 09:29:34 [notice] 1#1: start worker process 33
2022/08/09 09:29:34 [notice] 1#1: start worker process 34

在另一个终端上查看端口映射情况

[root@localhost ~]# docker port web1
80/tcp -> 0.0.0.0:80
80/tcp -> :::80

自定义docker0桥的网络属性信息

自定义docker0桥的网络属性信息需要修改/etc/docker/daemon.json配置文件

[root@localhost ~]# vim /etc/docker/daemon.json
[root@localhost ~]# cat /etc/docker/daemon.json
{
  "registry-mirrors": ["https://urcxei73.mirror.aliyuncs.com"],
  "bip": "192.168.1.5/24"
}
[root@localhost ~]# systemctl restart docker

核心选项为bip,即bridge ip之意,用于指定docker0桥自身的IP地址;其它选项可通过此地址计算得出

docker远程连接

dockerd守护进程的C/S,其默认仅监听Unix Socket格式的地址(/var/run/docker.sock),如果要使用TCP套接字,则需要修改/etc/docker/daemon.json配置文件,添加如下内容,然后重启docker服务:
“hosts”: [“tcp://0.0.0.0:2375”, “unix:///var/run/docker.sock”]

[root@localhost ~]# vim /lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock

[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker.service

在客户端上向dockerd直接传递“-H|–host”选项指定要控制哪台主机上的docker容器

[root@localhost ~]# docker -H 192.168.181.159:2375 ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

docker创建自定义桥

创建一个额外的自定义桥,区别于docker0

[root@localhost ~]# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
276fd5810426   bridge    bridge    local
9886211cb62f   host      host      local
55370c0f451a   none      null      local
[root@localhost ~]# docker network create -d bridge --subnet "192.168.2.0/24" --gateway "192.168.2.1" br0
4434dff6060951509342d74f5d848545b8ad5536a6a5ed90cf7953aeaa32ecd9
[root@localhost ~]# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
4434dff60609   br0       bridge    local
276fd5810426   bridge    bridge    local
9886211cb62f   host      host      local
55370c0f451a   none      null      local

使用新创建的自定义桥来创建容器:

[root@localhost ~]# 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:38 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:5203 (5.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)

/ #


再创建一个容器,使用默认的bridge桥:

[root@localhost ~]# 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:01
          inet addr:192.168.1.1  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:21 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:2680 (2.6 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)

/ #

试想一下,此时的b2与b1能否互相通信?如果不能该如何实现通信?
docker容器网络配置与测试_第4张图片

使用connect把zlh加入bridge网桥中去,使它们在同一个网段,所以能ping通
[root@localhost ~]# docker network connect  bridge zlh


取消或者禁止,使用disconnect把zlh拉出bridge网桥中去,使它们不在同一个网段,所以不能ping通
[root@localhost ~]# docker network disconnect  bridge zlh

你可能感兴趣的:(docker,网络,linux)