How to change ip address of docker0 bridge interfa

2019-07-29 04:12发布

I'm using Docker Toolbox for windows 7, I'm trying to change the ip address of docker0 interface but, having difficulty in finding the exact solution which works for Windows 7. Can anyone please help me in finding the solution.

Client:
 Version:      1.12.5
 API version:  1.24
 Go version:   go1.6.4
 Git commit:   7392c3b
 Built:        Fri Dec 16 06:14:34 2016
 OS/Arch:      linux/amd64

Server:
 Version:      1.12.5
 API version:  1.24
 Go version:   go1.6.4
 Git commit:   7392c3b
 Built:        Fri Dec 16 06:14:34 2016
 OS/Arch:      linux/amd64

Thank you.

1条回答
闹够了就滚
2楼-- · 2019-07-29 04:39

To change the IP address Docker will set on it's docker0 interface, you have to use the --bip option which defines the CIDR (eg. --bip=10.32.57.1/24), see "Customize the docker0 bridge" in Docker user guide.

Docker Toolbox uses Boot2Docker (running in a virtual machine) which is based on the Tiny Core Linux OS.

Docker daemon reads /var/lib/boot2docker/profile before starting (see "Local Customisation" in Boot2Docker's FAQ) where a EXTRA_ARGS variable is ready to be filled with your custom settings.

Just add your --bip=... in EXTRA_ARGS's value part and restart the daemon.

The following command (to type in the Docker Quickstart Terminal) will stop the Docker daemon, drop any existing rule, delete the interface, add a --bip option to /var/lib/boot2docker/profile and restart the daemon:

docker-machine ssh default "\
    sudo /etc/init.d/docker stop ; \
    sudo iptables -t nat -F POSTROUTING ; \
    sudo ip link del docker0 ; \
    sudo sed -i \"/^EXTRA_ARGS='\\$/a --bip=10.32.57.1/24\" /var/lib/boot2docker/profile ; \
    sudo /etc/init.d/docker start \
    "

(Content of /var/lib/boot2docker is persisted between Boot2Docker VM restarts so running this command once should suffice)

You can check with:

docker-machine ssh default "ip a show dev docker0"

If anyone needs the same manipulation on Debian (without Boot2Docker thus):

For Sysvinit:

cat >> /etc/default/docker <<EOT
# Change Docker network bridge:
DOCKER_OPTS="--bip=10.32.57.1/24" # "3257" = "dckr" on a phone keyboard
EOT

For systemd:

cat > /etc/systemd/system/docker.service <<'EOT'
[Service]
EnvironmentFile=-/etc/sysconfig/docker
EnvironmentFile=-/etc/sysconfig/docker-storage
EnvironmentFile=-/etc/sysconfig/docker-network
ExecStart=
ExecStart=/usr/bin/docker daemon -H fd:// $OPTIONS \
          $DOCKER_STORAGE_OPTIONS \
          $DOCKER_NETWORK_OPTIONS \
          $BLOCK_REGISTRY \
          $INSECURE_REGISTRY
EOT
mkdir /etc/sysconfig
cat > /etc/sysconfig/docker <<EOT
OPTIONS="--bip=10.32.57.1/24"
EOT
systemctl daemon-reload

Then (for both Sysvinit and systemd):

service docker stop
iptables -t nat -F POSTROUTING
ip link del docker0
service docker start
iptables -t nat -L -n # Check if POSTROUTING table is OK
查看更多
登录 后发表回答