Container Lifecycle
Storing Extra Snacks Safely
Think of containers as living organisms. They are born (created), they live (running), they can rest (paused), they can stop (terminated), and eventually, they can be removed (deleted). Understanding this lifecycle is crucial for managing applications effectively in development and production environments.
Lifecycle Foundations
1. Container States
- Created: The container is defined but not yet running.
- Running: The container is actively executing its process.
- Paused: The container’s processes are temporarily frozen.
- Stopped/Exited: The container has finished execution or was terminated.
- Removed: The container is deleted from the system.
2. Lifecycle Commands
Remove a Container:
docker rm container_id
Deletes the container from the system.
Kill a Container:
docker kill container_id
Immediately terminates the container.
Stop a Container:
docker stop container_id
Gracefully stops the container.
Pause/Unpause a Container:
docker pause container_id
docker unpause container_id
Temporarily freezes and resumes processes.
Run a Container (create + start):
docker run nginx
Shortcut that creates and starts in one step.
Start a Container:
docker start container_id
Moves the container into the running state.
Create a Container:
docker create nginx
Defines a container but doesn’t start it.
3. Lifecycle Flow
- docker run → Create + Start.
- docker pause/unpause → Freeze/resume.
- docker stop/kill → Terminate.
- docker rm → Remove.
Things to Remember
- Containers are ephemeral by design - they can be created and destroyed quickly.
- The lifecycle is controlled entirely by Docker commands.
- Pausing is useful for temporary suspension without losing state.
- Removing containers frees resources and keeps systems clean.
Hands‑On Lab
Step 1: Create and Start a Container
docker run -d --name mynginx nginx
Step 2: Pause and Unpause
docker pause mynginx
docker unpause mynginx
Step 3: Stop and Restart
docker stop mynginx
docker start mynginx
Step 4: Remove the Container
docker rm mynginx
Practice Exercise
- Run a container using
alpinethat prints “Hello Lifecycle.” - Pause the container and observe its state with
docker ps. - Stop the container and restart it.
- Finally, remove the container and confirm it no longer appears in
docker ps -a.
Visual Learning Model
Created → Running → Paused → Stopped → Removed
The Hackers Notebook
The container lifecycle defines how containers are created, run, paused, stopped, and removed. Mastering these states and commands gives learners control over containerized applications, ensuring efficient resource usage and smooth operations in both development and production.
