Docker Compose DNS entries
Sometimes you need some additional DNS entries in your containers. This is how to do it in compose.yaml.
Internal entries
Within the containers, the name of the container in compose.yaml is resolvable via DNS.
Given this compose.yaml:
services: web: # ... app: # ... networks: myapp: driver: "bridge"
We can ping web from within the app container.
If we need to access the web container using another domain, we add it as a network alias:
services: web: # ... networks: myapp: aliases: - appname.dev.myclient.com
Now, we can ping appname.dev.myclient.com from the app container and it will resolve to web.
External entries
Sometimes we want to resolve external domains that aren’t in the global DNS registry. To do this we add extra_hosts:
services: # ... app: # ... extra_hosts: - "app1.intranent.myclient.com:10.0.56.57" - "app2.intranent.myclient.com:10.0.56.58"
Now we can ping app1.intranent.myclient.com from within the app container and it will resolve to 10.0.56.57.