C.W.K.
Stream
Lesson 04 of 05 · published

Lifecycle — ps, stop, start, kill, rm, prune

~14 min · commands, lifecycle

Level 0Container Curious
0 XP0/36 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

The container state machine

created → running → paused → stopped → removed

Each transition has a command. Knowing the right one matters when a container is misbehaving.

  • docker ps — list running containers. -a to include stopped.
  • docker stop <name> — sends SIGTERM, waits 10s, sends SIGKILL. Graceful.
  • docker kill <name> — SIGKILL immediately. Use only if stop hangs.
  • docker start <name> — restart a stopped container.
  • docker restart <name> — stop then start.
  • docker rm <name> — remove a stopped container. -f to force-remove a running one.

Cleanup commands

  • docker container prune — remove all stopped containers.
  • docker image prune — remove dangling (untagged) images.
  • docker system prune -a --volumes — nuke unused images, containers, networks, AND volumes. Read that flag list twice.

Code

Daily cleanup workflow·bash
# What's running?
docker ps

# What has run?
docker ps -a --format 'table {{.Names}}\t{{.Status}}\t{{.Image}}'

# Stop everything
docker stop $(docker ps -q)

# Remove stopped containers
docker container prune -f

# Remove dangling images (safe)
docker image prune -f

# Check disk usage before going nuclear
docker system df
Nuclear option (read twice)·bash
# Removes:
#  - all stopped containers
#  - all unused networks
#  - all unused images (not just dangling)
#  - ALL volumes that aren't used by any container
#
# That last one will delete database data if no container is
# currently mounting it. There is no undo.
docker system prune -a --volumes

External links

Exercise

Start a container, write a file inside it, stop it (don't remove). Use docker start to bring it back. Is the file still there? Now docker rm -f it and docker run a fresh one from the same image. Is the file there? Explain the difference in two sentences.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.