大家好,又见面了,我是你们的朋友全栈君。
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 (属于Linux内核的命令,所以不加 "--"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命令不会返回任何信息。
[root@localhost ~]# ip netns list # 列出
[root@localhost ~]#
创建Network Namespace
通过命令创建一个名为ns0的命名空间:
[root@localhost ~]# ip netns add ns0 # 创建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
# 手动创建在/var/run/netns下也是不认可的
[root@localhost ~]# touch /var/run/netns/ns1
[root@localhost ~]# ip netns list
Error: Peer netns reference is invalid. # 报错
Error: Peer netns reference is invalid.
ns1
ns0
# 删除
[root@localhost ~]# ip netns del ns1
[root@localhost ~]# ip netns list # 再次查看无报错
ns0
对于每个 Network Namespace 来说,它会有自己独立的网卡、路由表、ARP 表、iptables 等和网络相关的资源。
操作Network Namespace
ip命令提供了ip netns exec
子命令可以在对应的 Network Namespace 中执行命令。
查看新创建 Network Namespace 的网卡信息
[root@localhost ~]# 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@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.042 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.053 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@localhost ~]# 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:21:52:e8 brd ff:ff:ff:ff:ff:ff
inet 192.168.220.17/24 brd 192.168.220.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::1792:21f6:7f28:5ffa/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:a1:e4:66:9d 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
[root@localhost ~]# ip link add type veth # 创建
[root@localhost ~]# 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:21:52:e8 brd ff:ff:ff:ff:ff:ff
inet 192.168.220.17/24 brd 192.168.220.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::1792:21f6:7f28:5ffa/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:a1:e4:66:9d 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
4: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000 # 新创建的
link/ether d6:90:9d:4e:95:77 brd ff:ff:ff:ff:ff:ff
5: veth1@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000 # 新创建的
link/ether 9e:79:1e:8a:72:3d 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 list
ns0
[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 netns exec ns0 ip link set veth0 up
[root@localhost ~]# ip netns exec ns0 ip addr add 192.168.2.1/24 dev veth0
[root@localhost ~]# ip netns exec ns1 ip link set veth1 up
[root@localhost ~]# ip netns exec ns1 ip addr add 192.168.2.2/24 dev veth1
查看这对veth pair的状态
[root@localhost ~]# 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
4: veth0@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether d6:90:9d:4e:95:77 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::d490:9dff:fe4e:9577/64 scope link
valid_lft forever preferred_lft forever
[root@localhost ~]# 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
5: veth1@if4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 9e:79:1e:8a:72:3d 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::9c79:1eff:fe8a:723d/64 scope link
valid_lft forever preferred_lft forever
从上面可以看出,我们已经成功启用了这个veth pair,并为每个veth设备分配了对应的ip地址。我们尝试在ns1中访问ns0中的ip地址:
[root@localhost ~]# 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.223 ms
64 bytes from 192.168.2.1: icmp_seq=2 ttl=64 time=0.106 ms
可以看到,veth pair成功实现了两个不同Network Namespace之间的网络交互。
veth设备重命名
重命名ns0中的veth0
[root@localhost ~]# ip netns exec ns0 ip link set veth0 down # 关闭veth0
[root@localhost ~]# ip netns exec ns0 ip link set dev veth0 name eth0 # 重命名veth0为eth0
[root@localhost ~]# ip netns exec ns0 ip link set eth0 up # 启用eth0
[root@localhost ~]# 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
4: eth0@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 # 重命名成功
link/ether d6:90:9d:4e:95:77 brd ff:ff:ff:ff:ff:ff link-netns ns1
inet 192.168.2.1/24 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::d490:9dff:fe4e:9577/64 scope link
valid_lft forever preferred_lft forever
重命名ns1中的veth0
[root@localhost ~]# ip netns exec ns1 ip link set veth1 down #关闭veth1
[root@localhost ~]# ip netns exec ns1 ip link set dev veth1 name eth0 # 重命名veth1为eth0
[root@localhost ~]# ip netns exec ns1 ip link set eth0 up # 启用eth0
[root@localhost ~]# 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
5: eth0@if4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 # 重命名成功
link/ether 9e:79:1e:8a:72:3d 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
inet6 fe80::9c79:1eff:fe8a:723d/64 scope link
valid_lft forever preferred_lft forever
四种网络模式配置
bridge模式配置
[root@localhost ~]# docker pull busybox
[root@localhost ~]# 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
6: eth0@if7: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ # exit
[root@localhost ~]# 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
8: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ # exit
在创建容器时添加–network bridge与不加–network选项效果是一致的 默认的模式
none模式
[root@localhost ~]# 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@localhost ~]# 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
12: eth0@if13: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ #
# 重新打开一个终端使用container模式 与第一个容器相比较
[root@localhost ~]# 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
12: eth0@if13: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/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 QAQ
/ # ls
QAQ bin dev etc home proc root sys tmp usr var
# b4上查看
/ # ls
bin dev etc home proc root sys tmp usr var
# 到b4容器上检查会发现并没有这个目录,因为文件系统是处于隔离状态,仅仅是共享了网络而已。
# 在b3上部署一个网站
/ # echo "This is a pig." > QAQ/index.html
/ # httpd -h QAQ/
/ # 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
This is a pig.
/ #
# 由此可见,container模式下的容器间关系就相当于一台主机上的两个不同进程
这个模式指定新创建的容器和已经存在的一个容器共享一个 Network Namespace,而不是和宿主机共享。新创建的容器不会创建自己的网卡,配置自己的 IP,而是和一个指定的容器共享 IP、端口范围等。同样,两个容器除了网络方面,其他的如文件系统、进程列表等还是隔离的。两个容器的进程可以通过 lo 网卡设备通信。
host模式
启动容器时直接指明模式为host
[root@localhost ~]# 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:21:52:e8 brd ff:ff:ff:ff:ff:ff
inet 192.168.220.17/24 brd 192.168.220.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::1792:21f6:7f28:5ffa/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue
link/ether 02:42:a1:e4:66:9d 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:a1ff:fee4:669d/64 scope link
valid_lft forever preferred_lft forever
[root@localhost ~]# 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:21:52:e8 brd ff:ff:ff:ff:ff:ff
inet 192.168.220.17/24 brd 192.168.220.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::1792:21f6:7f28:5ffa/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:a1:e4:66:9d 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:a1ff:fee4:669d/64 scope link
valid_lft forever preferred_lft forever
# 此时我们在这个容器中启动一个站点,我们就可以直接用宿主机的IP直接在浏览器中访问这个容器中的站点了
# 容器部署一个网站
/ # mkdir www
/ # echo "This is a cat." > www/index.html
/ # httpd -h www/
# 宿主机访问
[root@localhost ~]# curl 192.168.220.17
This is a cat.
启动容器的时候使用host模式,那么这个容器将不会获得一个独立的Network Namespace,而是和宿主机共用一个Network Namespace。容器将不会虚拟出自己的网卡,配置自己的IP等,而是使用宿主机的IP和端口。但是,容器的其他方面,如文件系统、进程列表等还是和宿主机隔离的。
容器的常用操作
查看容器的主机名
[root@localhost ~]# docker run -it --name b6 --rm busybox
/ # hostname
322e0365483b
在容器启动时注入主机名
[root@localhost ~]# docker run -it --name b7 --rm --hostname glfqdp busybox
/ # hostname
glfqdp
/ # 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 glfqdp # 注入主机名时会自动创建主机名到IP的映射关系
/ # cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 114.114.114.114 # DNS也会自动配置为宿主机的DNS
nameserver 8.8.8.8
/ # ping baidu.com
PING baidu.com (220.181.38.251): 56 data bytes
64 bytes from 220.181.38.251: seq=0 ttl=127 time=30.270 ms
64 bytes from 220.181.38.251: seq=1 ttl=127 time=30.020 ms
/ # exit
手动指定容器要使用的DNS
[root@localhost ~]# docker run -it --name b8 --rm --dns 8.8.8.8 --hostname glfqdp busybox
/ # cat /etc/resolv.conf
nameserver 8.8.8.8
/ # exit
手动往/etc/hosts文件中注入主机名到IP地址的映射
[root@localhost ~]# docker run -it --name b9 --rm --hostname lplp --add-host baidu.com:8.8.8.8 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
8.8.8.8 baidu.com
172.17.0.2 lplp
/ # exit
端口映射
执行docker run的时候有个-p选项,可以将容器中的应用端口映射到宿主机中,从而实现让外部主机可以通过访问宿主机的某端口来访问容器内应用的目的。
-p选项能够使用多次,其所能够暴露的端口必须是容器确实在监听的端口。
-p选项的使用格式:
- -p <containerPort>
- 将指定的容器端口映射至主机所有地址的一个动态端口
动态端口指的是随机端口,具体的映射结果可使用docker port命令查看。
# 将容器内nginx的80端口映射到宿主机的随机端口
[root@localhost ~]# docker run -d --name web --rm -p 80 1225514226/nginx:v2.0
acaea4eab08b9937b06dfe93da3d86795ac859c29a60ee0edc8f120aaf9d29ab
[root@localhost ~]# docker port web
80/tcp -> 0.0.0.0:49153 # ipv4
80/tcp -> :::49153 # ipv6
由此可见,容器的80端口被暴露到了宿主机的49153端口上,此时我们在宿主机上访问一下这个端口看是否能访问到容器内的站点
[root@localhost ~]# curl 192.168.220.17:49153
welcome to nginx!
iptables防火墙规则将随容器的创建自动生成,随容器的暂停 / 删除自动删除规则。
- -p <hostPort>:<containerPort>
- 将容器端口<containerPort>映射至指定的主机端口<hostPort>
将容器端口映射到宿主机的指定端口
[root@localhost ~]# docker run -itd --name web --rm -p 8080:80 1225514226/nginx:v2.0
[root@localhost ~]# docker port web
80/tcp -> 0.0.0.0:8080
80/tcp -> :::8080
# 宿主机访问
[root@localhost ~]# curl 192.168.220.17:8080
welcome to nginx!
- -p <ip>::<containerPort>
- 将指定的容器端口<containerPort>映射至主机指定<ip>的动态端口
将指定的容器端口映射到主机指定IP的随机端口
[root@localhost ~]# docker run -itd --name web --rm -p 192.168.220.17::80 1225514226/nginx:v2.0
68d446f3c450ef707519dc92cc55adba837623eaa63062b49abc8af07c1e5b35
[root@localhost ~]# docker port web
80/tcp -> 192.168.220.17:49153
# 宿主机访问
[root@localhost ~]# curl 192.168.220.17:49153
welcome to nginx!
- -p <ip>:<hostPort>:<containerPort>
- 将指定的容器端口<containerPort>映射至主机指定<ip>的端口<hostPort>
将指定的容器端口映射到指定主机IP的指定端口
[root@localhost ~]# docker run -itd --name web --rm -p 192.168.220.17:9999:80 1225514226/nginx:v2.0
1deff8a4a3f28a3ed2661907edd9f64c4878373e44260698cb60875e860010df
[root@localhost ~]# docker port web
80/tcp -> 192.168.220.17:9999
# 宿主机访问
[root@localhost ~]# curl 192.168.220.17:9999
welcom to nginx!
-P(大写)将所有容器内暴露的端口发布到宿主机的随机端口
自定义docker0桥的网络属性信息
自定义docker0桥的网络属性信息需要修改/etc/docker/daemon.json
配置文件
[root@localhost ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://wn5c7d7w.mirror.aliyuncs.com"],
"bip": "192.168.2.1/24" # 改变宿主机的docker0网卡IP
}
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# 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:21:52:e8 brd ff:ff:ff:ff:ff:ff
inet 192.168.220.17/24 brd 192.168.220.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::1792:21f6:7f28:5ffa/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:a1:e4:66:9d 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:a1ff:fee4:669d/64 scope link
valid_lft forever preferred_lft forever
docker0 ip未改变之前默认是172.17.0.1/16,核心选项为bip,即bridge ip之意,用于指定docker0桥自身的IP地址;其它选项可通过此地址计算得出。
创建一个容器进去查看IP
[root@localhost ~]# docker run -itd --name web --rm 1225514226/nginx:v2.0
94b02bec9a8e03d8c73f19282f5954ba70c53130fd5a8d4c8af3bf3fe9ff8fdd
[root@localhost ~]# docker exec -it web /bin/bash
[root@94b02bec9a8e /]# 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
46: eth0@if47: <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 # 默认IP也变成192...
valid_lft forever preferred_lft forever
docker创建自定义桥
创建一个额外的自定义桥,区别于docker0
[root@localhost ~]# docker network create -d bridge --subnet "172.17.2.0/24" --gateway "172.17.2.1" br0
f96a9671bfa582b925305f8890c7fadf4b54cda6410cd238786dc7b0574700a5
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
f96a9671bfa5 br0 bridge local
788ac3e94c5a bridge bridge local
cd5368439dc0 host host local
c49a1db81682 none null local
使用新创建的自定义桥来创建容器:
[root@localhost ~]# docker run -itd --name web01 --rm --network br0 1225514226/nginx:v2.0
a98412139dc85eae51f6994737f24c56b2be3dac7211d7734fc099e8031904a4
[root@localhost ~]# docker exec -it web01 /bin/bash
[root@a98412139dc8 /]# 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
49: eth0@if50: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:02:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.2.2/24 brd 172.17.2.255 scope global eth0
valid_lft forever preferred_lft forever
再创建一个容器,使用默认的bridge桥:
[root@localhost ~]# docker run -itd --name web02 --rm 1225514226/nginx:v2.0
65d36dd328f7f522c3808917d2289ea84e69e9faa404ae7bc523138b4ff1292e
[root@localhost ~]# docker exec -it web02 /bin/bash
[root@65d36dd328f7 /]# 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
51: eth0@if52: <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
试想一下,此时的b2与b1能否互相通信?如果不能该如何实现通信?
# 运行两个容器在不同网段
[root@localhost ~]# docker run -itd --name c1 --rm --network br0 1225514226/nginx:v2.0
b3b6e6dc9e2b486519acc5fd53ed4e911493715a097ebfddb53a509be12a6c80
[root@localhost ~]# docker run -itd --name c2 --rm 1225514226/nginx:v2.0
0ed765ee0e78132eac679b0da613cccf7196240ba5cde093b47593666fbadad7
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ed765ee0e78 1225514226/nginx:v2.0 "/usr/local/nginx/sb…" 4 seconds ago Up 3 seconds c2
b3b6e6dc9e2b 1225514226/nginx:v2.0 "/usr/local/nginx/sb…" 14 seconds ago Up 12 seconds c1
[root@localhost ~]# docker exec -it c1 /bin/bash
[root@b3b6e6dc9e2b /]# 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
57: eth0@if58: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:02:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.2.2/24 brd 172.17.2.255 scope global eth0 # 172网段
valid_lft forever preferred_lft forever
[root@b3b6e6dc9e2b /]#
[root@localhost ~]# docker exec -it c2 /bin/bash
[root@0ed765ee0e78 /]# 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
59: eth0@if60: <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 # 192网段
valid_lft forever preferred_lft forever
将br0网络 (C1) 连接到c2(一个容器运行两个桥)
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ed765ee0e78 1225514226/nginx:v2.0 "/usr/local/nginx/sb…" 6 minutes ago Up 6 minutes c2
b3b6e6dc9e2b 1225514226/nginx:v2.0 "/usr/local/nginx/sb…" 6 minutes ago Up 6 minutes c1
[root@localhost ~]# docker network connect br0 0ed765ee0e78(C2容器ID)
# 查看c2
[root@0ed765ee0e78 /]# 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
59: eth0@if60: <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
61: eth1@if62: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:02:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.2.3/24 brd 172.17.2.255 scope global eth1
valid_lft forever preferred_lft forever # 添加了c1的网段
[root@0ed765ee0e78 /]# ping 172.17.2.2 # ping c1容器的地址
PING 172.17.2.2 (172.17.2.2) 56(84) bytes of data.
64 bytes from 172.17.2.2: icmp_seq=1 ttl=64 time=0.112 ms
64 bytes from 172.17.2.2: icmp_seq=2 ttl=64 time=1.21 ms
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/149625.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...