How to define DNS in Docker containers

Jack Wallen exhibits you how to configure specific DNS servers for your Docker container deployments.

Graphic: Shutterstock/roman-Samborskyi

When you deploy a container on your network, if it can’t uncover a DNS server outlined in /etc/resolv.conf, by default it will just take on the DNS configured for the host machine. That could be good and dandy for particular cases. But what if (perhaps for security reasons), you do not want your containers employing the exact DNS as your hosts. Say, for illustration, your host servers use a unique DNS server to reduce buyers from visiting specific web sites. Or possibly you have distinctive DNS configurations for VPNs.

SEE: The best programming languages to master–and the worst (TechRepublic Premium)

There may possibly be quite a few good reasons you will not want your containers using the identical DNS as their hosts. So, what do you do? How do you determine DNS for Docker containers, such that it won’t pick up the DNS of the internet hosting machine?

It really is truly very straightforward. Permit me show you how.

What you will require

To make this work, you will require a machine working the Docker motor. I am going to be demonstrating on Ubuntu Server 20.04, but the internet hosting system will not make a difference, so extended as you have Docker working and can deploy containers. You’ll also want to have a person that is a member of the docker team (so you might be not deploying as both the root consumer or with sudo, both of which are a security difficulty). With individuals factors at the prepared, let us deploy.

How to deploy a container with DNS pre-configured

I’m heading to show you how to deploy Docker containers with DNS pre-configured. The very first strategy will use the docker command and the 2nd will be by means of Docker Compose.

Let us say you want to deploy a Ubuntu container, named ubuntuDNS, with the key Cloudflare DNS server of 1…1. That command would be:

docker operate -d -t --title ubuntuDNS --dns="1...1" ubuntu

You could also deploy that container with a primary and secondary DNS like so:

docker run -d -t --identify ubuntuDNS --dns="1...1" --dns="" ubuntu

Let us make guaranteed the container honors our DNS configuration. For that, accessibility the shell of the container with the command:

docker exec -it ubuntuDNS bash

From the shell, situation the command:

cat /etcetera/resolv.conf

You really should see the DNS server(s) you configured from the command line (Figure A).

Determine A


Our Cloudflare DNS servers have been effectively configured.

Exit from the shell with the exit command.

Now we are going to do the exact, making use of Docker Compose. Of system, you have to have this command set up, which can be finished with the next:

chmod u+x docker-compose-Linux-x86_64
sudo mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose

With Docker Compose set up, you can now develop the required file with the command:

nano docker-compose.yaml

In that file, let’s deploy a rabbitmq (an open up-source information broker) container that will use a CloudFlare DNS entry, which will seem like:

variation: '3'
      - "1...1"
           - ""
    network_mode: "bridge"
    image: rabbitmq:3-management
    container_title: rabbitmq
    hostname: rabbitmq
      - "15672:15672"

Take note: Without the network_manner possibility established to “bridge” the DNS entry will not get the job done. 

Help you save and shut the file. Deploy the container with the command:

docker-compose up

The container will deploy. Open a new SSH connection to the web hosting server (as the deployment will not return your prompt) and then accessibility the shell of the rabbitmq container with the command:

docker exec -it rabbitmq bash

At the time inside of the container, watch the DNS entries with the command:

cat /and so on/resolv.conf

You should really see 1…1 and detailed (Determine B).

Determine B


Our WordPress deployment is applying the correct DNS entry.

And which is all there is to defining DNS in your Docker containers. Why you would use this will rely on your demands, but getting this element at the all set can be rather useful. 

Subscribe to TechRepublic’s How To Make Tech Do the job on YouTube for all the hottest tech tips for small business professionals from Jack Wallen.

Also see

Fibo Quantum