compose icon indicating copy to clipboard operation
compose copied to clipboard

Add option to remove service name as default alias on networks

Open randombk opened this issue 3 years ago • 14 comments

(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:

  • 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.

randombk avatar Mar 21 '21 07:03 randombk

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.

randombk avatar Mar 21 '21 07:03 randombk

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.

stale[bot] avatar Nov 09 '21 20:11 stale[bot]

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.

marekkon avatar Jan 29 '22 23:01 marekkon

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

stale[bot] avatar Jan 29 '22 23:01 stale[bot]

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.

stale[bot] avatar Jul 31 '22 02:07 stale[bot]

I'd like to keep this alive.

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

randombk avatar Jul 31 '22 03:07 randombk

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

stale[bot] avatar Jul 31 '22 03:07 stale[bot]

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?

theothermatt avatar Aug 26 '22 10:08 theothermatt

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.

anttivikman avatar Sep 21 '22 11:09 anttivikman

any workaround on this issue?

a-parser avatar Jul 30 '23 14:07 a-parser

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)...

timephy avatar Sep 01 '23 12:09 timephy