Module 2 Lesson 5: The Container Lifecycle
From 'Created' to 'Exited'. Learn the different states a container can inhabit and how to transition between them like a pro.
Module 2 Lesson 5: The Container Lifecycle
A container goes through several distinct "States" during its existence. Understanding these states is critical for monitoring, orchestration, and troubleshooting.
1. The Key States
- Created: The container has been initialized but not started.
- Running: The main process of the container is active.
- Paused: All processes in the container are suspended (frozen in RAM).
- Restarting: The container is in the process of shutting down and starting back up.
- Exited: The main process has finished or crashed. The container is dead, but its record (and writable layer) still exists on disk.
2. Command Flow
docker create: Image -> Createddocker start: Created -> Runningdocker run: Image -> Running (Combines create + start)docker stop: Running -> Exiteddocker pause: Running -> Pauseddocker unpause: Paused -> Runningdocker rm: Exited -> Deleted
Visualizing the Container Lifecycle
stateDiagram-v2
[*] --> Created: docker create
Created --> Running: docker start
[*] --> Running: docker run
Running --> Paused: docker pause
Paused --> Running: docker unpause
Running --> Restarting: docker restart
Restarting --> Running
Running --> Exited: docker stop / crash
Exited --> Running: docker start
Exited --> [*]: docker rm
note right of Exited
Exit codes:
0 = Success
1 = Error
137 = Killed (OOM)
127 = Command not found
end note
3. Why did my container exit? (Exit Codes)
When a container stops, it returns an Exit Code.
- Exit Code 0: Success! The task is done. (e.g.,
echo "hello"finished). - Exit Code 1: General error (often a code bug).
- Exit Code 137: The container was "Killed" (often by the OS because it ran out of RAM—OOM Kill).
- Exit Code 127: "Command not found." You tried to run a tool that isn't inside the container.
4. The "Zombie" Container Problem
Beginners often forget that stopping a container (docker stop) doesn't delete it.
- If you have 50 stopped containers, they are still taking up space on your disk!
- The Fix: Use
docker container pruneto delete all stopped containers at once. - The 'Auto-Cleanup' Flag: Use
docker run --rm ...to automatically delete the container the moment it stops.
Exercise: State Management
- Run
docker run --name sleeper alpine sleep 100. - In a second terminal, run
docker ps. What is its status? - Run
docker pause sleeper. Checkdocker psagain. What changed? - Try to run
docker stop sleeper. Can you stop a paused container? (Try it!). - Run
docker stop sleeper(after unpausing) and then list withdocker ps -a. What is the status now? - Finally,
docker rm sleeperto clean up.
Summary
You now understand the full journey of a container. You know how to start it, pause it, stop it, and finally, delete it. This lifecycle management is the core of "Infrastructure as Code."
Next Module: We build our own! Module 3: Docker Images and Dockerfiles.