-
-
Save kamermans/94b1c41086de0204750b to your computer and use it in GitHub Desktop.
#!/bin/sh -e | |
# | |
# NOTE: Since Docker 1.10 (February 4, 2016), it has been possible to configure the | |
# Docker daemon using a JSON config file. On Linux, this file is normally located at | |
# /etc/docker/daemon.json. You should use this JSON config method if you are running | |
# a version of Docker that is at least 1.10! | |
# Here is an example configuration that sets the docker0 bridge IP to 192.168.254.1/24: | |
# { | |
# "bip": "192.168.254.1/24" | |
# } | |
# | |
# You can run this script directly from github as root like this: | |
# curl -sS https://gist.githubusercontent.com/kamermans/94b1c41086de0204750b/raw/configure_docker0.sh | sudo bash -s - 192.168.254.1/24 | |
# | |
# * Make sure you replace "192.168.254.1/24" with the network that you want to use | |
# | |
# NOTE: This script is intended for Debian / Ubuntu only! | |
if [ $# -lt 1 ]; then | |
echo "Usage: sudo ./configure_docker0.sh <ip/CIDR>" | |
echo " examples: " | |
echo " ./configure_docker0.sh 10.200.0.57/16" | |
echo " ./configure_docker0.sh 172.31.0.21/16" | |
echo " ./configure_docker0.sh 192.168.254.1/24" | |
echo " " | |
echo " NOTE: You should stop Docker before running this script." | |
echo " When you restart it, Docker will use the new IP." | |
exit 2 | |
fi | |
INIT_SYSTEM="sysv" | |
if ps -o comm -1 | grep -q systemd; then | |
INIT_SYSTEM="systemd" | |
fi | |
NEW_IP="$1" | |
DOCKER_INIT="/etc/default/docker" | |
if [ ! -f "$DOCKER_INIT" ]; then | |
cat << EOF > $DOCKER_INIT | |
# Docker Upstart and SysVinit configuration file | |
# Customize location of Docker binary (especially for development testing). | |
#DOCKER="/usr/local/bin/docker" | |
# Use DOCKER_OPTS to modify the daemon startup options. | |
#DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4" | |
DOCKER_OPTS="--bip=$NEW_IP" | |
# If you need Docker to use an HTTP proxy, it can also be specified here. | |
#export http_proxy="http://127.0.0.1:3128/" | |
# This is also a handy place to tweak where Docker's temporary files go. | |
#export TMPDIR="/mnt/bigdrive/docker-tmp" | |
EOF | |
echo "Created a new Docker default file at $DOCKER_INIT" | |
exit 0; | |
fi | |
echo "Removing old docker0 network(s)" | |
NETWORKS=$(ip addr list docker0 | grep "inet " | cut -d" " -f6) | |
for NET in $NETWORKS; do | |
echo " $NET" | |
ip addr del $NET dev docker0 | |
done | |
echo "Adding new docker0 network" | |
ip addr add $NEW_IP dev docker0 | |
echo "Removing old iptables rules" | |
iptables -t nat -F POSTROUTING | |
iptables -F DOCKER | |
CURRENT_OPTS=$(cat $DOCKER_INIT | grep "^ *DOCKER_OPTS" | sed 's/^/ /g') | |
NEW_OPTS=DOCKER_OPTS=\"--bip=$NEW_IP\" | |
echo " " | |
if [ "$CURRENT_OPTS" != "" ]; then | |
TEMP_FILE="/tmp/docker_config.tmp" | |
grep -v "^ *DOCKER_OPTS" $DOCKER_INIT > $TEMP_FILE | |
echo " " >> $TEMP_FILE | |
echo DOCKER_OPTS=\"--bip=$NEW_IP\" >> $TEMP_FILE | |
cat $TEMP_FILE > $DOCKER_INIT | |
rm -f $TEMP_FILE | |
echo "WARNING: The existing DOCKER_OPTS were overwritten in $DOCKER_INIT:" | |
echo "Old:" | |
echo "$CURRENT_OPTS" | |
echo "New:" | |
echo " $NEW_OPTS" | |
else | |
echo " " >> $DOCKER_INIT | |
echo DOCKER_OPTS=\"--bip=$NEW_IP\" >> $DOCKER_INIT | |
echo "Success: $DOCKER_INIT has been modified." | |
fi | |
SYSTEMD_DOCKER_DIR="/etc/systemd/system/docker.service.d" | |
if [ "$INIT_SYSTEM" = "systemd" ]; then | |
echo "Configuring systemd to use /etc/default/docker" | |
if [ ! -d $SYSTEMD_DOCKER_DIR ]; then | |
mkdir -p $SYSTEMD_DOCKER_DIR | |
fi | |
OPTS='$DOCKER_OPTS' | |
cat << EOF > $SYSTEMD_DOCKER_DIR/debian-style-config.conf | |
# Enable /etc/default/docker configuration files with Systemd | |
[Service] | |
EnvironmentFile=-/etc/default/docker | |
ExecStart= | |
ExecStart=/usr/bin/dockerd -H fd:// $OPTS | |
EOF | |
fi | |
echo "" | |
echo "Restarting Docker" | |
case $INIT_SYSTEM in | |
sysv) | |
service docker restart | |
;; | |
systemd) | |
systemctl daemon-reload | |
systemctl restart docker.service | |
sleep 1 | |
systemctl status --lines=0 docker.service | |
;; | |
esac | |
echo "done." |
Thanks @evan-wu, that is indeed the correct way to configure it in newer versions of Docker - I'm going to add a warning for people still using this script to use the new method:
Create / edit /etc/docker/daemon.json
and set bip
:
{
"bip": "172.26.0.1/16"
}
The new method with daemon.json dont reset the ip addr list, when restarting systemctl networking. I also tried robooting the system, no chance. With the script ifconfig and ip addr list changed to new ip. Thanks.
Thanks for the script.
Hi, when I try to start up docker, I get the error as below
sudo systemctl start docker
Job for docker.service failed because the control process exited with error code.
See "systemctl status docker.service" and "journalctl -xe" for details.
And when I run- systemctl status docker.service, I get-
hostname profile.d yum.conf
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor pres>
Drop-In: /etc/systemd/system/docker.service.d
└─override.conf
Active: failed (Result: exit-code) since Mon 2020-11-02 06:41:38 UTC; 1min 3>
Docs: https://docs.docker.com
Process: 6095 ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --/containerd.sock --bip 192.168.5.1/24 (code=exited, status=1/FAILURE)
Main PID: 6095 (code=exited, status=1/FAILURE)
docker.service: Service RestartSec=2s expired, scheduling restart.
docker.service: Scheduled restart job, restart counter is at 3.
Stopped Docker Application Container Engine
docker.service: Start request repeated too quickly
docker.service: Failed with result 'exit-code'.
Failed to start Docker Application Container Engine
Fyi- I had recently created an override.conf in (/etc/systemd/system/docker.service.d) to override the subnet settings to ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --/containerd.sock --bip 192.168.5.1/24
Please help me start up docker
Hi @sanatthomas,
I'm not sure why Docker isn't starting for you, but it's probably a configuration problem. I would try run it manually like sudo /usr/bin/dockerd
to see if you get any more details that way. Anyway, this isn't a very good place to get help with Docker since it's just a random gist from 4 years ago.
You shouldn't need my Gist for a recent installation of Docker, this was mainly for older operating systems when I made this in 2016.
https://success.docker.com/article/how-do-i-configure-the-default-bridge-docker0-network-for-docker-engine-to-a-different-subnet