How to define DNS in Docker containers

2 years ago 360

Jack Wallen shows you however to configure circumstantial DNS servers for your Docker instrumentality deployments.

shutterstock-coder-roman-samborskyi-med-copy.jpg

Image: Shutterstock/roman-Samborskyi

When you deploy a container connected your network, if it cannot find a DNS server defined successful /etc/resolv.conf, by default it volition instrumentality connected the DNS configured for the big machine. That whitethorn beryllium good and dandy for definite situations. But what if (maybe for information reasons), you bash not privation your containers utilizing the aforesaid DNS arsenic your hosts. Say, for example, your big servers usage a circumstantial DNS server to forestall users from visiting peculiar sites. Or possibly you person antithetic DNS configurations for VPNs.

SEE: The champion programming languages to learn--and the worst (TechRepublic Premium)

There mightiness beryllium respective reasons you don't privation your containers utilizing the aforesaid DNS arsenic their hosts. So, what bash you do? How bash you specify DNS for Docker containers, specified that it won't prime up the DNS of the hosting machine?

It's really rather simple. Let maine amusement you how.

What you'll request

To marque this work, you'll request a instrumentality moving the Docker engine. I'll beryllium demonstrating connected Ubuntu Server 20.04, but the hosting level doesn't matter, truthful agelong arsenic you person Docker moving and tin deploy containers. You'll besides privation to person a idiosyncratic that's a subordinate of the docker radical (so you're not deploying arsenic either the basal idiosyncratic oregon with sudo, some of which are a information issue). With those things astatine the ready, let's deploy.

How to deploy a instrumentality with DNS pre-configured

I'm going to amusement you however to deploy Docker containers with DNS pre-configured. The archetypal method volition usage the docker bid and the 2nd volition beryllium via Docker Compose.

Let's accidental you privation to deploy a Ubuntu container, named ubuntuDNS, with the superior Cloudflare DNS server of 1.0.0.1. That bid would be:

docker tally -d -t --name ubuntuDNS --dns="1.0.0.1" ubuntu

You could besides deploy that instrumentality with a superior and secondary DNS similar so:

docker tally -d -t --name ubuntuDNS --dns="1.0.0.1" --dns="1.1.1.1" ubuntu

Let's marque definite the instrumentality honors our DNS configuration. For that, entree the ammunition of the instrumentality with the command:

docker exec -it ubuntuDNS bash

From the shell, contented the command:

cat /etc/resolv.conf

You should spot the DNS server(s) you configured from the bid enactment (Figure A).

Figure A

dockerdnsa.jpg

Our Cloudflare DNS servers person been successfully configured.

Exit from the ammunition with the exit command.

Now we'll bash the same, utilizing Docker Compose. Of course, you person to person this bid installed, which tin beryllium done with the following:

wget https://github.com/docker/compose/releases/download/1.29.2/docker-compose-Linux-x86_64 chmod u+x docker-compose-Linux-x86_64 sudo mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose

With Docker Compose installed, you tin present make the indispensable record with the command:

nano docker-compose.yaml

In that file, let's deploy a rabbitmq (an open-source connection broker) instrumentality that volition usage a CloudFlare DNS entry, which volition look like:

version: '3' services: service: dns: - "1.0.0.1" - "1.1.1.1" network_mode: "bridge" image: rabbitmq:3-management container_name: rabbitmq hostname: rabbitmq ports: - "15672:15672"

Note: Without the network_mode enactment acceptable to "bridge" the DNS introduction volition not work. 

Save and adjacent the file. Deploy the instrumentality with the command:

docker-compose up

The instrumentality volition deploy. Open a caller SSH transportation to the hosting server (as the deployment won't instrumentality your prompt) and past entree the ammunition of the rabbitmq instrumentality with the command:

docker exec -it rabbitmq bash

Once wrong the container, presumption the DNS entries with the command:

cat /etc/resolv.conf

You should spot 1.0.0.1 and 1.1.1.1 listed (Figure B).

Figure B

dockerdns-b.jpg

Our WordPress deployment is utilizing the due DNS entry.

And that's each determination is to defining DNS successful your Docker containers. Why you would usage this volition beryllium connected your needs, but having this diagnostic astatine the acceptable tin beryllium rather handy. 

Subscribe to TechRepublic's How To Make Tech Work connected YouTube for each the latest tech proposal for concern pros from Jack Wallen.

Developer Essentials Newsletter

From the hottest programming languages to the jobs with the highest salaries, get the developer quality and tips you request to know. Weekly

Sign up today

Also spot

Read Entire Article