Fix Docker Containers Not Resolving Local DNS

Apr 19, 2025

I frequently run into issues with Docker containers misbehaving, particularly failing to resolve local DNS names. In this post, we’ll walk through how to manually configure DNS settings both in Docker Compose and at the Docker daemon level.

Why this happens

Docker uses its own DNS resolver inside containers, occasionally defaulting to Google DNS (8.8.8.8) or falling back to the host’s /etc/resolv.conf.

[-] Containers often bypass your LAN DNS resolver.
[-] .local and .lan names don’t resolve over public DNS.
[-] Docker’s bridge network doesn’t inherit your host’s full DNS settings.

Quick and dirty per container fix

Add your local DNS to the Docker Compose file:

services:
  myservice:
    image: myimage
    dns:
      - 192.168.1.1
      - 192.168.1.2

Alternativly for docker run you can use:
docker run --dns=192.168.1.1 myimage

Set Global Docker DNS

Modify or create /etc/docker/daemon.json:

{
  "dns": ["192.168.1.1", "192.168.1.2"]
}

Then restart docker:
sudo systemctl restart docker.service

Just need two containers to talk to eachother via hostname?

Just add them to the same docker network

For example with docker compose:

services:
  webapp1:
    image: web/app:1
    networks:
      - webapp_network

  webapp2:
    image: web/app:2
    networks:
      - webapp_network
      
networks:
  webapp_network:
    driver: bridge

Testing

You should now be able to force a lookup within the container using a similar command to:
docker exec -it <container_name> ping google.com