What You'll Learn
Beginner
- The five states every container goes through
- Which commands transition between states
- The difference between stop, kill, and pause
- How to clean up containers properly
The Five Container States
| State | What it means | Memory used? | How to get here |
|---|---|---|---|
| created | Container exists but hasn't started | No | docker create |
| running | Container is executing its main process | Yes | docker run or docker start |
| paused | Container is frozen in memory, not executing | Yes | docker pause |
| stopped (exited) | Container's main process has ended | No (freed) | docker stop, docker kill, or process exits |
| removed | Container is deleted — filesystem and all | No | docker rm |
State 1: created
The created state means a container exists but hasn't started. This is rarely used directly — docker run combines create + start. But you can do them separately:
# Create a container without starting it
docker create --name my-container ubuntu
# It exists but isn't running
docker ps -a
# CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES
# abc123 ubuntu "bash" 3 seconds ago Created my-container
# Start it later
docker start my-container
State 2: running
The running state is when the container's main process (PID 1) is executing. This is the normal state for active containers.
# Start a container in running state
docker run -d --name web nginx
# Check it's running
docker ps
# STATUS: Up 5 seconds
How a container stays running
A container runs as long as its main process (PID 1) is alive. If the process exits, the container stops. This is why:
docker run ubuntu echo hello— runsecho, prints "hello", exits immediately.docker run -d nginx— nginx's master process runs forever, container stays up.docker run -it ubuntu bash— bash runs as long as you're in the shell. Exit → container stops.
State 3: paused
The paused state freezes the container's processes in memory. The container is still using RAM but isn't executing. Useful for temporarily freeing CPU without stopping the container.
# Pause a running container
docker pause web
# Check status
docker ps
# STATUS: Up 2 minutes (Paused)
# Unpause to resume
docker unpause web
State 4: stopped (exited)
A container enters the stopped state when:
- The main process exits naturally.
- You run
docker stop(graceful shutdown). - You run
docker kill(immediate kill).
docker stop vs docker kill
| Aspect | docker stop | docker kill |
|---|---|---|
| Signal sent | SIGTERM | SIGKILL |
| Graceful? | Yes — app can clean up | No — immediate termination |
| Timeout | 10 seconds, then SIGKILL | None |
| Use when | Normal shutdown | Container is stuck/unresponsive |
# Graceful stop (recommended)
docker stop web
# Force kill (if stop hangs)
docker kill web
# Stop with custom timeout
docker stop -t 30 web # wait 30 seconds before SIGKILL
A stopped container still exists!
docker start or remove it with docker rm.# See stopped containers
docker ps -a
# STATUS: Exited (0) 2 minutes ago
# Restart a stopped container
docker start web
# Remove a stopped container
docker rm web
State 5: removed
The removed state is final — the container and its filesystem are deleted. This cannot be undone.
# Remove a stopped container
docker rm web
# Force remove a running container (stops then removes)
docker rm -f web
# Remove all stopped containers
docker container prune
The Full Lifecycle in Practice
docker run -d --name demo nginx → creates and starts (created → running)docker pause demo → running → pauseddocker unpause demo → paused → runningdocker stop demo → running → stoppeddocker start demo → stopped → running (restart)docker stop demo → running → stoppeddocker rm demo → stopped → removed (gone forever)Auto-Remove with --rm
The --rm flag tells Docker to remove the container automatically when it stops. Perfect for one-off commands:
# Container is removed as soon as the command finishes
docker run --rm alpine echo "hello"
# Great for one-off tasks
docker run --rm -v $(pwd):/data alpine ls /data
# NOT suitable for long-running services you want to restart later
Restart Policies
For production containers, you want them to restart automatically if they crash or if the host reboots. Use --restart:
# Always restart (even on reboot)
docker run -d --restart always nginx
# Restart unless explicitly stopped
docker run -d --restart unless-stopped nginx
# Restart up to 3 times on failure
docker run -d --restart on-failure:3 nginx
| Policy | When it restarts |
|---|---|
no (default) | Never — container stays stopped |
always | Always — even if stopped manually, even on reboot |
unless-stopped | Always, unless you explicitly stopped it |
on-failure | Only if the process exits with non-zero code |
Common Mistakes
- Expecting stopped containers to disappear. They don't.
docker psonly shows running; usedocker ps -ato see stopped ones. - Using
docker killby default. Usedocker stopfor graceful shutdown.killis for stuck containers. - Forgetting
--rmfor one-off commands. Without it, stopped containers pile up. - Not using restart policies in production. If a container crashes at 3 AM, you want it to restart automatically.
- Removing containers with data inside. Use volumes for persistent data. Removing a container deletes its writable layer.
Practical Exercise (10 minutes)
docker run -d --name lifecycle-demo nginxdocker psdocker pause lifecycle-demo — check docker ps shows (Paused)docker unpause lifecycle-demodocker stop lifecycle-demodocker ps — it's gone from the listdocker ps -a — it's still there, status Exiteddocker start lifecycle-demodocker rm -f lifecycle-demodocker ps -aMini Challenge
Run a container with --restart unless-stopped. Stop it explicitly with docker stop. Restart the Docker daemon (or restart your computer). Does the container come back? (Hint: with unless-stopped, it should NOT come back because you explicitly stopped it. Try the same with --restart always to see the difference.)
Key Takeaways
- Five states: created → running → (paused) → stopped → removed.
docker stopis graceful (SIGTERM);docker killis immediate (SIGKILL).- Stopped containers still exist — use
docker ps -ato see them. --rmauto-removes containers when they exit — great for one-off commands.- Use
--restart unless-stoppedor--restart alwaysfor production containers. - Removing a container deletes its writable layer — use volumes for persistent data.
Today: You went deep into container lifecycle — the five states and how to move between them.
Next: Module 1 is complete! Module 2 begins with lesson 09 — What Is a Docker Image?
FAQ
What happens to a container when the Docker daemon restarts?
By default, containers that were running before the daemon restart will NOT start again. Use --restart always or --restart unless-stopped to have them start automatically when the daemon comes back up.
Can I rename a container after creation?
Yes: docker rename old-name new-name. The container keeps its state — running containers stay running.
Comments
Comments
Post a Comment