此文已由作者袁欢授权网易云社区发布。

欢迎访问网易云社区,了解更多网易技术产品运营经验。

创建docker容器

docker run -it --name=yh -h yh --net=none debian:sshd bash   ### 确保使用--net=none参数,此时新建的容器内不会创建网卡

docker ps

此时登录容器查看IP,会发现没有eth0网卡:

root@yh:/# ifconfig -a
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

CentOS6.6升级iproute

宿主机是CentOS6.6,为了支持ip netns命令,需要进行升级:

rpm -ivh http://www.elrepo.org/elrepo-release-6-6.el6.elrepo.noarch.rpm

yum --enablerepo=elrepo-kernel install kernel-lt -y  ### 升级内核

vi /etc/grub.conf   ###   修改default=0,默认启动新内核

reboot  ### 重启使新内核生效

uname -r   ### 查看内核版本号是否是新的

yum install -y http://rdo.fedorapeople.org/rdo-release.rpm  ### 更新rdo仓库
vim /etc/yum.repos.d/rdo-release.repo  ### 修改文件内容为如下

[openstack-juno]
name=OpenStack Juno Repository
baseurl=http://repos.fedorapeople.org/repos/openstack/openstack-icehouse/epel-6/
enabled=1
skip_if_unavailable=0
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-RDO-Juno

yum --enablerepo=openstack-juno install iproute   ### 更新iproute
rpm -q iproute

设置静态IP

新建一个修改静态IP的脚本modify_docker_ip.sh,内容如下:

  1. #/bin/bashif [ -z $1 ] || [ -z $2 ] || [ -z $3 ] || [ -z $4 ] || [ -z $5 ];then
  2. echo "Usage: $0 CONTAINERID/CONTAINER_NAME IP MASK GATEWAY ETHNAME"
  3.         echo "       Call the script like: sh manual_con_static_ip.sh  b0e18b6a4432 192.168.5.123 24 192.168.5.1 deth0"
  4.         echo "       Call the script like: sh manual_con_static_ip.sh  my_container 192.168.5.123 24 192.168.5.1 deth0"
  5.         exitfi
  6.   CONTAINERID_NAME=$1SETIP=$2SETMASK=$3GATEWAY=$4ETHNAME=$5
  7.  #判断宿主机网卡是否存在ifconfig $ETHNAME > /dev/null 2>&1if [ $? -eq 0 ]; then
  8.     read -"$ETHNAME exist,do you want delelte it? y/n " del    if [[ $del == 'y' ]]; then
  9.     ip link del $ETHNAME
  10.     else
  11.     exit
  12.     fifi#pid=`docker inspect -f '{{.State.Pid}}' $CONTAINERID_NAME`echo pid=$pidmkdir -p /var/run/netns
  13. find -/var/run/netns -type l -delete 
  14. if [ -/var/run/netns/$pid ]; then
  15.     rm -/var/run/netns/$pidfiln -/proc/$pid/ns/net /var/run/netns/$pid#ip link add $ETHNAME type veth peer name B
  16. brctl addif docker0 $ETHNAMEip link set $ETHNAME up
  17. ip link set B netns $pid#先删除容器内已存在的eth0ip netns exec $pid ip link del eth0 > /dev/null 2>&1#设置容器新的网卡eth0ip netns exec $pid ip link set dev B name eth0
  18. ip netns exec $pid ip link set eth0 up
  19. ip netns exec $pid ip addr add $SETIP/$SETMASK dev eth0
  20. ip netns exec $pid ip route add default via $GATEWAY

在宿主机上执行如下命令为容器创建网卡,并分配静态IP:

./modify_docker_ip.sh 8feff00a0a26 172.17.0.2 16 172.17.42.1 deth0

其中:8feff00a0a26 是容器ID,172.17.0.2是容器的静态IP,16是掩码,172.17.42.1是容器的网关地址(即运行容器的系统中docker0的IP),deth0为新建的宿主机网卡名(对应容器内的eth0)

此时查看宿主机IP:

[root@node0003 ~]# ifconfig 
deth0     Link encap:Ethernet  HWaddr DA:19:96:9B:1B:E5  
          inet6 addr: fe80::d819:96ff:fe9b:1be5/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:468 (468.0 b)  TX bytes:468 (468.0 b)

docker0   Link encap:Ethernet  HWaddr 56:84:7A:FE:97:99  
          inet addr:172.17.42.1  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::5484:7aff:fefe:9799/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:12 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:768 (768.0 b)  TX bytes:468 (468.0 b)

在容器内查看IP:

root@yh:/# ifconfig -a
eth0      Link encap:Ethernet  HWaddr 22:e1:72:17:b6:dd  
          inet addr:172.17.0.2  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::20e1:72ff:fe17:b6dd/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:238 (238.0 B)  TX bytes:238 (238.0 B)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

附录:

Docker关于网络这块的文档详见:https://docs.docker.com/articles/networking/

另外一个工具pipework也可以设置静态IP:https://github.com/jpetazzo/pipework

遗留问题:

问题:docker容器重启之后,eth0消失,IP失效。

描述:docker文档中描述:容器stop的时候,docker自动清理网卡配置,所以重启之后容器内的eth0消失,静态IP也就失效了。

