Sub topic 1
The container lifecycle: created, running, stopped, removed
Prerequisites
This chapter assumes you have the following:
- Docker installed and the Docker daemon running.
- Basic command-line skills.
- A clear idea of 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 it.
Why the lifecycle matters
Every Docker command that manages containers acts on a container in a particular state. docker stop needs a running container. docker start needs a stopped one. docker rm refuses to touch a running one unless you force it. If you know the states, most error messages from these commands become easy to read.
This chapter gives you the working model. Later chapters cover each command in detail.
The four stages
A container moves through these stages:
- Created. Docker has built the container from an image and set up its writable layer and configuration. No process is running yet.
- Running. The main process (PID 1 inside the container) is executing.
- Stopped. The main process has exited. Docker reports this state as
exited. The container still exists, with its filesystem changes and its configuration. - Removed. The container is deleted. Its name, ID, and writable layer are gone.
Docker also has a paused state, where processes are frozen, and a restarting state, used with restart policies. This tutorial focuses on the four stages above.
How docker run fits in
docker run combines creating and starting in one step. It creates a container from the image, then starts it. That is why a single command takes you straight to the running state.
You can also do the two steps yourself. docker create stops after the created stage, and docker start moves the container to running.
docker create --name web nginx:alpine
docker ps -a --filter "name=web"
docker start web
docker ps --filter "name=web"The first docker ps -a shows web with the status Created. After docker start, it appears in plain docker ps with a status such as Up 2 seconds.
Why containers stop
A container stays running only while its main process is alive. It moves to stopped in two cases:
- You stop it with
docker stopordocker kill. - The main process finishes on its own, either successfully or with an error.
The second case surprises many learners. This command runs, prints, and exits at once, so the container is stopped as soon as you look at it:
docker run --name greeter alpine echo "hello"
docker ps -a --filter "name=greeter"The listing shows Exited (0). The number in parentheses is the exit code. Zero means success. A non-zero code means the process failed or was killed.
Stopped is not removed
A stopped container is still on disk. It keeps its name, its configuration, and any files written inside it. You can start it again, read its logs, or copy files out of it. It also keeps its name, so you cannot create a new container with the same name until you remove the old one.
docker run --name greeter alpine echo "again"This fails with a conflict error, because greeter already exists in the stopped state.
Removal ends the lifecycle
docker rm deletes the container. After that, the writable layer is gone, and the name is free to reuse. The image the container was created from is not affected. You can create a new container from it at any time.
docker rm greeter
docker run --name greeter alpine echo "fresh start"State summary
| State | Process running? | Shown by `docker ps`? | Shown by `docker ps -a`? | Data kept? |
|---|---|---|---|---|
| Created | No | No | Yes | Configuration only |
| Running | Yes | Yes | Yes | Yes |
| Stopped (exited) | No | No | Yes | Yes |
| Removed | No | No | No | No |
Exercise
You run docker run --name job alpine echo "done". A minute later docker ps shows nothing, but docker run --name job alpine echo "done" fails with a name conflict. What state is the container in, which flag shows it, and which command lets you reuse the name?
Show answer
The container is stopped (exited), because its main process echo finished immediately. docker ps -a shows it with a status like Exited (0). Run docker rm job to delete it, and the name job can then be reused.
Exercise
What is the difference between docker create and docker run, and which state does a container have right after docker create?
Show answer
docker create only builds the container, leaving it in the created state with no process running. docker run creates the container and then starts it, so it ends in the running state.