docker / compose

Define and run multi-container applications with Docker
https://docs.docker.com/compose/
Apache License 2.0
33.97k stars 5.22k forks source link

Add option to remove service name as default alias on networks #8223

Open randombk opened 3 years ago

randombk commented 3 years ago

(Copied from https://forums.docker.com/t/add-option-to-remove-service-name-as-default-alias-on-networks/106172 after I realized this was a compose-specific request)

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:

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.

randombk commented 3 years ago

In short, I'm looking for a flag that would toggle the logic in https://github.com/docker/compose/blob/5b7851f55b4ae9533f42eed282ffad7c442778f5/compose/service.py#L805-L810

into something that looks more like

 def _get_aliases(self, network, container=None): 
     return list( 
         ({container.short_id} if container else set()) | 
         set(network.get('aliases', ()) or [self.name]) 
     ) 

where it would only use the configured aliases if present and non-empty, and default to the service name if no custom aliases are provided.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

marekkon commented 2 years ago

I really like the idea of breaking the website name in definition from the host name on a shared network and this functionality would be very helpful.

I also have a second scenario where I would see this functionality. When I work on several branches using worktree, I wish I could switch to another branch without having to turn off the containers of the other branch. Currently, it is not possible, especially since the definition of these services is almost identical.

An additional, huge advantage would be the ability to define / overwrite these aliases in the override file.

stale[bot] commented 2 years ago

This issue has been automatically marked as not stale anymore due to the recent activity.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

randombk commented 2 years ago

I'd like to keep this alive.

This issue is still a problem on the latest release, and no effective solution is available.

stale[bot] commented 2 years ago

This issue has been automatically marked as not stale anymore due to the recent activity.

theothermatt commented 2 years ago

I'm seeing exactly this too, and it took me a while to track down, since I assumed that if I provided a list of aliases the automatically generated ones wouldn't be present. Is there anywhere in the documentation that states that the generated aliases will still be added in addition to the ones specified explicitly?

anttivikman commented 2 years ago

While there is a workaround for this "just don't use same names". It's annoying since it requires you to be aware of the names used in different stacks (when you have generic router that routes traffic domain based to multiple stacks). This seems like relatively small development effort, that would significantly ease up our DevOps. I'm talking about use case in Swarm mode.

a-parser commented 1 year ago

any workaround on this issue?

timephy commented 1 year ago

Hey, i am facing the same issue. I guess the easiest workaround for me would be using a generic service name, and then alias it to a specific name in the internal network (which will hopefully never appear in the public network)...

tmo1 commented 1 month ago

I just ran into this - see my write-up at Stack Overflow. I certainly think that the feature requested here and / or better documentation and warnings would be desirable.

crossdot commented 3 weeks ago

Just got the same problem

bd781n commented 2 weeks ago

version: '3'

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

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