Skip to content

Instantly share code, notes, and snippets.

@IslamAlam
Created May 25, 2019 12:15
Show Gist options
  • Save IslamAlam/0c9e9dc42126d60cfed66f4779fd8047 to your computer and use it in GitHub Desktop.
Save IslamAlam/0c9e9dc42126d60cfed66f4779fd8047 to your computer and use it in GitHub Desktop.
**1\. Hardcode DNS server in docker daemon.json**
* Edit `/etc/docker/daemon.json`
{
"dns": ["10.1.2.3", "8.8.8.8"]
}
* Restart the docker daemon for those changes to take effect:
`sudo systemctl restart docker`
* Now when you run/start a container, docker will populate `/etc/resolv.conf` with the values from `daemon.json`.
* * *
**2\. Fix the hosts's `/etc/resolv.conf`**
**A. Ubuntu 16.04 and earlier**
* For Ubuntu 16.04 and earlier, `/etc/resolv.conf` was dynamically generated by NetworkManager.
* Comment out the line `dns=dnsmasq` (with a `#`) in `/etc/NetworkManager/NetworkManager.conf`
* Restart the NetworkManager to regenerate `/etc/resolv.conf` :
`sudo systemctl restart network-manager`
* Verify on the host: `cat /etc/resolv.conf`
**B. Ubuntu 18.04 and later**
* Ubuntu 18.04 changed to use [`systemd-resolved` to generate `/etc/resolv.conf`](http://manpages.ubuntu.com/manpages/bionic/man8/systemd-resolved.service.8.html#contenttoc3). Now by default it uses a local DNS cache 127.0.0.53\. That will not work inside a container, so Docker will default to Google's 8.8.8.8 DNS server, which may break for people behind a firewall.
* `/etc/resolv.conf` is actually a symlink (`ls -l /etc/resolv.conf`) which points to `/run/systemd/resolve/stub-resolv.conf` (127.0.0.53) by default in Ubuntu 18.04.
* Just change the symlink to point to `/run/systemd/resolve/resolv.conf`, which lists the real DNS servers:
`sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf`
* Verify on the host: `cat /etc/resolv.conf`
Now you should have a valid `/etc/resolv.conf` on the host for docker to copy into the containers.
## Install Docker
'
sudo apt-get install bridge-utils
sudo pkill docker
sudo iptables -t nat -F
sudo ifconfig docker0 down
sudo brctl delbr docker0
sudo service docker restart
'
@IslamAlam
Copy link
Author

IslamAlam commented May 25, 2019

1. Hardcode DNS server in docker daemon.json

  • Edit /etc/docker/daemon.json

    {
        "dns": ["10.1.2.3", "8.8.8.8"]
    }
    
  • Restart the docker daemon for those changes to take effect:
    sudo systemctl restart docker

  • Now when you run/start a container, docker will populate /etc/resolv.conf with the values from daemon.json.


2. Fix the hosts's /etc/resolv.conf

A. Ubuntu 16.04 and earlier

  • For Ubuntu 16.04 and earlier, /etc/resolv.conf was dynamically generated by NetworkManager.

  • Comment out the line dns=dnsmasq (with a #) in /etc/NetworkManager/NetworkManager.conf

  • Restart the NetworkManager to regenerate /etc/resolv.conf :
    sudo systemctl restart network-manager

  • Verify on the host: cat /etc/resolv.conf

B. Ubuntu 18.04 and later

  • Ubuntu 18.04 changed to use systemd-resolved to generate /etc/resolv.conf. Now by default it uses a local DNS cache 127.0.0.53. That will not work inside a container, so Docker will default to Google's 8.8.8.8 DNS server, which may break for people behind a firewall.

  • /etc/resolv.conf is actually a symlink (ls -l /etc/resolv.conf) which points to /run/systemd/resolve/stub-resolv.conf (127.0.0.53) by default in Ubuntu 18.04.

  • Just change the symlink to point to /run/systemd/resolve/resolv.conf, which lists the real DNS servers:
    sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf

  • Verify on the host: cat /etc/resolv.conf

Now you should have a valid /etc/resolv.conf on the host for docker to copy into the containers.

Install Docker

https://docs.docker.com/install/linux/docker-ce/ubuntu/

sudo apt-get update
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common -y

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io -y
sudo docker run hello-world

Manage Docker as a non-root user

  • Create the docker group.

     sudo groupadd docker
    
  • Add your user to the docker group.

    sudo usermod -aG docker $USER
    
  • Log out and log back in so that your group membership is re-evaluated.

    If testing on a virtual machine, it may be necessary to restart the virtual machine for changes to take effect.

    On a desktop Linux environment such as X Windows, log out of your session completely and then log back in.

  • Verify that you can run docker commands without sudo.

    $ docker run hello-world
    

    This command download

Docker containers can't resolve DNS on Ubuntu 14.04 Desktop Host

Woo, I found a post on github that solved my problem.

After Steve K. pointed out that it wasn't actually a DNS issue and was a connectivity issue, I was able to find a post on github that described how to fix this problem.

Apparently the docker0 network bridge was hung up. Installing bridge-utils and running the following got my Docker in working order:

sudo apt-get install bridge-utils -y
sudo pkill docker
sudo iptables -t nat -F
sudo ifconfig docker0 down
sudo brctl delbr docker0
sudo service docker restart

@IslamAlam
Copy link
Author

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