神剑山庄资源网 Design By www.hcban.com
在CentOS 7上当我们以类似下列命令将主机端口与容器端口映射时可能遇到无法访问容器服务的问题
docker run --name web_a -p 192.168.1.250:803:80 -d web_a:beta1.0.0 .
由于docker在执行此命令时,是向iptables注入了一条规则将主机803映射到容器80端口,但是CentOS 7中以firewalld服务替代了iptables。因此,上述命令的端口映射不会生效。
解决方法:首先观察一下主机上的网卡信息,确认增加了一个docker0的虚拟网卡:
[root@localhost /home]# ifconfig docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 inet6 fe80::42:5cff:fe0e:82f9 prefixlen 64 scopeid 0x20<link> ether 02:42:5c:0e:82:f9 txqueuelen 0 (Ethernet) RX packets 1288 bytes 1561177 (1.4 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1594 bytes 108755 (106.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 enp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.250 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::76f4:9aea:4973:ec6c prefixlen 64 scopeid 0x20<link> inet6 240e:379:542:2800:8844:77ba:78dd:7 prefixlen 128 scopeid 0x0<global> inet6 240e:379:542:2811:3ead:218:ba68:38e6 prefixlen 64 scopeid 0x0<global> ether 74:d4:35:09:93:19 txqueuelen 1000 (Ethernet) RX packets 10166908 bytes 1221399579 (1.1 GiB) RX errors 0 dropped 3014 overruns 0 frame 0 TX packets 982334 bytes 427296782 (407.5 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 device interrupt 18 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 1833650 bytes 450567722 (429.6 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1833650 bytes 450567722 (429.6 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 vethecef228: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet6 fe80::f425:f1ff:fe82:9c19 prefixlen 64 scopeid 0x20<link> ether f6:25:f1:82:9c:19 txqueuelen 0 (Ethernet) RX packets 234 bytes 1520113 (1.4 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 613 bytes 39809 (38.8 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
使用如下命令确认容器实例得到的虚拟ip:
docker inspect web_a
假设容器中的ip为172.17.0.2,接下来我们要为此IP做个NAT转发规则,并让firewalld服务处理此规则:
#主机端口请求转发到容器(容器中的服务不要监听localhost而要监听容器分配的虚拟IP或者以0.0.0.0替代) firewall-cmd --permanent --zone=public --add-masquerade 启用端口NAT转发 #将主机803端口请求转发到容器上的80端口 firewall-cmd --add-forward-port=port=803:proto=tcp:toaddr=172.17.0.2:toport=80 --permanent #重载规则 firewall-cmd --reload #列出所有规则 firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: enp2s0 sources: services: ssh dhcpv6-client ports: 3306/tcp 80/tcp 21/tcp 5000/tcp 6379/tcp 900/tcp 801/tcp 802/tcp 6000/tcp 5002/tcp 90/tcp 9092/tcp 81/tcp 803/tcp protocols: masquerade: yes forward-ports: port=803:proto=tcp:toport=80:toaddr=172.17.0.2 source-ports: icmp-blocks: rich rules:
#重新启动docker systemctl restart docker #重新启动容器 docker start web_a
经上述操作,就能以主机IP:803访问容器上的80端口的服务,并不需要关闭firewalld(很多网上的结论是换成iptables服务,实测不需要)。
总结
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无在CentOS 7 上为docker配置端口转发以兼容firewall的解决方法的评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
2024年09月19日
2024年09月19日
- 张宇.1995-一言难揪EMI百代】【WAV+CUE】
- 尹毓恪.2018-浴室与太平洋【海蝶】【FLAC分轨】
- 《遗迹2:黑暗的视界》发布全新职业护卫者预告片,将于9月25日上线
- 好评如潮《彼方的她》迎来首次大型更新
- 《最终幻想16》PC版震撼登场 官方发布精彩宣传片
- 《中国十大国语老歌.男人篇 3CD》[WAV/分轨][1.5GB]
- 《汽车音响极品流行热曲发烧大碟 情伤 男声篇2CD》[WAV/分轨][980MB]
- 《绝版经典情歌 国语老歌 内地珍藏篇 2CD》[WAV/分轨][1.2GB]
- 崩坏星穹铁道冬城史学家成就获得攻略
- 金铲铲之战符文大陆城邦效果分享 有哪些效果
- 仙境传说新启航单手斧流派技能流铁匠玩法介绍
- 归龙潮笼鸟意未平任务怎么完成 笼鸟意未平任务流程攻略
- 归龙潮长命锁指引任务怎么过 长命锁的指引前进任务攻略
- 归龙潮破败小屋密码是什么 尘封的记忆支线攻略
- 瑞鸣音乐《中国音乐地图之听见西藏HQCD》[WAV]