Docker容器网络地址 docker容器网络配置文件( 六 )



示例:

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

[rootlocalhost ~]# docker run -d -p 80 httpd5d969f5c435c02164caf5945ba2a0f5d0ff3c8b7d3e6217adc5c03f855b1c028[rootlocalhost ~]# docker ps -aCONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES5d969f5c435chttpd"httpd-foreground"28 seconds agoUp 27 seconds0.0.0.0:49153->80/tcp, :::49153->80/tcpcompetent_payne[rootlocalhost ~]# ss -anltStateRecv-QSend-QLocal Address:PortPeer Address:PortProcessLISTEN01280.0.0.0:220.0.0.0:*LISTEN01280.0.0.0:491530.0.0.0:*LISTEN0128[::]:22[::]:*LISTEN0128[::]:49153[::]:*[rootlocalhost ~]# curl 172.17.0.2<html><body><h1>It works!</h1></body></html>[rootlocalhost ~]# curl 192.168.111.135:49153<html><body><h1>It works!</h1></body></html>
-p (hostPort):(containerPort)
将容器端口(containerPort)映射至指定的主机端口(hostPort)

[rootlocalhost ~]# docker run -d -p 8080:80 httpd554dc98f55155d31e6b79adedda74c937eaf4459582a088bae5d2e3fbfb25307[rootlocalhost ~]# docker ps -aCONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES554dc98f5515httpd"httpd-foreground"9 seconds agoUp 8 seconds0.0.0.0:8080->80/tcp, :::8080->80/tcpdistracted_galileo[rootlocalhost ~]# ss -anltStateRecv-QSend-QLocal Address:PortPeer Address:PortProcessLISTEN01280.0.0.0:80800.0.0.0:*LISTEN01280.0.0.0:220.0.0.0:*LISTEN0128[::]:8080[::]:*LISTEN0128[::]:22[::]:*[rootlocalhost ~]# curl 172.17.0.2<html><body><h1>It works!</h1></body></html>[rootlocalhost ~]# curl 192.168.111.135:8080<html><body><h1>It works!</h1></body></html>
-p (ip)::(containerPort)
将指定的容器端口(containerPort)映射至主机指定(ip)的动态端口

[rootlocalhost ~]# docker run -d -p 192.168.111.135::80 httpdedde41dc824b45b2aafd50e41268a19faf0f31d11a43c67c821d0b8fa997ecec[rootlocalhost ~]# docker ps -aCONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMESedde41dc824bhttpd"httpd-foreground"7 seconds agoUp 5 seconds192.168.111.135:49153->80/tcpstrange_curie[rootlocalhost ~]# ss -anltStateRecv-QSend-QLocal Address:PortPeer Address:PortProcessLISTEN01280.0.0.0:220.0.0.0:*LISTEN0128192.168.111.135:491530.0.0.0:*LISTEN0128[::]:22[::]:*rootlocalhost ~]# curl 172.17.0.2<html><body><h1>It works!</h1></body></html>[rootlocalhost ~]# curl 192.168.111.135:49153<html><body><h1>It works!</h1></body></html>
-p (ip):(hostPort):(containerPort)
将指定的容器端口(containerPort)映射至主机指定(ip)的端口(hostPort)

[rootlocalhost ~]# docker run -d -p 192.168.111.135:80:80 httpdc044e47fbff6473c26df06a78f523b9683e60ac906e136799669dba47d92059a[rootlocalhost ~]# docker ps -aCONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMESc044e47fbff6httpd"httpd-foreground"5 seconds agoUp 4 seconds192.168.111.135:80->80/tcpmystifying_chatterjee[rootlocalhost ~]# ss -anltStateRecv-QSend-QLocal Address:PortPeer Address:PortProcessLISTEN0128192.168.111.135:800.0.0.0:*LISTEN01280.0.0.0:220.0.0.0:*LISTEN0128[::]:22[::]:*[rootlocalhost ~]# curl 172.17.0.2<html><body><h1>It works!</h1></body></html>[rootlocalhost ~]# curl 192.168.111.135:80<html><body><h1>It works!</h1></body></html>
动态端口指的是随机端口 , 具体的映射结果可使用docker port命令 。
[rootlocalhost ~]# docker port c044e47fbff680/tcp -> 192.168.111.135:80
注意:
iptables防火墙规则将随容器的创建自动生成 , 随容器的删除自动删除规则 。

3.6 自定义docker0网桥的网络属性信息
自定义docker0网桥的网络属性信息很多 , 以下只演示bip , 详情见官方文档相关配置
需要修改/etc/docker/daemon.json配置文件
[rootlocalhost ~]# vim /etc/docker/daemon.json {"registry-mirrors": ["https://6vrrj6n2.mirror.aliyuncs.com"],"bip": "192.168.100.1/24"}[rootlocalhost ~]# systemctl daemon-reload [rootlocalhost ~]# systemctl restart docker[rootlocalhost ~]# ip a1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope hostvalid_lft forever preferred_lft forever2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000link/ether 00:0c:29:bb:22:82 brd ff:ff:ff:ff:ff:ffinet 192.168.111.135/24 brd 192.168.111.255 scope global dynamic noprefixroute ens160valid_lft 1224sec preferred_lft 1224secinet6 fe80::3d5c:b9d6:55f:48e9/64 scope link noprefixroutevalid_lft forever preferred_lft forever3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group defaultlink/ether 02:42:35:39:6d:e6 brd ff:ff:ff:ff:ff:ffinet 192.168.100.1/24 brd 192.168.100.255 scope global docker0valid_lft forever preferred_lft forever# 网桥修改后 , 创建出来的容器虚拟网卡IP地址规律也会改变[rootlocalhost ~]# docker run -it --rm busybox/ # ip a1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft forever4: eth0if5: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueuelink/ether 02:42:c0:a8:64:02 brd ff:ff:ff:ff:ff:ffinet 192.168.100.2/24 brd 192.168.100.255 scope global eth0valid_lft forever preferred_lft forever/ # route -nKernel IP routing tableDestinationGatewayGenmaskFlags Metric RefUse Iface0.0.0.0192.168.100.10.0.0.0UG000 eth0192.168.100.00.0.0.0255.255.255.0U000 eth0


推荐阅读