Default container names in Docker

Rounak Agarwal
3 min readNov 14, 2021

--

Photo by Ari Ara on Unsplash

When a container is created using Docker, Docker provides a way to name the container using the the --name <container_name> flag. However, if no name is provided by the user while creating/running a Docker container, Docker automatically assigns the container a name.

Let’s take a look at some examples of names assigned by Docker to containers:

Docker default container names

If you look at them carefully, you could see a pattern emerge. The first name is an adjective while the second name is the surname of a notable scientist or hacker.

How does Docker generate the names? Let’s find out! 🔍

Digging into the source code of Docker on GitHub, we come across the algorithm used to generate the names. The code is written in the file names-generator_test.go which is written in Go. If Go isn’t something you are familiar with, no need to worry. I will translate it for you.

The file contains two arrays. The first array named left defined at line 9 consists of a collection of adjectives.

Left array containing the adjectives

The second array named right defined at line 122 consists of a collection of surnames of notable scientists or hackers.

Right array containing the surnames of notable scientists or hackers

Finally, we have a function GetRandomName at line 841 that generates the name of the docker container using the arrays defined above. Let’s understand the below code line by line.

GetRandomName function

First, a label begin is defined. Labels in Go allow you to transfer control to the place in the function where the label is defined. Scope of a label is the function where it is declared. We will see how it is used soon.

Next, a variable name is defined with its value being set to a random element from the array left concatenated with an underscore (_) which in turn is concatenated with a random element from the array right.

Now, we have a very interesting Easter Egg in the source code. If you look at Docker generated container names, you will never find the name boring_wozniak. This is because if the name generated is boring_wozniak, the goto begin statement transfers the control to the label begin defined earlier and a new name is generated. This prevents boring_wozniak from being generated as the name of a docker container.

Enjoyed reading this article? Hit the applause 👏 button or leave a comment below to let me know.

--

--

Rounak Agarwal
Rounak Agarwal

Written by Rounak Agarwal

Developer | Open Source Contributor | Quizzer | Loves to convert Ideas into Code | Github profile: https://github.com/agarwalrounak

No responses yet