Lesson 1: The Puppet Master (Container Lifecycle)
A container is like a puppet — you pull the strings to start it, pause it, stop it, and remove it. Understanding the lifecycle lets you manage containers like a pro.
The Container Lifecycle
docker create docker start docker stop
──────────────────→ ──────────────────→ ──────────────────→
CREATED RUNNING STOPPED
←────────────────── ←────────────────── ←──────────────────
docker pause docker start
──────────→
PAUSED
←──────────
docker unpause
docker rm
──────────────→
REMOVED
Lifecycle Commands
| Command | Action | Example |
|---------|--------|---------|
| docker run | Create + Start | docker run -d nginx |
| docker create | Create only | docker create nginx |
| docker start | Start a stopped container | docker start webserver |
| docker stop | Graceful shutdown (SIGTERM) | docker stop webserver |
| docker kill | Force stop (SIGKILL) | docker kill webserver |
| docker restart | Stop + Start | docker restart webserver |
| docker pause | Freeze (suspend) | docker pause webserver |
| docker unpause | Unfreeze | docker unpause webserver |
| docker rm | Remove a stopped container | docker rm webserver |
Running in the Background: -d
Most production containers run detached (in the background):
docker run -d --name webserver nginx
-d— Detached mode (returns control to your terminal).--name webserver— Give it a human-readable name.
Stopping vs. Killing
docker stopsends SIGTERM — gives the app 10 seconds to shut down gracefully.docker killsends SIGKILL — instant termination (use only if stop fails).
Removing Containers
docker rm webserver # Remove a specific container
docker rm $(docker ps -aq) # Remove ALL stopped containers
docker container prune # Clean up stopped containers
Auto-Remove: --rm
For one-off tasks, use --rm to automatically delete the container when it stops:
docker run --rm ubuntu echo "I self-destruct!"
Mission Objective
Master the container lifecycle:
- Launch: Run
docker run -d --name webserver nginxin the background. - Stop: Shut it down with
docker stop webserver. - Revive: Bring it back with
docker start webserver.