1

I've an application that uses custom hosts. For example user1.app user2.app user3.app.

In a traditional dev environment running on my computer I add all those entries to /etc/hosts file pointing to 127.0.0.1

For automatic testing purpose I need that a single docker container is referenced with multiple host using docker-compose.

On docker-compose each service receive a hostname as the service name and I can ping to other containers using the service name, but how I can give a container multiple hostnames?

The --add-host or extra_host doesn't seems to be the solution because I don't know the docker ip address that will be used for that service.

Andre
  • 1,042
  • 12
  • 20
Arnold Roa
  • 7,335
  • 5
  • 50
  • 69

1 Answers1

1

The solution is to:

  • Have same container on the same network
  • Use aliases setting to add more hosts to the service.

on the first docker-compose.yml file

services:
  one:
    ...
    networks:
      - my-network

networks:
  my-network:
    driver: bridge
    name: my-network # This is to not prefix the project name (directory_name)

on the other docker-compose.yml file:

services:
  two:
    networks:
      my-network:
        aliases:
          - dos
          - bbb
networks:
  my-network:
    external: true # it was created by other compose file

this way service one can access to service two by using any of this host:

  • two
  • dos
  • bbb

This can be tested with:

$ docker-compose run one ping two
$ docker-compose run one ping dos
$ docker-compose run one ping bbb

All should return pings reply.

More on this here: https://docs.docker.com/compose/compose-file/compose-file-v3/#aliases

Arnold Roa
  • 7,335
  • 5
  • 50
  • 69