How to change Docker IP address on Centos 7?

2019-09-13 23:42发布

问题:

I installed Docker on Centos 7. Below is the output of ifconfig command

# ifconfig

docker0: flags=4099 mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
ether 02:42:ea:cd:a1:19 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

ens33: flags=4163 mtu 1500
inet 192.168.149.5 netmask 255.255.255.0 broadcast 192.168.149.255
inet6 fe80::c494:6514:b641:e046 prefixlen 64 scopeid 0x20
ether 00:0c:29:57:00:09 txqueuelen 1000 (Ethernet)
RX packets 42800 bytes 26241065 (25.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 31687 bytes 4228571 (4.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1 (Local Loopback)
RX packets 302 bytes 29477 (28.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 302 bytes 29477 (28.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

virbr0: flags=4099 mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:ce:2f:27 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

I tried to change IP address of docker0 172.17.0.1 to 192.168.149.5, the IP address of ens33. I searched many ref site, but I can't make it. How can I set the ip address of docker0 on CentOS 7?

回答1:

First, the position of docker configuration file depends on the docker version. If some newer docker you used, its configuration will be in /etc/docker/daemon.json. Put the following code into the configuration file and restart the docker daemon, the IP of docker bridge will be updated.

{
  "bip": "192.168.1.1/24"
}

Hope helps.

Control and configure Docker with systemd

Daemon configuration file



回答2:

This is my process to change ip address of docker container.

1. systemctl stop docker 

2. ip link set dev docker0 down 

3. ip addr del 172.17.0.1/16 dev docker0

4. ip addr add 192.168.149.5/24 dev docker0

5. ip link set dev docker0 up

6. iptables -t nat -L -n
   iptables -t nat -F POSTROUTING

7. echo 'DOCKER_OPTS="--bip 192.168.149.5/24"' >> /etc/default/docker

The problem is there is no such a file (/etc/default/docker). And i generated docker file, but it does not work.