解决方法:1. run一个docker容器之后,再次执行文中的脚本或者pipework重新设置IP即可。 2. 可能还有更好的办法,待研究。

网易云容器服务为用户提供了无服务器容器,让企业能够快速部署业务,轻松运维服务。容器服务支持弹性伸缩、垂直扩容、灰度升级、服务发现、服务编排、错误恢复及性能监测等功能。

免费体验云安全(易盾)内容安全、验证码等服务

更多网易技术、产品、运营经验分享请点击

相关文章:
【推荐】 分布式存储系统可靠性系列一:如何估算

为Docker容器设置静态IP的更多相关文章

  1. docker容器分配静态IP

    最近因为工作要求需要用学习使用docker,最后卡在了网络配置这一块.默认情况下启动容器的时候,docker容器使用的是bridge策略比如: docker run -ti ubuntu:latest ...

  2. 利用pipework为docker容器设置固定IP

    今天介绍如何在redhat/centos7系列机器上使用pipework为docker启动的容器指定一个固定ip,我们知道默认情况下,docker会使用 bridge网络模式为每一个启动的容器动态分配 ...

  3. 为docker容器设置独立ip

    docker 1.12使用新版macvlan设置与宿主机同网段ip ****************************************** 由于开发的一些特殊需求,需要将容器部署在与宿主 ...

  4. 转:为Docker容器设置固定IP实现网络联通(1)——通过Pipework为Docker容器设置

    https://blog.csdn.net/chinagissoft/article/details/51250839 1. 创建并启动一个容器: docker run --cap-add=NET_A ...

  5. docker容器配置独立ip

    一般安装docker后都会通过端口转发的方式使用网络,比如 “-p 2294:22” 就将2294抓发到22端口来提供sftp服务,这样使用起来没有问题.但端口号很难记忆,如果前边有nginx等抓发工 ...

  6. 在linux中设置静态ip地址

    在linux中设置静态ip地址1.在终端中输入:vi /etc/sysconfig/network-scripts/ifcfg-eth0 2.开始编辑,填写ip地址.子网掩码.网关.DNS等[root ...

  7. CentOS 6.5、6.7 设置静态 ip 教程

    CentOS 6.5.6.7 设置静态 ip 教程,可以ping通外网:www.baidu.com ①. 网络适配器(VMware Network Adapter) 选择NAT模式 ②. 设置静态 i ...

  8. 在ubuntu14.04设置静态ip

    打开网络的配置文件 sudo vim /etc/network/interfaces 选择网卡,我这里是有线网卡eth0,设置静态ip为192.168.1.108 auto eth0 iface et ...

  9. 为Linux服务器设置静态IP的方法

    这里以CentOS 7系列为例设置静态IP,原来RedHat系列的Linux发行版可以通过setup工具方便的设置静态IP,但是在版本7之后setup工具的功能就逐渐减弱了,所以这时候采用修改配置文件 ...

随机推荐

  1. Fedora21 安装视频播放解码器

    12 12月 2014年12月12日 Posted by 涛儿 2 首先启用RPM Fusion软件源: sudo rpm -ivh http://download1.rpmfusion.org/fr ...

  2. Machine Learning: 一部气势恢宏的人工智能发展史

    转载自:雷锋网 本文作者:陈圳 2016-09-12 09:46 导语:机器学习的从产生,发展,低潮和全盛的历史 雷锋网(公众号:雷锋网)按:本文作者DataCastle数据城堡,主要介绍了机器学习的 ...

  3. Mac 怎么打开两个终端

    把光标移到终端上,然后Command+N 启动maven  : mvn tomcat7:run

  4. Android UI经常使用实例 怎样实现欢迎界面(Splash Screen)

    在Android平台下.下载一个应用后,首次打开映入眼帘的便是Splash Screen,暂且不说Android的设计原则提不提倡这样的Splash Screen.先来看看一般使用Splash Scr ...

  5. Hadoop - YARN 概述

    一 概述       Apache Hadoop YARN (Yet Another Resource Negotiator,还有一种资源协调者)是一种新的 Hadoop 资源管理器,它是一个通用资源 ...

  6. 【leetcode】Jump Game I, II 跳跃游戏一和二

    题目: Jump Game I: Given an array of non-negative integers, you are initially positioned at the first ...

  7. iOS开发之──传感器使用

    本文转载至 http://mobile.51cto.com/iphone-423219.htm 在实际的应用开发中,会用到传感器,下面首先介绍一下iphone4的传感器,然后对一些传感器的开发的API ...

  8. c++的运算符的重载

    1 什么是c++运算符的重载 c++运算符的重载就是说对+.-.>.<等运算符进行重新定义,这样的话,除了基本的类型,所有的类都可以进行基本的运算了,用起来非常方便.特别是用在各种算法中. ...

  9. Conditions in bash scripting (if statements)

    Shell中判断语句if中-z至-d的意思 - sunny_2015 - 博客园 https://www.cnblogs.com/coffy/p/5748292.html Conditions in ...

  10. static 静态域 类域 静态方法 工厂方法 he use of the static keyword to create fields and methods that belong to the class, rather than to an instance of the class 非访问修饰符

    总结: 1.无论一个类实例化多少对象,它的静态变量只有一份拷贝: 静态域属于类,而非由类构造的实例化的对象,所有类的实例对象共享静态域. class Employee { private static ...