DNS Resolution errors in Docker containers
When first setting up Docker containers for the first time on a system, you may be able to build the hello-world container, pull images from Dockerhub, and even launch containers, but when you try to perform configuration that requires the container itself to reach out to external resources, it is unable to do so.
Take, for example, this simple Dockerfile, which utilizes the
httpd container image to host a simple flask app:
COPY ./FlaskApp/ /usr/local/apache2/htdocs
After building this container (
docker build -t flask_docker .), and then running a new container using this image (
docker run -it flask_docker -p 8080:80), after accessing the container entrypoint (in this case
/bin/sh or bash), when we try to test the external network capabilities of our container, we may receive an error like this:
Your initial instinct may be that a 'Temporary failure resolving' error means a DNS problem, and you would be correct. Typically,
dockerd, the daemon Docker uses to administer containers built using Docker, is responsible for acting as a DNS service for the containers it is responsible for. Docker will attempt to use the pre-configured DNS servers on your system to provide a seamless experience, but if that is not possible, you may need to manually configure them. to do, create a file in the
/etc/docker/ directory with the following values:
This JSON file configures the
dockerd process with the DNS values for Google's public DNS service (if you need to use a different set of DNS servers, update them in the JSON file). Once this is configured, the next time a container attempts to access external services, the Docker daemon will appropriately route traffic.