Skip to content

Instantly share code, notes, and snippets.

@JPvRiel
Last active March 14, 2025 14:17
Show Gist options
  • Save JPvRiel/dcb9e2866a9d0aa19042028cca3306c7 to your computer and use it in GitHub Desktop.
Save JPvRiel/dcb9e2866a9d0aa19042028cca3306c7 to your computer and use it in GitHub Desktop.
Ubuntu, NetworkManager and Docker DNS workaround

Docker issues are frequently logged for DNS resolution in containers because it doens't inhert or get values for DNS from NetworkManager which leverages a built in dnsmasq to inteligently manage DNS.

Perminant workarround

sudo bash -c "echo listen-address=$(ip -4 addr show dev docker0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}') > /etc/NetworkManager/dnsmasq.d/docker-bridge"
sudo systemctl reload NetworkManager
sudo bash -c 'echo -e "{\n\t\"dns\": [\"$(ip -4 addr show dev docker0 | grep -oP "(?<=inet\s)\d+(\.\d+){3}")\"]\n}" > /etc/docker/daemon.json'
sudo systemctl restart docker

Note:

  • makes dnsmasq plugin for network manager listen on the host's docker bridge interface
  • adds (clobbers!) the daemon.json - take care, could overwrite other customisations you already have there...

Per run workarround

The bash one liner below generates the dns attributes needed for docker

nm_dns=$(for d in $(nmcli device show | grep -E "^IP4.DNS" | grep -oP '(\d{1,3}\.){3}\d{1,3}'); do echo -n " --dns $d"; done)
sudo -E docker run -it --rm -e http_proxy -e https_proxy -e no_proxy $nm_dns ubuntu

Ain't pretty, but works... (until they change the nmcli output format or something)

Related Issues

Related issues with docker DNS:

@marcelgsteiger
Copy link

marcelgsteiger commented Dec 10, 2024

For adding/modifying the dns entry in /etc/docker/daemon.json and keeping everything else untouched in this file, I'm using jq and sponge as follows:

DNS=$(ip -4 addr show dev docker0 | grep -oP "(?<=inet\s)\d+(\.\d+){3}"); cat /etc/docker/daemon.json | jq ". + {\"dns\": [\"$DNS\"]}" | sponge /etc/docker/daemon.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment