Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
containers Docker Docker & Containers: From Zero to Production Docker Basics Module 1 — Container Fundamentals

Container Lifecycle — Run, Stop, Start, Remove Explained

Reviewed & accurate
AI Summary

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
created docker create start running docker run/start pause paused stop stopped docker stop rm removed docker rm restart (docker start) Container Lifecycle States
Container lifecycle — created → running → (paused) → stopped → removed

The Five Container States

StateWhat it meansMemory used?How to get here
createdContainer exists but hasn't startedNodocker create
runningContainer is executing its main processYesdocker run or docker start
pausedContainer is frozen in memory, not executingYesdocker pause
stopped (exited)Container's main process has endedNo (freed)docker stop, docker kill, or process exits
removedContainer is deleted — filesystem and allNodocker 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:

Terminalbash
# 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.

Terminalbash
# 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 — runs echo, 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.

Terminalbash
# Pause a running container
docker pause web

# Check status
docker ps
# STATUS: Up 2 minutes (Paused)

# Unpause to resume
docker unpause web
When to use pause
Pause is useful when you want to temporarily free CPU for another container without losing the paused container's state. Rarely used in practice, but good to know.

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

Aspectdocker stopdocker kill
Signal sentSIGTERMSIGKILL
Graceful?Yes — app can clean upNo — immediate termination
Timeout10 seconds, then SIGKILLNone
Use whenNormal shutdownContainer is stuck/unresponsive
Terminalbash
# 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!

Important
A stopped container is NOT deleted. Its filesystem, logs, and configuration are all still there. You can restart it with docker start or remove it with docker rm.
Terminalbash
# 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.

Terminalbash
# 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
Warning
Removing a container deletes its writable layer. Any data stored inside the container (not in a volume) is lost. Use volumes for persistent data.

The Full Lifecycle in Practice

docker run -d --name demo nginx → creates and starts (created → running)
docker pause demo → running → paused
docker unpause demo → paused → running
docker stop demo → running → stopped
docker start demo → stopped → running (restart)
docker stop demo → running → stopped
docker 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:

Terminalbash
# 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:

Terminalbash
# 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
PolicyWhen it restarts
no (default)Never — container stays stopped
alwaysAlways — even if stopped manually, even on reboot
unless-stoppedAlways, unless you explicitly stopped it
on-failureOnly if the process exits with non-zero code

Common Mistakes

Avoid these
  • Expecting stopped containers to disappear. They don't. docker ps only shows running; use docker ps -a to see stopped ones.
  • Using docker kill by default. Use docker stop for graceful shutdown. kill is for stuck containers.
  • Forgetting --rm for 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)

Run docker run -d --name lifecycle-demo nginx
Check it's running: docker ps
Pause it: docker pause lifecycle-demo — check docker ps shows (Paused)
Unpause: docker unpause lifecycle-demo
Stop it: docker stop lifecycle-demo
Check docker ps — it's gone from the list
Check docker ps -a — it's still there, status Exited
Restart it: docker start lifecycle-demo
Remove it: docker rm -f lifecycle-demo
Confirm it's gone: docker ps -a

Mini 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 stop is graceful (SIGTERM); docker kill is immediate (SIGKILL).
  • Stopped containers still exist — use docker ps -a to see them.
  • --rm auto-removes containers when they exit — great for one-off commands.
  • Use --restart unless-stopped or --restart always for production containers.
  • Removing a container deletes its writable layer — use volumes for persistent data.
Previously: Lesson 07 covered the essential CLI commands.
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.

Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments