码迷,mamicode.com
首页 > 其他好文 > 详细

Docker设置静态IP

时间:2015-05-19 15:02:06      阅读:688      评论:0      收藏:0      [点我收藏+]

标签:blank   网卡   target   新建   

创建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

为了支持ip netns命令,需要对CentOS6.6进行升级:

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

modify_docker_ip.sh:

#/bin/bash

if [ -z $1 ] || [ -z $2 ] || [ -z $3 ] || [ -z $4 ] || [ -z $5 ];

then

        echo "Usage: $0 CONTAINERID 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"

        exit

fi

CONTAINERID=$1

SETIP=$2

SETMASK=$3

GATEWAY=$4

ETHNAME=$5

#判断宿主机网卡是否存在

ifconfig $ETHNAME > /dev/null 2>&1

if [ $? -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

    fi

fi

#

pid=`docker inspect -f ‘{{.State.Pid}}‘ $CONTAINERID`

echo pid=$pid

mkdir -p /var/run/netns

find -L /var/run/netns -type l -delete

if [ -f /var/run/netns/$pid ]; then

    rm -f /var/run/netns/$pid

fi

ln -s /proc/$pid/ns/net /var/run/netns/$pid

#

ip link add $ETHNAME type veth peer name B

brctl addif docker0 $ETHNAME

ip link set $ETHNAME up

ip link set B netns $pid

#先删除容器内已存在的eth0

ip netns exec $pid ip link del eth0 > /dev/null 2>&1

#设置容器新的网卡eth0

ip 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设置静态IP

标签:blank   网卡   target   新建   

原文地址:http://yuanhuan.blog.51cto.com/3367116/1652690

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!