docker 配置docker0内外网互联

说明:

正常情况下在docker0下的容器IP与局域名其他机器隔绝,需要做端口映射来使用容器中的资源,那么有没有一种方法在不使用br0桥接模式下,使用docker0默认方式使局域网直接访问容器呢?请见下面过程

1、配置路由,将至172网段的数据路由至docker宿主机

在这里插入图片描述

2、宿主机开启转发功能

添加最下面一行

[root@localhost ~]# cat /etc/sysctl.conf 
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.ip_forward=1

3、firewalld开启NAT转发功能

[root@localhost src]# firewall-cmd --permanent --zone=public --add-masquerade
success
[root@localhost src]# firewall-cmd --reload
success
[root@localhost src]# firewall-cmd --query-masquerade
yes

4、测试

[root@localhost src]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos7_ssh         latest              b1a0c70ee5a8        2 days ago          859 MB
docker.io/nginx     latest              08393e824c32        6 days ago          132 MB
[root@localhost src]# 

[root@localhost src]# docker run -itd --privileged docker.io/nginx 
c5d74b849f60f078d17cdc27e7f4851de6a8ede8b782ac2922be3027f59cbe78

[root@localhost src]# docker inspect c5d74b849f60|grep -i ipaddr
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",

浏览器直接访问172.17.0.2
在这里插入图片描述

----------------end

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值