Tamas
Tamas

Reputation: 3462

domain configuration in docker-compose

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

Upvotes: 26

Views: 42649

Answers (1)

Constantin Galbenu
Constantin Galbenu

Reputation: 17713

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:

Upvotes: 30

Related Questions