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
andproject2_database_1
should be known by onshared_network
are the custom aliases provided. - From within
project1
:- The DNS for
database
should always point toproject1_database_1
over theproject1_app_internal
network - The DNS for
project1-database
should always point toproject1_database_1
over theshared_network
network - The DNS for
project2-database
should always point toproject2_database_1
over theshared_network
network
- The DNS for
- 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.