How to configure hostnames with domains in docker-compose.yml
?
Let's say the service worker
expects the service web
on the http://web.local/
address. But web.local
doesn't resolve to an ip address no matter what I configure using the hostname
directive. Adding an extra_hosts
directive doesn't work either as I should know the ip of the service web
for that, which I don't as it is assigned by docker.
docker-compose.yml:
version: '3'
services:
worker:
build: ./worker
networks:
- mynet
web:
build: ./web
ports:
- 80:80
hostname: web.local
networks:
- mynet
networks:
mynet:
but ping web.local
doesn't resolve inside the service worker
The default path for a Compose file is ./docker-compose.yml .
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. Then, with a single command, you create and start all the services from your configuration.
Run docker network ls to get the running networks names, and then docker network inspect NETWORK_NAME to see the containers in it. Look for the "Containers" keyword in the JSON, it is a list of connected devices. Look for the instance with the "IPv4Address": "127.0. 0.11/24" entry, the "Name" key is the DNS name.
According to docker-compose documentation, dns_search is for the purpose of configuring search domains. I am used to seeing this in context of transparently adding a domain to unqualified short domains.
For this to work you need to add an alias
in the network mynet
.
From the official documentation:
Aliases (alternative hostnames) for this service on the network. Other containers on the same network can use either the service name or this alias to connect to one of the service’s containers.
So, your docker-compose.yml
file should look like this:
version: '3'
services:
worker:
build: ./worker
networks:
- mynet
web:
build: ./web
ports:
- 80:80
hostname: web.local
networks:
mynet:
aliases:
- web.local
networks:
mynet:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With