sclarke81
sclarke81

Reputation: 1769

Docker container to container DNS with containerised DNS server

I'm having difficulty figuring out how to get the DNS working for container to container communication. I have dnsmasq and Traefik as well as a bunch of other services. The below docker-compose.yml is a trimmed down version to illustrate my setup.

Essentially I think I want to direct containers and external traffic looking for *.domain.test to the Traefik container. I've gone round in circles looking for an elegant way to do this. Any help is much appreciated.

After bringing the stack up I run:

docker run --rm --network=traefik praqma/network-multitool dig domain.test

and get:

; <<>> DiG 9.14.8 <<>> domain.test
;; global options: +cmd
:: connection timed out; no servers could be reached

docker-compose.yml

version: "3.7"

networks:
  dns: {}
  traefik:
    name: traefik

volumes:
  heimdall-data:

services:
  dnsmasq:
    cap_add:
    - NET_ADMIN
    command:
    - --cache-size=1000
    - --local-ttl=10
    - --address=/.domain.test/192.168.1.10
    - --address=/#/0.0.0.0
    container_name: dnsmasq
    image: andyshinn/dnsmasq:2.78
    labels:
      traefik.enable: "False"
    networks:
      dns: null
    ports:
    - "53:53/tcp"
    - "53:53/udp"
    restart: unless-stopped

  traefik:
    command:
    - --api.insecure=true
    - --providers.docker=true
    - --providers.docker.exposedbydefault=true
    - --entrypoints.web.address=:80
    - --log.level=ERROR
    container_name: traefik
    image: traefik:2.1
    labels:
      traefik.enable: "true"
      traefik.http.routers.api.entrypoints: web
      traefik.http.routers.api.rule: Host(`traefik.domain.test`)
      traefik.http.routers.api.service: api@internal
    networks:
      traefik: null
    ports:
    - 80:80
    - 443:443
    restart: unless-stopped
    volumes:
    - /var/run/docker.sock:/var/run/docker.sock:ro

  heimdall:
    container_name: heimdall
    image: linuxserver/heimdall:2.2.2-ls66
    labels:
      traefik.docker.network: traefik
      traefik.http.routers.heimdall.entrypoints: web
      traefik.http.routers.heimdall.rule: Host(`www.domain.test`, `domain.test`)
    networks:
      traefik: null
    restart: unless-stopped
    volumes:
    - heimdall-data:/config:rw


Upvotes: 1

Views: 1149

Answers (1)

mohammad asiyabanii
mohammad asiyabanii

Reputation: 43

when you run your container with --net or --network docker will use a self DNS server to discover services that run with it. and all DNS queries sent to the docker engine.

in normal time (when using default network) you can use the --dns option on your run command but when you run your images with a self-defined network you must add your customize resolv. conf file to the container. you make an on your system and name it custom-resolv.conf.

at this file, you can add your custom dns address and add it to your container.

nameserver 1.0.0.1
nameserver 4.2.2.4

then you must add this small part to your run command:

-v /path/to/file/custom-resolv.conf:/etc/resolv.conf:ro
docker run --rm --network=traefik -v /path/to/file/custom-resolv.conf:/etc/resolv.conf:ro praqma/network-multitool dig domain.test

use ro for read-only volume mapping. ven your container started you can exec to your container and check /etc/resolv.conf file that must be the same as custom-resolv.conf

now all your DNS requests are sent to your DNS server

Upvotes: 1

Related Questions