Add Dns To Docker Container

Add Dns To Docker Container

Reading time1 min
#Docker#Networking#Containers#DNS#DockerCompose#DevOps

How to Seamlessly Add Custom DNS Settings to Docker Containers for Reliable Network Resolution

Most developers overlook Docker's default DNS setup, inadvertently causing intermittent network failures. This post challenges that norm by demonstrating precise techniques to override and customize DNS settings, ensuring your containers always resolve the right addresses swiftly and reliably.


Why Custom DNS Matters in Docker Containers

When you spin up a container, Docker automatically configures DNS for it using the host’s DNS or the Docker daemon’s default settings. While this often works well for simple applications, complex or multi-container architectures frequently encounter DNS resolution issues:

  • Containers struggling to reach internal services by hostname
  • Slow or inconsistent name resolution
  • Failures due to incorrect or incomplete DNS server configurations

Properly configuring custom DNS servers inside your containers helps avoid these pitfalls — providing consistent name resolution and safer network communication.


How Docker Handles DNS by Default

By default, Docker copies the /etc/resolv.conf file from the host machine into each container. This usually includes host network’s DNS servers (e.g., your ISP or corporate resolvers). This works fine unless:

  • Your external DNS doesn’t resolve your internal domain names (e.g., my-service.local)
  • You need to route queries to internal caching servers or private DNS zones
  • Your infrastructure requires specifying multiple fallback DNS servers for redundancy

Step-by-Step: Adding Custom DNS Settings to Docker Containers

Docker provides straightforward options to override and add custom DNS entries when running containers.

1. Using --dns flag in docker run

You can specify one or more custom DNS servers using the --dns flag:

docker run --dns 8.8.8.8 --dns 8.8.4.4 -it ubuntu bash

Inside this container’s /etc/resolv.conf, you’ll find:

nameserver 8.8.8.8
nameserver 8.8.4.4

This overrides the host’s default DNS and forces the container to use Google’s public DNS servers explicitly.

2. Adding Search Domains with --dns-search

Sometimes you want a search domain appended automatically when using short names (e.g., resolving myservice as myservice.mycompany.local):

docker run --dns-search mycompany.local -it ubuntu bash

Check /etc/resolv.conf inside the container shows:

search mycompany.local
nameserver <default_or_custom_dns>

You can combine --dns and --dns-search for greater control:

docker run --dns 10.0.0.10 --dns-search mycompany.local -it ubuntu bash

3. Configuring Custom DNS in docker-compose.yml

If you use Docker Compose, adding custom DNS settings is simple under the relevant service:

version: '3'
services:
  app:
    image: myapp:latest
    dns:
      - 10.0.0.10
      - 8.8.8.8
    dns_search:
      - mycompany.local

Run it via:

docker-compose up -d

This ensures every time the service spins up, it uses your specified name servers.


Bonus Tip: Configure Daemon-Wide Custom DNS Settings

If you want all containers on a host to inherit certain custom DNS servers without specifying them repeatedly per container, configure your Docker daemon’s settings.

Edit (or create) /etc/docker/daemon.json:

{
  "dns": ["10.0.0.10", "8.8.4.4"]
}

Then restart Docker:

sudo systemctl restart docker

Now, every container launched will automatically use these overrides unless explicitly set otherwise.


Troubleshooting & Verification

Once you add custom DNS settings:

  1. Enter your running container shell.

  2. Check /etc/resolv.conf and confirm nameservers are set as expected.

  3. Use tools like nslookup, dig, or ping for hostname resolution tests:

nslookup myservice.mycompany.local
dig @10.0.0.10 myservice.mycompany.local

ping google.com

If resolution fails, ensure that:

  • Your custom nameservers are reachable from inside the container network.
  • Firewall or security groups don’t block UDP/TCP on port 53.
  • The domain suffix matches correctly with your search domains.

Summary

Misconfigured or overlooked default DNS can bring down container networks intermittently and cause frustrating debugging sessions.

Using these simple but effective methods — from passing flags at runtime (--dns, --dns-search), updating compose files, or setting daemon-wide defaults — lets you take complete control over how your containers resolve domain names.

With reliable name resolution ensured by proper custom DNS configs, your multi-container deployments will be more resilient and easier to maintain.


Try this approach today on your next project requiring steady network communication across containers — you’ll instantly save time chasing elusive connectivity bugs!