zoukankan      html  css  js  c++  java
  • docker 给容器配置ip(和主机一个网段)

    docker 给容器配置ip(和主机一个网段)。详情参考:http://www.xiaomastack.com/2015/02/06/docker-static-ip/

    #/bin/bash
    if [ -z $1 ] || [ -z $2 ] || [ -z $3 ] || [ -z $4 ] || [ -z $5 ];
    then
            echo "*****Input the necessary parameters: 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`
    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 br3 $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
    pid=`docker inspect -f '{{.State.Pid}}' $CONTAINERID`

    为了操作容器需要获取容器的进程号PID,docker inspect 可以查看容器的底层信息,查看容器dfe83012cda2所有的底层相关信息,用docker inspect dfe83012cda2就可以查看。-f参数可以格式化输出给定的信息,比如查看容器的状态

  • 相关阅读:
    数据结构进阶——线段树
    基本数据结构—Hash哈希
    NOIP2013提高组 day2 2019.7.15
    基本算法——归并排序
    基本数据结构—Trie
    NOIP 2011 提高组 Day2 校模拟 7.11
    Noip2014提高组真题Day1,2 校模拟7.7
    NOIP2015 提高组 day1 7.8校模拟
    NOIP2008 提高组 6.9校模拟
    STL-#include<set>
  • 原文地址:https://www.cnblogs.com/zipon/p/7424747.html
Copyright © 2011-2022 走看看