Sub topic 2
Referring to containers by name and ID
Every Docker command that acts on a container needs to know which container you mean. This chapter covers the two identifiers Docker gives each container, how Docker matches what you type to a container, and the habits that keep your commands from hitting the wrong one.
It assumes Docker is installed and the daemon is running. It also assumes you are comfortable in a shell and know that an image is a template and a container is a running or stopped instance of one.
Two identifiers for every container
Each container has an ID and a name, and you can use either in almost any command.
- The ID is a 64-character hexadecimal string that the daemon generates. It is unique and never changes.
- The name is a human-readable label. It is unique among the containers on that daemon, but you can change it.
Start a container and look at both:
docker run -d --name web nginx:alpine
docker ps --no-trunc --format "{{.ID}} {{.Names}}"The first command prints the full 64-character ID of the new container. The second prints the full ID next to the name web.
Container IDs
The long form is accurate but awkward to type. Docker therefore shows a short ID in most places. The short ID is the first 12 characters of the full ID.
docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f4a9c1d2b7e nginx:alpine "/docker-entrypoint.…" 5 seconds ago Up 4 seconds 80/tcp webThe CONTAINER ID column holds the short form. To see the full form, add --no-trunc.
ID prefixes
You do not have to type all 12 characters. Docker accepts any unambiguous prefix of the ID, so these commands are equivalent:
docker stop 3f4a9c1d2b7e
docker stop 3f4a
docker stop 3The last one works only if no other container's ID starts with 3. If two or more containers match, Docker refuses to guess:
Error response from daemon: Multiple IDs found with provided prefix: 3fType more characters until the prefix is unique. A 4-character prefix is usually enough on a machine with a few dozen containers, but a one-character prefix is a habit worth avoiding in scripts. A prefix that is unique today may be ambiguous tomorrow, when you have created more containers.
Container names
Docker always gives a container a name. If you do not choose one, the daemon generates one from an adjective and the surname of a notable scientist or engineer, such as boring_wozniak or quirky_hopper. Generated names are fine for throwaway experiments. They are hard to remember and tell you nothing about the container's purpose.
Choosing a name with --name
Set the name when you create the container:
docker run -d --name web nginx:alpine
docker run -d --name cache redis:7-alpineNames may contain letters, digits, underscores, periods, and hyphens, and they must start with a letter or digit. A name such as my app (with a space) or -web is rejected.
A good name states the role of the container and, if you run several of the same kind, the instance: api-1, api-2, db-staging.
Names must be unique, even for stopped containers
A name belongs to a container until the container is removed, not only while it runs. That surprises many learners. Stopping a container does not free its name:
docker stop web
docker run -d --name web nginx:alpineThe second command fails:
docker: Error response from daemon: Conflict. The container name "/web" is already in use by container "3f4a9c1d2b7e…". You have to remove (or rename) that container to be able to reuse that name.You have three ways out:
- Start the existing container again with
docker start web. - Remove it with
docker rm web, then create the new one. - Rename the old one, as described below, to free the name.
Renaming a container
docker rename changes the name of an existing container, whether it is running or stopped. The ID stays the same.
docker rename web web-old
docker run -d --name web nginx:alpine
docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.Status}}"Renaming does not restart the container. Anything that refers to the old name, such as a script or another container that connects to it by name on a user-defined network, will need updating.
How Docker resolves what you type
When you pass web or 3f4a to a command, the daemon has to decide which container you mean. It tries these matches in order:
- An exact match on a full 64-character ID.
- An exact match on a name.
- A unique prefix match on an ID.
If none of these finds exactly one container, the command fails with No such container or the Multiple IDs error shown earlier.
This order has a consequence. A name that looks like hexadecimal can collide with an ID prefix. Suppose you name a container abc, and another container's ID begins with abc. The name wins because exact names are checked before prefixes. This is legal, but it is confusing, so avoid names made only of the characters 0-9 and a-f.
Container IDs are not image IDs
Images also have IDs, and they look the same: hexadecimal strings, shortened to 12 characters in listings. docker rm takes container identifiers. docker rmi takes image identifiers. Passing an image ID to docker rm gives No such container, because the daemon only searches containers.
When a command says it cannot find something you can plainly see in docker images, check that you used the command for the right kind of object.
Getting identifiers into your commands
Typing IDs by hand is error-prone. These techniques let the shell do it.
Capture the ID at creation
With -d, docker run prints the full container ID. Store it in a variable:
cid=$(docker run -d nginx:alpine)
echo "$cid"
docker stop "$cid"This is the most reliable way for a script to refer to a container it just created. It works even if you did not set a name.
Print only IDs with -q
The -q (quiet) flag on docker ps prints only the short IDs, one per line:
docker ps -q
docker ps -aqThe first lists running containers. The second lists all containers, including stopped ones. Combine -q with --no-trunc for full IDs. Later chapters use this output as the argument list for bulk commands.
Select by name with a filter
--filter name= selects containers by name. The value is matched as a substring or regular expression, not as an exact string:
docker ps -a --filter "name=web"This lists web, but also web-old and my-web-app. To match one container only, anchor the expression:
docker ps -aq --filter "name=^web$"Names are stored with a leading slash internally, but Docker's filter handles that for you, so ^web$ works as written.
Similarly, --filter "id=3f4a" selects by ID prefix.
Look up one container's details
docker inspect accepts a name or an ID and prints everything the daemon knows about the container. A Go template extracts a single field. This is a quick way to convert between names and IDs:
docker inspect --format "{{.Id}}" web
docker inspect --format "{{.Name}}" 3f4aThe second command prints /web. The leading slash is how the daemon stores the name. It appears in docker inspect output but not in docker ps.
Using several containers in one command
Most lifecycle commands accept several identifiers, and you can mix names and IDs:
docker stop web cache 9b2e
docker rm web cacheDocker handles each argument separately and prints each one that succeeded. If one fails, for example because the name is wrong, the others still run, and the command exits with a non-zero status. Check the output rather than assuming that all of them worked.
Common errors
| Message | Likely cause | What to do |
|---|---|---|
| `No such container: web` | Wrong name, the container was already removed, or you meant an image | Run `docker ps -a` and check the spelling and the object type |
| `Conflict. The container name "/web" is already in use` | A container with that name exists, possibly stopped | Start, remove, or rename the existing container |
| `Multiple IDs found with provided prefix` | The prefix matches more than one container | Use a longer prefix or the name |
| `"docker stop" requires at least 1 argument` | An `$(docker ps -q)` substitution was empty | Check that something is running first |
The last row is a real trap in scripts. If no containers are running, docker stop $(docker ps -q) expands to docker stop with no arguments, so the command prints its usage error. It is harmless, but a script that stops on any error will stop here.
Practical conventions
- Name every container you intend to keep. Use generated names only for one-off experiments.
- Use names in interactive work. They are easier to read and type.
- Use full IDs, captured from
docker run -d, when a script needs to refer to a container it created. - Quote variables that hold IDs, as in
"$cid", to protect against empty values. - Anchor name filters with
^and$whenever an exact match matters.
Exercise
You run docker run -d --name api nginx:alpine, then docker stop api. A moment later you run docker run -d --name api nginx:alpine again and get a "Conflict" error. Why does it fail, and what are two ways to fix it?
Show answer
A container's name stays reserved until the container is removed, and stopping does not remove it. The stopped container api still holds the name. Fix it by running docker start api to reuse the existing container, or by running docker rm api and then creating a new container with that name. Renaming the old container with docker rename api api-old also frees the name.
Exercise
Two containers have IDs starting with 3f4a9c... and 3fb812.... What happens when you run docker stop 3f, and how do you stop the first one?
Show answer
Docker reports Multiple IDs found with provided prefix: 3f and stops nothing, because the prefix matches both containers. Use a longer unique prefix such as docker stop 3f4, the full short ID, or the container's name.
Exercise
Write a command that prints only the ID of the container named exactly web, and not containers such as web-old.
Show answer
docker ps -aq --filter "name=^web$"