Edit /etc/resolv.conf in Docker container to fix 'DNS resolution is unavailable'

If you are running docker with docker-compose, first run:

sudo docker exec -it pi-hole /bin/bash

Where pi-hole is the name of your container.

Do:

cat /etc/resolv.conf

Make note of everything in this file, or copy and paste.

Create a resolv.conf file in ypur docker-compose.yml directory and paste it in. Change nameserver to 127.0.0.1

Add a volume in docker-compose pointing this file to /etc/resolv.conf

Should look like:

 - './resolv.conf:/etc/resolv.conf'

Restart the container.

Exec back into the container and try an apt update, if it works you should be good.

7 Likes

Thanks a lot for this, worked for me as well.

Just for future readers: the docker[-compose] required rebuilding of the container so the new volume is picked up:

docker-compose up --detach --remove-orphans --build pihole

After that the new /etc/resolv.conf was in place.

Though I also want to voice disappointment here, this should have been the default and as a programmer I am so burned out that I don't even wanna know why is it not the default.

By default, Docker container's would use DNS servers as defined in the host system's /etc/resolv.conf, either directly by copying the file into a container or indirectly via Docker's internal DNS resolver using the nameservers from /etc/resolv.conf as upstreams, depending on a container's network mode.

If required, DNS options should be passed to a container using a variety of dns(-xxx) flags.
See Docker's networking documentation on DNS services for details.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.