4

I have read how the containers are assigned the containers IDs: How the docker container id is generated

How is the docker ID uniqueness verified? And in which pool is it unique? Among all exited, among all running, among all deleted/removed, among all ever created by a specific docker service?

I was wondering whether the container ID is a reusable value, since it comes from a random number, how likely is it that a new container will have exactly the same container ID as another one (exited, deleted etc)?

Another relative issue: https://forums.docker.com/t/docker-container-id-uniqueness/5253

UPDATE: Could you please point me to the code that verifies that if the container ID already exists in creates a new one?

belabrinel
  • 851
  • 8
  • 15

2 Answers2

3

The ID needs to be unique within a given docker host among all containers that currently exist (including exited and created containers). Once deleted, the engine no longer tracks the container ID. A container could potentially reuse the same container ID as a previously existing container, but the odds of that are fairly low.

The full ID is a 64 character hex string, which gives 16^64 possible permutations (115792089237316195423570985008687907853269984665640564039457584007913129639936 if my calculator is correct). If you only track the short ID's, that's a 12 character hex string, with 16^12 (281,474,976,710,656) permutations. If you create a significant number of containers and need to track them historically and uniquely, then you may want to use the full container ID.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • Have a look at the birthday problem (https://en.wikipedia.org/wiki/Birthday_problem) which makes clear that a collision is definitely possible. – Ercksen Jul 04 '21 at 14:32
  • @Ercksen "definitely" is a rather absolute word. I've yet to hear of a single case of this actually occurring in the wild. 16^64 is quite a bit larger than the number of days a year, so the number of containers you need to create will be rather large to make a collision probable. – BMitch Jul 04 '21 at 18:28
1

A deck of cards gives you a similarly large number of possibilities. When you shuffle, it is highly likely that you create a permutation of cards that has never before existed. Think about it the other way around: how likely is it that you shuffle the cards and end up with a deck in order? That's "definitely possible" too, but "the odds are fairly low".

timkay
  • 656
  • 9
  • 15