0

I have container1, I have edited the /etc/resolv.conf and put nameserver 8.8.8.8 to ping google.com. It works fine. But I need to make this permanent, when I restart my docker's container using

sudo docker restart container1

I can't ping google.com anymore through my container.

Note:

I have tried what they have posted from 3-7 years ago on here. It doesn't work. My Ubuntu release is 18.04.

closed as unclear what you're asking by G-Man, Rui F Ribeiro, Thomas, RalfFriedl, Stephen Harris Dec 2 '18 at 3:57

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1

As explained under DNS services, a container inherits the resolv.conf file from the host by default.

[root@testvm ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.1.1

[root@testvm ~]# docker run -it ubuntu bash

root@a0a0d88f6db8:/# mount|grep resolv.conf
/dev/mapper/centos-root on /etc/resolv.conf type xfs (rw,relatime,seclabel,attr2,inode64,noquota)

root@79c567a830b4:/# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.1.1

To add the DNS nameserver configuration inside the container, you then have two options:

  1. Add the DNS nameserver(s) to the resolv.conf file on the host.
  2. Start the container with one or more --dns options, explicitly specifying the DNS nameservers.

Here is the --dns option in use:

[root@testvm ~]# docker run -it --dns 8.8.8.8 --dns 8.8.4.4 ubuntu bash
root@3ae155186997:/# cat /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4

This option also persists across a restart, as demonstrated with an nginx image below:

[root@testvm ~]# docker run -d -p 8080:80 --dns 8.8.8.8 --dns 8.8.4.4 nginx
97f76bedfad8173bb38e4e83f898b61be6c8587775736576d09c94ebaf5d5d36

[root@testvm ~]# docker exec -it 97f76bedfad8173bb38e4e83f898b61be6c8587775736576d09c94ebaf5d5d36 bash

root@97f76bedfad8:/# cat /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4

root@97f76bedfad8:/# exit
exit

[root@testvm ~]# docker restart 97f76bedfad8173bb38e4e83f898b61be6c8587775736576d09c94ebaf5d5d36
97f76bedfad8173bb38e4e83f898b61be6c8587775736576d09c94ebaf5d5d36

[root@testvm ~]# docker exec -it 97f76bedfad8173bb38e4e83f898b61be6c8587775736576d09c94ebaf5d5d36 bash
root@97f76bedfad8:/# cat /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4

Not the answer you're looking for? Browse other questions tagged or ask your own question.