Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Stop using sequential container numbers #1516

Closed
aanand opened this issue Jun 5, 2015 · 25 comments
Closed

Proposal: Stop using sequential container numbers #1516

aanand opened this issue Jun 5, 2015 · 25 comments

Comments

@aanand
Copy link

aanand commented Jun 5, 2015

Sequential container numbers (myapp_web_1, myapp_web_2) are problematic.

  1. They're costly to maintain: we have to query the names of all containers for a service to determine what the next number should be. As apps get bigger, this is going to get worse.
  2. They're error-prone: if a Swarm node with myapp_web_LASTNUM goes down at the same time as Compose is querying the cluster, it will incorrectly think LASTNUM is the next number we should use. Once the node comes up, there'll be a clash of container names.
  3. They make it impossible to do multiple docker-compose runs in parallel.

As such, I think we should start using random suffixes. This may well break some scripts that rely on container names, but now that we're using labels to track containers, we should be explicit about making no guarantees about container names.

@ghost
Copy link

ghost commented Jun 13, 2015

For scripts is useful ps command with -q option. If there is option for get my only one container there would be no need for some explicit naming.

@SBoudrias
Copy link

I just wondered what were the plans for this feature? Is it planned soon?

@dnephin
Copy link

dnephin commented Oct 13, 2015

Hey Simon!

We aren't doing anything about this in the 1.5 release (which will be out soon), but I think we may look to implement something in the 1.6 release which will be a few months away.

@murphyke
Copy link

+1 I want to run parallel copies of a container in a data processing pipeline. If there were a --container-name=NAME CLI flag, I would be in business, but random suffixes would work also.

@ibizaman
Copy link

Another use case where this would be useful is if the prefix - the directory of the docker-compose.yml - is the same and the same service name is used.

For example, having two apps with the following structure:

myapp/
    doc/
    src/
        docker-compose.yml
myapp2/
    doc/
    src/
        docker-compose.yml

and assuming they both use a mysql service, this will result in two identically named services src_mysql_1.

A not so great solution would be to use the complete $PWD as a prefix.
But in fact, why do we even need to name the machines in the first place? This is not a troll question. :)

EDIT: I just found out about the --projectname command line option which solves my particular problem. Although the question about why should we even give a name still stands.

@dnephin
Copy link

dnephin commented Feb 20, 2016

The project name is required to support isolated environments.

@caiofbpa
Copy link

caiofbpa commented Apr 6, 2016

👍

@rvernica
Copy link

@dnephin, just wondering, are you still planning to remove the sequential container numbers?

@kchudy
Copy link

kchudy commented Mar 3, 2017

I just came across this issue when CI executes tests in parallel (triggered by different commits). I handled it by generating a random name for the container like so:

docker-compose run --rm --no-deps --name `uuidgen` backend python3 ./tools/manage.py test

@EspadaV8
Copy link

EspadaV8 commented Jul 4, 2017

Thanks @kchudy . I had to use cat /proc/sys/kernel/random/uuid instead of uuidgen since that wasn't available on my CI server so the full command looks something like

docker-compose \
  -f docker-compose.yml \
  -f docker-compose.override.yml \
  -f docker-compose.development.yml \
  run --rm \
  --name `cat /proc/sys/kernel/random/uuid` \
  dev composer install --no-progress --no-ansi --no-scripts

@shin-
Copy link

shin- commented Oct 6, 2017

Related: #4688

@ghost
Copy link

ghost commented Oct 6, 2017

Is pure naive randomness enough to solve the parallel docker-compose run problem though? This could still lead to races, it just makes them more unlikely.

IMHO a mechanism is needed in addition to query a number for being free & reserve it in one atomic step. This should be less work performance-wise than going through all numbers to find the next higher one as of now, but it's still more complex than simply throwing a dice and using that number without any further considerations. However, I guess it begs the question who keeps track of the reservations - something in the file system? The docker daemon? ..

@shin-
Copy link

shin- commented Oct 7, 2017

@Jonast That's just not an issue in practice: https://en.wikipedia.org/wiki/Universally_unique_identifier#Collisions

