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

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

创建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,内容如下:

#/bin/bashif [ -z $1 ] || [ -z $2 ] || [ -z $3 ] || [ -z $4 ] || [ -z $5 ];then
echo "Usage: $0 CONTAINERID/CONTAINER_NAME IP MASK GATEWAY ETHNAME"
        echo "       Call the script like: sh manual_con_static_ip.sh  b0e18b6a4432 192.168.5.123 24 192.168.5.1 deth0"
        echo "       Call the script like: sh manual_con_static_ip.sh  my_container 192.168.5.123 24 192.168.5.1 deth0"
        exitfi
  CONTAINERID_NAME=$1SETIP=$2SETMASK=$3GATEWAY=$4ETHNAME=$5
 #判断宿主机网卡是否存在ifconfig $ETHNAME > /dev/null 2>&1if [ $? -eq 0 ]; then
    read -p "$ETHNAME exist,do you want delelte it? y/n " del    if [[ $del == 'y' ]]; then
    ip link del $ETHNAME
    else
    exit
    fifi#pid=`docker inspect -f '{{.State.Pid}}' $CONTAINERID_NAME`echo pid=$pidmkdir -p /var/run/netns
find -L /var/run/netns -type l -delete 
if [ -f /var/run/netns/$pid ]; then
    rm -f /var/run/netns/$pidfiln -s /proc/$pid/ns/net /var/run/netns/$pid#ip link add $ETHNAME type veth peer name B
brctl addif docker0 $ETHNAMEip link set $ETHNAME up
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
ip netns exec $pid ip link set eth0 up
ip netns exec $pid ip addr add $SETIP/$SETMASK dev eth0
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. 不使用flash实现复制文字(图片)到剪贴板

    <div>这里是待复制的文字或图片</div> var range = document.createRange(); var referenceNode = document ...

  2. Android自己定义控件--下拉刷新的实现

    我们在使用ListView的时候.非常多情况下须要用到下拉刷新的功能.为了了解下拉刷新的底层实现原理,我採用自己定义ListView控件的方式来实现效果. 实现的基本原理是:自己定义ListView, ...

  3. 基于注解的Sping AOP详解

    一.创建基础业务 package com.kang.sping.aop.service; import org.springframework.stereotype.Service; //使用注解@S ...

  4. JQuery 如何获取select选中的值

    一.html代码 <select id="ddl"> <option value="100" emoney="12" &g ...

  5. memcached和一致性hash算法

    1 一致性hash算法的一致性 这里的一致性指的是该算法可以保持memcached和数据库中的数据的一致性. 2 什么是一致性hash算法 2.1 为什么需要一致性hash算法 现在有大量的key v ...

  6. Transforming Auto-encoders

    http://www.cs.toronto.edu/~hinton/absps/transauto6.pdf The artificial neural networks that are used ...

  7. The JSP specification requires that an attribute name is preceded by whitespace--异常

    异常信息:org.apache.jasper.JasperException: /pages/selectedCourse.jsp (line: 4, column: 39) The JSP spec ...

  8. 【LeetCode】Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  9. github多用户提交错误Permission to repo denied to

    背景:同一台电脑的public key同时添加到了github的两个账户,导致user1的仓库没法正常提交. 解决办法:为两个账户分别配置ssh key,配置~/.ssh/config文件(windo ...

  10. Express中的Ejs模板传值问题

    在Ejs模板传值过程中,route下的变量值通过res.sender()中的变量参数传给views, 这时在views中若该变量在javascript代码中使用,可直接使用该变量,不必使用<% ...