User-Profile-Image
hankin
  • 5
  • centos7
  • docker
  • mysql
  • PostgreSQL
  • git/gitlab
  • ELK
  • python
    • python-Tornado
    • python-django
  • redis
  • nginx
  • kvm
  • proxmox
  • mongo
  • kubernetes
  • prometheus
  • GlusterFs
  • nfs
  • freeswitch
  • httpd
  • shell脚本
  • linux
  • fastdfs
  • nextcloud
  • openssl
  • openvpn
  • rabbitmq
  • sqlite
  • svn
  • java
  • ubuntu
  • vue2
  • wordpress
  • php
  • IOT物联网
  • 项目
  • 故障处理
  • 树莓派
  • 博客存档
  • 未分类
  • 杂项
  • #1742(无标题)
  • 新视野
  • 分类
    • 项目
    • 树莓派
    • 杂项
    • 未分类
    • 新视野
    • 故障处理
    • 博客存档
    • 交换机
    • wordpress
    • vue2
    • ubuntu
    • svn
    • sqlite
    • shell脚本
    • redis
    • rabbitmq
    • python-django
    • python
    • proxmox
    • prometheus
    • PostgreSQL
    • php
    • openvpn
    • openssl
    • nginx
    • nfs
    • nextcloud
    • mysql
    • mongo
    • linux
    • kvm
    • kubernetes
    • java
    • IOT物联网
    • httpd
    • GlusterFs
    • git/gitlab
    • freeswitch
    • fastdfs
    • ELK
    • docker
    • centos7
  • 页面
    • #1742(无标题)
  • 友链
      请到[后台->主题设置->友情链接]中设置。
Help?

Please contact us on our email for need any support

Support
    首页   ›   docker   ›   正文
docker

docker使用pipework给容器分配路由器下的局域网ip

2022-10-28 23:33:23
627  0 0

需求

需要让docker容器使用路由器提供的内网ip,使用内网ip直接访问容器。

环境准备

centos7系统、docker。

1.宿主机安装pipework

wget -O pipework-master.zip https://codeload.github.com/jpetazzo/pipework/zip/master
unzip pipework-master.zip
cp -p pipework-master/pipework /usr/local/bin/

2.修改宿主机网络为桥接

vim /etc/sysconfig/network-scripts/ifcfg-br0            
    TYPE=Bridge     
    DEVICE=br0      
    BOOTPROTO=static        
    ONBOOT=yes      
    IPADDR=192.168.2.80     
    NETMASK=255.255.255.0       
    GATEWAY=192.168.2.1

vim /etc/sysconfig/network-scripts/ifcfg-em3            
    DEVICE=em3      
    ONBOOT=yes      
    BRIDGE=br0      
然后重启网络确认修改后宿主机网络正常

3.docker创建容器

docker run -itd --net=none --name=test-nginx1 nginx         
        #--net=none不设置网络

4.使用pipework设置docker网络

方式一:使用固定ip

pipework br0  test01  192.168.2.235/24@192.168.2.1          
        网桥  容器名     IP地址/网关地址

方式二:使用dhcp

pipework br0 test1 dhcp
!若主机环境中存在dhcp服务器,才可以通过dhcp的方式获取IP

5.测试

进入容器、执行ip addr可看到容器已得到由路由器分配的ip
  docker exec -it xx /bin/bash              
  ifconfig
此时容器可正常与内网和公网联通

注意

重启容器后 ip会丢失,需要重新设置。
可使用脚本检查容器状态,并自动执行设置。
评论 (0)

点击这里取消回复。

欢迎您 游客  

Copyright © 2025 网站备案号 : 蜀ICP备2022017747号
smarty_hankin 主题. Designed by hankin
主页
页面
  • #1742(无标题)
博主
tang.show
tang.show 管理员
linux、centos、docker 、k8s、mysql等技术相关的总结文档
213 文章 2 评论 202083 浏览
测试
测试