@ghost
Copy link

ghost commented Oct 7, 2017

@shin- true if you use UUIDs. This wasn't confirmed before, and I kind of thought you might use a random 64bit int or similar. In hindsight, I suppose just using UUID4 solves this. Just as a relevant side note, are all modern systems equipped with sufficiently good entropy sources? I know some embedded systems aren't, I'm just pointing out in case it is relevant.

ilinum added a commit to ilinum/compose that referenced this issue Dec 4, 2017
Fix docker#1516

Signed-off-by: Svyatoslav Ilinskiy <ilinskiy.sv@gmail.com>
@adamkdean
Copy link

It's a shame that this has to be an issue. While it may not work in your use cases, there are so many other situations where this isn't a problem, sometimes simply testing services at scale. The use of hostnames in some cases can greatly help to identify services, and the ability to generate unique hostnames, whether sequentially numbered or randomly generated, is sometimes a requirement that cannot be ignored.

Take for example a service which identifies itself via it's hostname which must be unique within a cluster. The choice currently is: use randomly generated container hostnames, e.g. bf7c24f9f7c3, b817bffc9cd9, at the expense of clarity, or don't use docker-compose to scale. Supporting a simple feature doesn't mean docker-compose has to enforce this. Simply exposing a ${SCALE} variable would solve a lot of problems without introducing any.

I hope that you can see this, and perhaps in time, introduce it. We've come a long way since 2013, but we still have so far to go.

@kierankelleher
Copy link

What @adamkdean said : “Simply exposing a ${SCALE} variable would solve a lot of problems without introducing any.”

@sergiogiro
Copy link

Is there a way to programmatically query what the containers for a service are? How can I refer to them externally from a program if I don't know what the names are?

@ngrilly
Copy link

ngrilly commented Nov 1, 2018

@sergiogiro docker ps --filter label=com.docker.compose.service=my_service_name

@sergiogiro
Copy link

Thanks, appreciated. Plus -q so that you only get the ids :)

This change to docker compose broke our integration tests (as CircleCI automatically updated to the new version), and it was quite difficult to see why. FWIW, I think that a less traumatic implementation path would have been:

  • the random slug should have been disabled by default, and a warning displayed if the option to activate it wasn't set
  • make the slug present by default, having an option to deactivate, with a warning displayed if the slugs are deactivated, indicating that the option will be deprecated in the future (so that users facing a break don't need to rewrite a lot of code immediately as to fix this, as there's a temporary fix)
  • deprecate the option to deactivate

@sergiogiro
Copy link

Oh, and including @ngrilly 's one liner (plus -q :P) in the release notes where this was mentioned

@ngrilly
Copy link

ngrilly commented Nov 1, 2018

@sergiogiro I forgot about this: if your run multiple projects, you should also specify the project: docker ps -q --filter label=com.docker.compose.service=my_service_name --filter label=com.docker.compose.project=my_project_name.

@flaushi
Copy link

flaushi commented Nov 6, 2018

The default naming scheme has changed from 1.22 to 1.23.
now there is a random suffix in the container name.
I bet this breaks thousands of scripts. IMHO, this must be undone!

@shin-
Copy link

shin- commented Nov 6, 2018

Copying what I wrote in #6140 :

While this can be seen as a breaking change in the sense that container names can no longer be inferred the way they could before, it should be noted that

  1. Compose never made any guarantees about the naming pattern of the containers it creates
  2. Relying on inferred patterns can result in unexpected errors when the sequentiality breaks, making it something to avoid wherever possible
  3. In the rare cases where it is necessary, scripts will still be able to fetch individual containers based on the predictable prefix (the part before the slug), with the caveats mentioned above. The prefix is also backward-compatible.

@stale
Copy link

stale bot commented Apr 17, 2022

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 stale bot added the stale label Apr 17, 2022
@stale
Copy link

stale bot commented Apr 25, 2022

This issue has been automatically closed because it had not recent activity during the stale period.

@stale stale bot closed this as completed Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests