Sub topic 2
Referring to Containers by Name and ID
This chapter assumes Docker is installed and the daemon is running. It also assumes you can use a shell and know the difference between an image and a container. Every command that acts on a container needs you to say which container you mean. Docker gives you three ways to do that: the full ID, a short ID prefix, and the name.
Why containers have identifiers
Each time you create a container, Docker assigns it two identifiers. One is a unique ID that Docker generates. The other is a name, which you can choose or let Docker pick. Commands such as docker stop, docker start, and docker rm accept either one.
Start a container and look at both:
docker run -d --name web nginx:alpine
docker psThe docker ps output has a CONTAINER ID column and a NAMES column. The ID looks like 3f4a9c1be2d7, and the name is web.
Container IDs
A container ID is a 64-character hexadecimal string. docker ps shows only the first 12 characters, called the short ID. You can use the short ID in any command that takes a container.
You can also type a shorter prefix, as long as it matches exactly one container. If your container's ID starts with 3f4a9c1be2d7, these commands refer to the same container:
docker stop 3f4a9c1be2d7
docker stop 3f4aIf a prefix matches more than one container, Docker refuses to act and reports an error that the prefix is ambiguous. Type more characters to resolve it.
To see the full ID, use the --no-trunc flag:
docker ps --no-truncContainer names
A name is easier to remember than an ID. You set one with --name when you create the container:
docker run -d --name db postgres:16If you omit --name, Docker generates a random name from two words, such as quirky_turing or brave_hopper. These work, but they tell you nothing about what the container does. In scripts and day-to-day work, set names yourself.
Two rules apply to names:
- A name must be unique on the Docker host. This includes stopped containers, because a stopped container still owns its name until you remove it.
- A name may contain letters, digits, underscores, periods, and hyphens, and must start with a letter or digit.
Name conflicts
The uniqueness rule causes one of the most common errors. Suppose web has stopped but not been removed. Running a new container with the same name fails:
docker run -d --name web nginx:alpineDocker responds with a message like this:
docker: Error response from daemon: Conflict. The container name "/web" is already in use by container "3f4a9c1be2d7...". You have to remove (or rename) that container to be able to reuse that name.You have three options. Start the existing container with docker start web, remove it with docker rm web, or give the new container a different name. The error message also tells you the ID of the container that holds the name.
You can rename a container at any time, whether it is running or stopped:
docker rename web web-oldChoosing between name and ID
Both identify the same container, but they behave differently over time.
| Property | Name | ID |
|---|---|---|
| Chosen by | You, or Docker at random | Docker |
| Can change | Yes, with `docker rename` | No |
| Reusable after removal | Yes | No, each new container gets a new ID |
| Best for | Interactive work and scripts | Exact targeting when names repeat or are random |
The last row of the table matters when you remove and recreate a container. The new container can reuse the old name, but it has a different ID. A script that stored the old ID will no longer find it.
Capturing an ID in a script
docker run -d prints the full ID of the new container. You can save it in a variable and use it later:
CID=$(docker run -d nginx:alpine)
echo "Started $CID"
docker stop "$CID"
docker rm "$CID"This avoids guessing at a random name. Quote the variable so the shell treats it as a single value.
How the diagram fits together
The flow below shows how Docker resolves the identifier you type.
A full-length name that equals a container name is checked first, so avoid naming a container with a string that looks like the start of another container's ID.
Summary of the key points
- Every container has a unique ID and a unique name.
- Short IDs and unambiguous prefixes work wherever an ID is accepted.
- Stopped containers keep their names until removed.
- Set names with
--nameso your commands are readable and predictable.
Exercise
You run docker run -d --name web nginx:alpine and get the error that the name "/web" is already in use, although docker ps shows no container called web. Why does this happen, and what are two ways to fix it?
Show answer
A stopped container named web still exists and keeps its name. docker ps shows only running containers, so you would see it with docker ps -a. To fix it, either run docker start web to reuse the existing container, or run docker rm web and then create the new one. Choosing a different name also works.
Exercise
Two containers have IDs starting with a1b2c3 and a1f9e8. Which prefix is the shortest one you can safely use to refer to the first container?
Show answer
a1b works. It matches only the first container. The prefix a1 would be ambiguous because both IDs start with it.