Docker中使用固定IP

方法一: pipwork工具
1. 安装
下载pipework工具, 然后配置到path路径.
$ git clone https://github.com/jpetazzo/pipework.git
$ sudo cp -rp pipework/pipework /usr/local/bin/
$ sudo yum -y install apring bridge-utils


2. 网络设定
# 删除容器之后删除网络 
ip link set br0 down
brctl delbr br0
# 创建网络
brctl addbr br0;
ip link set dev br0 up;
ip addr add 192.168.2.1/24 dev br0;

docker stop db;
docker start db;
pipework br0 db 192.168.2.103/24;




3. 创建容器和设定IP
docker run -d -e SERVERADMIN="[email protected]" -e DEFAULT_LOGS_DIR="/logs/httpd/defaults" --name=apache -v /home/pandy/docker-resources/apache/conf:/etc/httpd/conf -v /home/pandy/docker-resources/apache/html:/var/www/html pandy/apache
pipework br0 apache 192.168.2.100/24;





方法二: Docker中使用固定IP: http://yaxin-cn.github.io/Docker/docker-container-use-static-IP.html
注意:重启电脑之后ip依然改变
创建容器
docker run -i -t --rm --net='none' --name test centos:centos7 /bin/bash
container id =>b3ae71122656
docker inspect -f "{{.State.Pid}}"  b3ae71122656
   //这个容器id对应的pid
输出:8948    , 注意, 记住这个符号

创建一个链接, 打开另一个终端, 输入
sudo mkdir -p /var/run/netns
sudo ln -s /proc/8948/ns/net /var/run/netns/8948


然后 创建一对端到端的网卡,将veth_db84e747c3绑定到docker0网桥,并启动。将另一块网卡X放到container内部
sudo ip link add veth_db84e747c3 type veth peer name X
sudo brctl addif docker0 veth_db84e747c3
sudo ip link set veth_db84e747c3 up
sudo ip link set X netns 8948


在docker容器里面查看:ip addr
[root@b3ae71122656 /]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
6: X: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
    link/ether c6:d8:5f:f6:fa:6a brd ff:ff:ff:ff:ff:ff

   
然后对container内部新添加的网卡进行配置(可以通过man ip-netns更详细查看)
sudo ip netns exec 8948 ip link set dev X name eth0   //重新命名容器内部的网卡叫eth0
sudo ip netns exec 8948 ip link set eth0 up
sudo ip netns exec 8948 ip addr add 172.17.111.10/16 dev eth0   //配置网卡
sudo ip netns exec 8948 ip route add default via 172.17.42.1


测试:
ping 172.17.111.10
PING 172.17.111.10 (172.17.111.10) 56(84) bytes of data.
64 bytes from 172.17.111.10: icmp_seq=1 ttl=64 time=0.097 ms
64 bytes from 172.17.111.10: icmp_seq=2 ttl=64 time=0.076 ms


命令组成的文件
#!/usr/bin/env bash 
# filename: bind_addr.sh
# 调用方式:[root@bogon pandy]# ./bind_addr.sh test 192.168.2.11
if [ `id -u` -ne 0 ];
  then echo '必须使用root权限' exit 
fi 

if [ $# != 2 ]; 
  then echo "使用方法: $0 容器名字 IP" exit 1 
fi 

container_name=$1 bind_ip=$2 container_id=`docker inspect -f '{{.Id}}' $container_name 2> /dev/null` 

if [ ! $container_id ];
  then echo "容器不存在" exit 2
fi 

bind_ip=`echo $bind_ip | egrep '^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$'` 

if [ ! $bind_ip ];
  then echo "IP地址格式不正确" exit 3
fi 

container_minid=`echo $container_id | cut -c 1-10`
container_netmask=`ip addr show docker0 | grep "inet\b" | awk '{print $2}' | cut -d / -f2`
container_gw=`ip addr show docker0 | grep "inet\b" | awk '{print $2}' | cut -d / -f1`
bridge_name="veth_$container_minid"
container_ip=$bind_ip/$container_netmask
#pid=`docker inspect -f '{{.State.Pid}}' salt-master 2> /dev/null`
pid=`docker inspect -f '{{.State.Pid}}' $container_name 2> /dev/null`
echo "pid=$pid"
if [ ! $pid ];
  then echo "获取容器$container_name的id失败" exit 4 
fi 

if [ ! -d /var/run/netns ];
  then mkdir -p /var/run/netns 
fi

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

echo "开始设定IP"
echo "====================================================="
ip link add $bridge_name type veth peer name X 
brctl addif docker0 $bridge_name 
ip link set $bridge_name up 
ip link set X netns $pid 
ip netns exec $pid 
ip link set dev X name eth0 
ip netns exec $pid 
ip link set eth0 up 
ip netns exec $pid 
ip addr add $container_ip dev eth0 
ip netns exec $pid 
ip route add default via $container_gw
echo "====================================================="

你可能感兴趣的:(docker)