docker网络配置过程详解介绍

目录
  • Linux内核实现名称空间的创建
  • 操作Network Namespace
  • 创建veth pair
  • 实现Network Namespace间通信
  • 四种网络模式配置
  • 容器的常用操作
  • 自定义docker0桥的网络属性信息

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

ip netns命令

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

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

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

[root@master ~]# 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
[root@master ~]# ip netns list
[root@master ~]# ip netns add ns0
[root@master ~]# ip netns list
ns0
[root@master ~]# ls /var/run/netns
ns0
[root@master ~]# ip netns add ns0
Cannot create namespace file "/var/run/netns/ns0": File exists
[root@master ~]# touch /var/run/netns/ns1
[root@master ~]# ip netns list
Error: Peer netns reference is invalid.
Error: Peer netns reference is invalid.
ns1
ns0
[root@master ~]# ip netns del ns1
[root@master ~]# ip netns list
ns0

操作Network Namespace

ip命令提供了ip netns exec子命令可以在对应的 Network Namespace 中执行命令。

查看新创建 Network Namespace 的网卡信息

[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
可以看到,新创建的Network Namespace中会默认创建一个lo回环网卡,此时网卡处于关闭状态。此时,尝试去 ping 该lo回环网卡,会提示Network is unreachable
[root@master ~]# ip netns exec ns0 ping 127.0.0.1
connect: 网络不可达
通过下面的命令启用lo回环网卡
[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.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

[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 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: veth2672252@if6: <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
[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 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: veth2672252@if6: <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: veth0@veth1: <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: veth1@veth0: <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间通信

[root@master ~]#  ip netns list
ns0
[root@master ~]# ip netns add ns1
[root@master ~]# ip link set veth0 netns ns0
[root@master ~]# ip link set veth1 netns ns1
[root@master ~]# ip netns exec ns0 ip link set veth0 up
[root@master ~]# ip netns exec ns0 ip addr add 192.168.2.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.168.2.2/24 dev veth1
[root@master ~]# 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: veth0@if11: <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
[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
11: veth1@if10: <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
[root@master ~]# 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设备重命名

[root@master ~]# ip netns exec ns0 ip link set veth0 down
[root@master ~]# ip netns exec ns0 ip link set veth0 down
[root@master ~]# ip netns exec ns0 ip link set veth0 down
[root@master ~]# 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: veth0@if11: <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
[root@master ~]# ip netns exec ns1 ip link set veth1 down
[root@master ~]# ip netns exec ns1 ip link  set dev veth1 name eth0
[root@master ~]# ip netns exec ns1 ip link set eth0
[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
11: eth0@if10: <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模式

[root@master ~]# 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
[root@master ~]# 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: eth0@if13: <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
[root@master ~]# 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: eth0@if15: <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模式

[root@master ~]# 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模式

[root@master ~]# 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: eth0@if17: <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
新开一个终端
[root@master ~]# 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: eth0@if17: <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

[root@master ~]# 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: veth2672252@if6: <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/
/ #
[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 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: veth2672252@if6: <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
[root@master ~]# curl 192.168.197.131
ccc

容器的常用操作

[root@master ~]# docker run -it --name b6 --rm busybox
/ # hostname
ef156ee78b70
/ # exit
[root@master ~]# 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
[root@master ~]# 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 .
[root@master ~]# 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命令查看。
[root@master ~]# docker run -d --name web1 --rm -p 80 yanghaixx/nginx:v2
62029a5977a06e07ab5b40c78ce60e9cc519364512bc2e6d5785afd7dc1e090f
[root@master ~]#  docker port web1
80/tcp -> 0.0.0.0:49153
80/tcp -> :::49153
[root@master ~]# 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>

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

[root@master ~]# docker run -itd --name web2 --rm -p 2525:80 yanghaixx/nginx:v2
62a93a8ef40eec981b2d1682da92523027999991b0294782be38d4565b210ff8
[root@master ~]# docker port web2
80/tcp -> 0.0.0.0:2525
80/tcp -> :::2525
[root@master ~]# 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配置文件

[root@master ~]# vim /etc/docker/daemon.json
[root@master ~]# 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.
[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 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创建自定义桥

[root@master ~]# vim /etc/docker/daemon.json
[root@master ~]# 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.
[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 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

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

[root@master ~]# docker run -itd --name web --network br0 --rm yanghaixx/nginx:v2
aec728f703567b36016cf09cde1ffd1036673d7c2d961cae0b81cd09721d5cb9
[root@master ~]# docker exec -it web /bin/bash
[root@aec728f70356 /]# 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: eth0@if32: <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桥:

[root@master ~]# docker run -itd --name web02 --rm luojiatian1904/nginx:v2
4ee412be6d20b96a18f0b6fc0062beefc2192d906b212134b362ec41be5144ff
[root@master ~]# docker exec -it web02 /bin/bash
[root@4ee412be6d20 /]# 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: eth0@if34: <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
[root@4ee412be6d20 /]#

试想一下,此时的b2与b1能否互相通信?如果不能该如何实现通信

# 运行两个容器在不同网段
[root@master ~]# docker run -itd --name web --network br0 --rm yanghaixx/nginx:v2
aec728f703567b36016cf09cde1ffd1036673d7c2d961cae0b81cd09721d5cb9
[root@master ~]# docker exec -it web /bin/bash
[root@aec728f70356 /]# 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: eth0@if32: <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

[root@master ~]# docker run -itd --name web02 --rm yanghaixx/nginx:v2
4ee412be6d20b96a18f0b6fc0062beefc2192d906b212134b362ec41be5144ff
[root@master ~]# docker exec -it web02 /bin/bash
[root@4ee412be6d20 /]# 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: eth0@if34: <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

[root@master ~]# 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(一个容器运行两个桥)

[root@master ~]# docker network connect  br0 aec728f70356(web容器ID)
[root@4ee412be6d20 /]# 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: eth0@if34: <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: eth1@if36: <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

[root@4ee412be6d20 /]# 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网络配置内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Docker 网络模式及配置方式

    一.Docker网络模式 docker run 创建 Docker 容器时,可以用 –net 选项指定容器的网络模式,Docker 有以下 4 种网络模式: host 模式,使用 –net=host 指定. container 模式,使用 –net=container:NAME_or_ID 指定. none 模式,使用 –net=none 指定. bridge 模式,使用 –net=bridge 指定,默认设置. host 模式 如果启动容器的时候使用 host 模式,那么这个容器将不会获得一个

  • Android okhttp3.0忽略https证书的方法

    最近公司项目需要,网络协议支持https,之前接触不多,所以这次想总结一下https在android开发中的相关内容 一.https证书 对于https和证书的概念,大家可以自行搜索百度. 证书分两种: 1.花钱向认证机构购买的证书,(我们公司买的证书一个就需要4000元,TMD,还不如多租一台服务器).服务器如果使用了此类证书的话,那对于移动端来说,直接可以忽略此证书,直接用https访问.与之不同的是ios内置了很多信任的证书,所以他们不需要做任何操作 2.另一种是自己制作的证书,使用此类证

  • Android中访问证书有问题的SSL网页的方法

    PC上的浏览器会弹出证书错误的对话框,提示你是否要无视错误继续浏览.实际上在WebView里也可以这样做,以实现加载证书有问题的页面. 复制代码 代码如下: WebView webview = (WebView) findViewById(R.id.webview);webview.setWebViewClient(new WebViewClient() {    @Override    public void onReceivedSslError(WebView view, SslError

  • Docker容器网络端口配置过程详解

    暴露网络端口 实际上,Docker中涉及暴露网络端口的参数有两个,分别是-p和-P.下面分别来介绍. -P 使用-P,Docker会在宿主机上随机为应用分配一个未被使用的端口,并将其映射到容器开放的端口,以Nginx 为例,如下: 可以看到,Docker为应用分配了一个随机端口32768,使用该端口即可访问容器中的 nginx(http://lcalhost:32768). -p -p参数则有几种不同的用法: hostPort:containerPort 这种用法是将宿主机端口和容器端口绑定起来

  • 获取Android签名证书的公钥和私钥的简单实例

    本文以Android签名JKS格式的证书为例: package com.test; import java.io.FileInputStream; import java.security.Key; import java.security.KeyStore; import java.security.PrivateKey; import java.security.PublicKey; import javax.crypto.Cipher; public class SignTest { pu

  • Docker网络原理及自定义网络详细解析

    Docker在宿主机上虚拟了一个网桥,当创建并启动容器的时候,每一个容器默认都会被分配一个跟网桥网段一致的ip,网桥作为容器的网关,网桥与每一个容器联通,容器间通过网桥可以通信.由于网桥是虚拟出来的,外网无法进行寻址,也就是默认外网无法访问容器,需要在创建启动容器时把宿主机的端口与容器端口进行映射,通过宿主机IP端口访问容器.这是Docker默认的网络,它有一个弊端是只能通过IP让容器互相访问,如果想使用容器名称或容器ID互相访问需要在创建启动容器时候用link的方式修改hosts文件实现.一般

  • docker网络配置过程详解介绍

    目录 Linux内核实现名称空间的创建 操作Network Namespace 创建veth pair 实现Network Namespace间通信 四种网络模式配置 容器的常用操作 自定义docker0桥的网络属性信息 Linux内核实现名称空间的创建 ip netns命令 可以借助ip netns命令来完成对 Network Namespace 的各种操作.ip netns命令来自于iproute安装包,一般系统会默认安装,如果没有的话,请自行安装. 注意:ip netns命令修改网络配置时

  • SEATA事务服务DOCKER部署的过程详解

    1.创库授权语句 > create database seata: > grant all on seata.* to seata_user@'%' identified by '123455' 2.数据库建表语句 cat create_seata_table.sql -- -------------------------------- The script used when storeMode is 'db' -------------------------------- -- the

  • Python深度学习实战PyQt5安装与环境配置过程详解

    目录 1. PyQt5 图形界面开发工具 1.1 从 CLI 到 GUI 1.2 PyQt5 开发工具 2. 安装 PyQt5 和 QtTools pip 安装 PyQt5 pip 安装 QtTools 3. QtDesigner 和 PyUIC 的环境配置 3.1 在 PyCharm 添加 Create Tools 3.2 添加 QtDesigner 工具 3.3 添加 PyUIC 工具 4. QtDesigner 和 PyUIC 的快速入门 4.1 QtDesigner 的启动和入门 新建一

  • 性能测试监控网络吞吐量过程详解

    性能监控之网络吞吐量 检查设备网络带宽 ip a#检查网卡,选取带eth的 ethtool eth0 这里就遇到问题了,有可能没有权限 也有可能你的是云主机,部分会是这样的 这种情况只能问一下运维来获取主机的带宽,那我们问来的是什么呢,偷来一张图,获取Speed里的数值,这里就是说是百兆宽带 监控网络吞吐量 sar -n DEV 1 4#1秒采集一次,采集4次 sar -n DEV 1#1秒采集1次,一直采集,直到手动结束 我们需要的是rxpck/s和txpck/s,这两个加起来就是现在总共使用

  • Spring Boot 2.4 新特性之一键构建Docker镜像的过程详解

    背景 在我们开发过程中为了支持 Docker 容器化,一般使用 Maven 编译打包然后生成镜像,能够大大提供上线效率,同时能够快速动态扩容,快速回滚,着实很方便.docker-maven-plugin 插件就是为了帮助我们在 Maven 工程中,通过简单的配置,自动生成镜像并推送到仓库中. spotify .fabric8 这里主要使用的主要是如下两种插件 spotify .fabric8 , - -配置通过 xml 定义出 Dockerfile 或者挂载外部 Dockerfile 通过调用

  • Docker 网络工作原理详解

    Docker 网络工作原理 当Docker server也就是docker daemon启动时,会自动创建一个名字是docker0的bridge,每当docker创建一个Container时,会在主机上面创建一个名字是veth*的ethernet 端口,并把这个eth*加入到docker0的bridge,在container中会自动创建一个名字是eth0的ethernet端口,这个eth0和veth*会形成一个类似管道的对,一一对应. 配置DNS docker是如何分配每个container的h

  • docker网络双向连接详解

    查看docker网络 docker network ls [root@master ~]# docker network ls NETWORK ID NAME DRIVER SCOPE e9ecd3b9be87 bridge bridge local 45336895a3a4 host host local 6a44e8330adb none null local 创建网桥 docker network create bridge my-bridge [root@master ~]# docke

  • Hadoop源码分析二安装配置过程详解

    目录 1. 创建用户 2. 安装jdk 3. 修改hosts 4. 配置ssh免密登录 5. 安装zookeeper 解压: 修改配置文件 修改内容如下: 配置环境变量 启动 6. 安装hadoop 对于三台节点的配置安排如下: 解压: 修改配置文件: 修改core-site.xml 配置hdfs-site.xml 配置mapred-site.xml 配置yarn-site.xml 配置slaves 7. 初始化 在初始化前需要将所有机器都配置好hadoop (1) 启动zookeeper (2

  • 树莓派ASP.NET环境配置过程详解

    目录 在树莓派上部署ASP.NET环境(树莓派做ASP.NET项目服务器),之后Windows上开发的ASP.NET项目可以部署在树莓派上. 在树莓派上部署ASP.NET环境(树莓派做ASP.NET项目服务器),之后Windows上开发的ASP.NET项目可以部署在树莓派上.配置过程如下: 前言:本篇文章内容是根据mono官网上查阅的配置教程所写,当时做期末课设的时候一边查翻译一边配置(好像当时访问这个网站还要禾斗学上网),最终亲身试验可以用,随后在word上做了配置过程笔记,现在搬上来. mo

  • VMware下Centos7桥接方式网络配置步骤详解

    VMware虚拟机下Centos7 桥接方式网络配置完整步骤,供大家参考,具体内容如下 一.打开虚拟机的设置页面,设置虚拟机桥接模式如图:选择桥接模式(复制物理网络连接可选) 二.桥接模式配置VMware虚拟机网络 1. 查看宿主机网络信息: ipconfig /all 以太网适配器 本地连接: 连接特定的 DNS 后缀 . . . . . . . : 描述. . . . . . . . . . . . . . . : Realtek PCIe GBE Family Controller 物理地

随机推荐