Created
October 13, 2016 11:43
-
-
Save jruusu/3b686e27649e321b8a950b4afc9ea2d2 to your computer and use it in GitHub Desktop.
Set docker dns options dynamically on a Ubuntu 16.04 (systemd) host
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# Set docker dns options dynamically on a Ubuntu 16.04 host | |
# | |
# See: | |
# https://github.com/docker/docker/issues/541 | |
# https://docs.docker.com/engine/admin/systemd/ | |
# http://stackoverflow.com/questions/33784295/setting-dns-for-docker-daemon-on-os-with-systemd | |
# | |
set -eui | |
if [ ! -d "/etc/systemd/system" ]; then | |
echo "Directory /etc/systemd/system not found. This may not be a systemd system. Cowardly exiting .." | |
exit 1 | |
fi | |
DOCKERD_CONF_DIR="/etc/systemd/system/docker.service.d" | |
DOCKERD_DNS_CONF_FILE="$DOCKERD_CONF_DIR/dockerd-dns.conf" | |
if [ ! -f "$DOCKERD_DNS_CONF_FILE" ]; then | |
echo "$DOCKERD_DNS_CONF_FILE not found, creating .." | |
sudo mkdir -p "$DOCKERD_CONF_DIR" | |
sudo touch "$DOCKERD_DNS_CONF_FILE" | |
fi | |
NEW_DNS_OPTS=$(nmcli device show | grep IP4.DNS | egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}" | sed -e 's/^/--dns /' | paste -s -d ' ') | |
NEW_CONF="[Service] | |
ExecStart= | |
ExecStart=/usr/bin/dockerd ${NEW_DNS_OPTS} -H fd://" | |
CURRENT_CONF=$(cat "$DOCKERD_DNS_CONF_FILE") | |
if [ "$CURRENT_CONF" == "$NEW_CONF" ]; then | |
echo "Docker DNS options untouched, exiting .." | |
exit 0 | |
else | |
echo "Updating $DOCKERD_DNS_CONF_FILE .." | |
echo "$NEW_CONF" | sudo tee "$DOCKERD_DNS_CONF_FILE" | |
echo "Reloading dockerd .." | |
sudo systemctl stop docker | |
sudo systemctl daemon-reload | |
sudo systemctl start docker | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment