Sub topic 1
The container lifecycle: created, running, stopped, removed
Prerequisites
This chapter assumes the following:
- Docker is installed and the Docker daemon is running. You can check with
docker version, which should print both a Client and a Server section. - You are comfortable in a terminal: running commands, reading their output, and chaining them with
&&. - You know the difference between an image and a container. An image is a read-only template. A container is a running or stopped instance created from that image.
Later chapters cover the commands for listing, stopping, starting, and removing in detail. Here you build the mental model that those commands act on. You will use a few of them as probes along the way.
Why the lifecycle matters
A container is not just "on" or "off". Docker tracks each container as an object with a state, and every management command either reads that state or moves the container to a new one. If you know the states and the transitions between them, the behavior of docker ps, docker stop, docker start, and docker rm becomes predictable. You also get a way to explain the common surprises, such as a container that "disappeared" from docker ps, or one that came back with its old files.
One point underlies the rest of this chapter. A container has its own existence, separate from the process inside it. The process can end while the container object stays on disk. That stopped container can be inspected and started again until you remove it.
The states
Docker reports a container's state in the State.Status field. The main states are:
| State | Meaning |
|---|---|
| `created` | The container exists, with its configuration and writable layer, but its process has never been started. |
| `running` | The main process is executing. |
| `paused` | The processes are frozen but still in memory. |
| `restarting` | Docker is in the middle of restarting the container, usually because of a restart policy. |
| `exited` | The main process has ended. The container object and its filesystem remain. |
| `removing` | Docker is deleting the container. |
| `dead` | A removal failed part-way and left the container in a state that cannot be started again. |
Most of your time is spent with four of these: created, running, exited, and the point where the container no longer exists. The chapter title calls that last point "removed". Removed is not a state you can list. After removal, Docker has no record of the container.
Users often say "stopped" for exited. The two are the same condition, and this tutorial uses both words. The docker ps status column shows Exited (0) 5 seconds ago, while the state filter uses the word exited.
The transitions
The diagram shows how a container moves between these states and which command usually causes each move.
Four points from the diagram deserve attention:
docker runis a shortcut. It creates a container from an image and then starts it, so it enters the lifecycle atrunningwithout you seeingcreated.exitedis not a dead end. You can go fromexitedback torunningwithdocker start, as many times as you like.- Removal is the only way out of the lifecycle. A container that is not removed stays on disk indefinitely, even if it has been stopped for months.
docker rm -fcan remove a running container directly. It kills the process first, so it skips the graceful path thatdocker stopuses.
Walking through the lifecycle
The following session takes one container through every main state. It uses the small nginx:alpine image. The docker create step downloads the image if you do not already have it.
Start by creating a container without starting it:
docker create --name lifecycle-demo nginx:alpine
docker inspect --format '{{.State.Status}}' lifecycle-demoThe second command prints created. The container has an ID, a name, a configuration, and a writable layer. No process is running. If you run docker ps at this point, the container does not appear, because docker ps shows only running containers by default. Add -a to see it.
Now start it and check again:
docker start lifecycle-demo
docker inspect --format '{{.State.Status}}' lifecycle-demo
docker ps --filter name=lifecycle-demoThe state is now running, and the container appears in the default docker ps listing. Docker runs the command that the image defines, which for this image is the nginx server. docker start prints the container name and returns right away. The container keeps running in the background.
Next, stop it:
docker stop lifecycle-demo
docker inspect --format '{{.State.Status}} exit={{.State.ExitCode}}' lifecycle-demo
docker ps
docker ps -a --filter name=lifecycle-demoThe state is exited, and docker ps no longer lists the container. Only docker ps -a finds it. That explains the usual "my container disappeared" report. The container did not vanish. It was filtered out of the default view because it is no longer running.
Finally, remove it:
docker rm lifecycle-demo
docker ps -a --filter name=lifecycle-demoThe listing is now empty. The container object, its writable layer, and its name are gone. The nginx:alpine image is untouched, and you can create another container from it immediately.
What "running" really means
A container is running for exactly as long as its main process (the process Docker started as PID 1 inside the container) is alive. When that process exits, the container exits with it. Docker does not keep a container up on its own.
This surprises many people on their first attempt to run a base image such as alpine in the background:
docker run -d --name quick-exit alpine echo "done"
docker ps -a --filter name=quick-exit --format "table {{.Names}}\t{{.Status}}"
docker rm quick-exitThe echo command prints a line and ends within milliseconds, so the container is exited almost immediately. The listing shows Exited (0). Nothing went wrong. The container did the one thing its command asked for.
The reverse also holds. A server such as nginx stays in the foreground, so the container stays running until something stops the process. If a service daemonizes itself and forks into the background, PID 1 may exit while the real work continues. Docker then sees the main process end and marks the container exited. This is why container images typically run their services in the foreground.
Exit codes
When a container reaches exited, Docker records the exit code of the main process. You can read it with docker inspect or see it in the docker ps -a status column.
| Exit code | Usual meaning |
|---|---|
| `0` | The process finished normally. |
| `1` and other small numbers | The process reported an error. The meaning is defined by the program. |
| `137` | The process was killed by SIGKILL (128 + 9). This happens after `docker kill`, after a grace period expires, or when the kernel kills a process for exceeding its memory limit. |
| `143` | The process ended because of SIGTERM (128 + 15) and did not handle the signal itself. |
Codes above 128 mean the process was ended by a signal, and the signal number is the code minus 128. Exit codes are your first clue when you diagnose why a container stopped.
What each state keeps
The practical differences between the states are about what survives. This table gives the most important comparisons.
| Thing | `created` | `running` | `exited` | removed |
|---|---|---|---|---|
| Container ID and name | kept | kept | kept | released |
| Main process | none | alive | ended | none |
| Writable layer (files written inside the container) | empty | in use | kept | deleted |
| Logs | none | being written | kept | deleted |
| Named volumes | kept | in use | kept | kept |
| Anonymous volumes | kept | in use | kept | kept unless removal asks for them to go |
| Image | kept | kept | kept | kept |
Two rows deserve more explanation.
The first is the writable layer. Every container gets a thin read-write layer on top of its image. Files you create or change inside the container live there. They persist across stop and start, because the container object is the same one. They are lost when the container is removed. You can see this by writing a file, stopping and starting the container, and then reading the file again.
docker run -d --name keeps-files alpine sleep 3600
docker exec keeps-files sh -c "echo hello > /note.txt"
docker stop keeps-files
docker start keeps-files
docker exec keeps-files cat /note.txt
docker rm -f keeps-filesThe cat command prints hello. After the final docker rm -f, that file is gone for good. A later container started from alpine has no /note.txt.
The second row is volumes. Named volumes and bind mounts live outside the container's writable layer, so removing the container does not delete them. Anonymous volumes are the exception that needs care. Docker can delete them together with the container when you ask it to. The chapter on removal covers that in detail.
Stopped does not mean cheap to ignore
An exited container uses no CPU or memory, but it still holds disk space for its writable layer and logs, and it still reserves its name. Both facts have consequences:
- Disk usage grows if you run many one-off containers without removing them.
- You cannot create a new container with a name that an exited container already holds. Docker reports a name conflict, and you must remove or rename the old container first.
Both problems come from the same cause. Stopping a container does not remove it. Later chapters show how to clean up deliberately.
A short vocabulary check
These terms recur throughout the tutorial, so it helps to fix them now:
- Create makes the container object. It does not start a process.
- Start launches the main process of an existing container.
- Stop asks the main process to end and waits for it. The container moves to
exited. - Kill ends the main process immediately with a signal.
- Remove deletes the container object. This is permanent for its writable layer and logs.
- Run is create plus start in one command.
Checking your understanding
Exercise
You run docker run -d --name job alpine echo "hi". A minute later docker ps shows nothing, but you did not stop the container. What happened, which command shows it, and does it still use disk space?
Show answer
The echo command was the container's main process. It finished in milliseconds, so the container moved to the exited state with exit code 0. docker ps shows only running containers, so you need docker ps -a to see it. The exited container still exists, so it keeps its writable layer, its logs, and the name job until you remove it with docker rm job.
Exercise
A container writes a report to /tmp/report.txt inside its own filesystem, with no volume attached. You run docker stop and then docker start on it. Is the file still there? What if you run docker rm and then create a new container from the same image?
Show answer
After docker stop and docker start the file is still there, because the same container object and its writable layer are reused. After docker rm, the writable layer is deleted, so a new container from the same image starts without the file. To keep such data, write it to a named volume or a bind mount.
Exercise
A container's status column reads Exited (137). What does the code tell you about how the process ended?
Show answer
A code above 128 means a signal ended the process, and the signal number is the code minus 128. 137 minus 128 is 9, which is SIGKILL. The container was killed forcefully, for example by docker kill, by a stop whose grace period ran out, or by the kernel after the container exceeded its memory limit.