Search code examples
dockerdocker-composednsdocker-network

How to change the DNS server that is being used by the services in docker-compose?


UPDATE: For some reason simply adding dns: 1.1.1.1 to the docker-compose.yml under the service worked now but it didn't work when I tried it when I asked the question. SOLVED

My host uses PiHole as the DNS server so all docker containers inherit it. I have an app that generates a lot of DNS traffic that I don't need filtered through PiHole so I want to make that app use 1.1.1.1 DNS server.
I know there is a dns option that can be applied to each service but I found that it requires network_mode to be set to "bridge" which uses the default bridge network. However I want my containers to use custom DNS and still be on a separate isolated network (as the default network is) and not on the default bridge network so I can potentially have multiple apps (docker-compose.yml files) using a custom DNS servers while isolated from each other (like they are normally). How to achieve that?


Solution

  • You can add a custom dns for each service inside your docker-compose.yaml by adding

    version: 2
    services:
     application:
      dns:
       - <dns_server> #example: 8.8.8.8
    

    Last tried myself 2 years ago using version: 2