Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 789
Tamas Avatar asked Apr 19 '18 05:04

Tamas


People also ask

Where is the Docker compose config file?

The default path for a Compose file is ./docker-compose.yml .

What is Docker compose config?

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.

How do I find my Docker DNS?

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.

What is Dns_search Docker compose?

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.


Video Answer


1 Answers

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:
like image 151
Constantin Galbenu Avatar answered Oct 18 '22 06:10

Constantin Galbenu