docker固定IP地址重启不变

时间:2021-09-10 14:04:00

docker固定IP地址重启不变

代码地址

https://github.com/lioncui/docker-static-ip

宿主机IP为  10.6.17.12

docker IP为 10.6.17.15

首先安装python , 以及 pip

然后使用 pip 安装 docker-py

pip install docker-py

yum install bridge-utils -y

新建 ifcfg-br0 文件

---------------------------------------------------------------

DEVICE=br0

TYPE=Bridge

BOOTPROTO=static

IPADDR=10.6.17.12

NETMASK=255.255.255.0

GATEWAY=10.6.17.1

ONBOOT=yes

----------------------------------------------------------------

修改 ifcfg-em1

----------------------------------------------------------------

TYPE=Ethernet

NAME=em1

DEVICE=em1

ONBOOT=yes

BRIDGE=br0

----------------------------------------------------------------

重启docker 使用 br0 桥接生效

service docker restart

下载 docker-static-ip

解压

cd docker-static-ip

python duration.py

docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

mysql_1             latest              91496cb918d7        3 hours ago         3.479 GB

centos              latest              5f6d37eb82ec        3 days ago          422.1 MB

docker  ps -a

CONTAINER ID        IMAGE               COMMAND               CREATED              STATUS              PORTS               NAMES

d741d1d7b00a        mysql_1             "/usr/sbin/sshd -D"   About a minute ago   Up About a minute                       mysql_1

docker run -h="mysql_1" --name mysql_1 -d --net=none -v /opt/upload/mysql:/opt/data d741d1d7b00a

d741d1d7b00a285173fe1953a78a7801b676bbbc1c9c7c1665d02b432fdee731

docker exec -i d741d1d7b00a ip a

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

echo "d741d1d7b00a,docker0,10.6.17.15/24,10.6.17.1" >> containers.cfg

docker exec -i d741d1d7b00a ip a

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

71: eth0@if72: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

link/ether 8e:f5:a6:59:21:69 brd ff:ff:ff:ff:ff:ff link-netnsid 0

inet 10.6.17.15/24 scope global eth0

valid_lft forever preferred_lft forever

inet6 fe80::8cf5:a6ff:fe59:2169/64 scope link

valid_lft forever preferred_lft forever