Add option to remove service name as default alias on networks

Edit: Created request in the docker-compose github repo: Add option to remove service name as default alias on networks · Issue #8223 · docker/compose · GitHub

I am facing an issue where two separate docker compose projects sharing a common externally defined docker network would encounter DNS name collisions when both projects have a service with the same name.

Example Setup:

project1/docker-compose.yml:

version: '3'

networks:
  app_internal:
    driver: bridge
  shared:
    external:
      name: shared_network

services:
  database:
    ...
    networks:
      app_internal:
      shared:
        aliases:
          - project1-database

project2/docker-compose.yml:

version: '3'

networks:
  app_internal:
    driver: bridge
  shared:
    external:
      name: shared_network

services:
  database:
    ...
    networks:
      app_internal:
      shared:
        aliases:
          - project2-database

Problem:

shared_network is an externally created docker network common to both project1 and project2. When I spin both projects up, both the project1_database_1 and project2_database_1 containers are aliased to database on shared_network, in addition to the custom aliases provided.

Making things worse, when I try to communicate with database on other containers in project1 or project2 who are also connected to shared_network, it’s a tossup whether I’ll be talking with project1_database_1 or project2_database_1 .

Desired Outcome for Example:

  • The only aliases project1_database_1 and project2_database_1 should be known by on shared_network are the custom aliases provided.
  • From within project1:
    • The DNS for database should always point to project1_database_1 over the project1_app_internal network
    • The DNS for project1-database should always point to project1_database_1 over the shared_network network
    • The DNS for project2-database should always point to project2_database_1 over the shared_network network
  • Ditto for project2

Feature Request:

Currently, the problematic behavior occurs because of this interaction in the Docker-compose documentation:

Other containers on the same network can use *either* the service name or this alias to connect to one of the service’s containers. (emphasis mine)

I would like to have an option at either a service level or a network level to tell Docker Compose not to add the service name as an alias when attaching a network.

Hi dlpnet
I am facing similar issue with a docker swarm config consisting of same service names in different stacks. But they have to communicate so if I place them on an overlay network, I face the same issue.

Please let me know if you have any work around for this.

My hacky workaround was to fork compose and implement the change myself: Implement #8223 · randombk/compose@5249eb6 · GitHub