Module 2 Lesson 5: The Container Lifecycle
·DevOps

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

  1. Created: The container has been initialized but not started.
  2. Running: The main process of the container is active.
  3. Paused: All processes in the container are suspended (frozen in RAM).
  4. Restarting: The container is in the process of shutting down and starting back up.
  5. 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 -> Created
  • docker start: Created -> Running
  • docker run: Image -> Running (Combines create + start)
  • docker stop: Running -> Exited
  • docker pause: Running -> Paused
  • docker unpause: Paused -> Running
  • docker 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 prune to 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

  1. Run docker run --name sleeper alpine sleep 100.
  2. In a second terminal, run docker ps. What is its status?
  3. Run docker pause sleeper. Check docker ps again. What changed?
  4. Try to run docker stop sleeper. Can you stop a paused container? (Try it!).
  5. Run docker stop sleeper (after unpausing) and then list with docker ps -a. What is the status now?
  6. Finally, docker rm sleeper to 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.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn