Container Lifecycle

Loading concept...

🐳 Container Lifecycle: The Life of a Shipping Container

Imagine you have a magical toy box. This toy box can hold any toy inside, and you can open it, close it, pause it (freeze time!), or even make it disappear. That’s exactly what a Docker container is like!


🎬 The Container Story: Meet Boxy

Let’s follow Boxy, our friendly container, through his life journey.

Boxy starts as just an idea (an image). Then he comes to life, runs around doing his job, takes naps, and eventually says goodbye. This journey has different states — like how you feel awake, sleepy, or frozen like a statue!


🔄 Container States and Lifecycle

Think of container states like the different moods of your pet:

State What It Means Like Your Pet…
Created Born but not moving yet Just woke up, still in bed
Running Actively doing work Playing fetch!
Paused Frozen in place Playing freeze tag
Stopped Sleeping peacefully Taking a nap
Dead/Removed Gone forever Went to pet heaven
graph TD A[📦 Created] --> B[🏃 Running] B --> C[⏸️ Paused] C --> B B --> D[🛑 Stopped] D --> B D --> E[💀 Removed] B --> E

The Big Picture: A container can move between these states. You’re the controller!


🚀 Starting Containers

Starting a container is like waking up your pet.

When you start a container, it begins doing its job — running your app, serving a website, or crunching numbers.

Two Ways to Start

Way 1: Create AND Start (Most Common)

docker run nginx

This is like getting a new pet AND waking it up at the same time!

Way 2: Start an Existing Container

docker start my-container

This is like waking up your pet who was already sleeping.

Real Example

# Create and start a web server
docker run -d --name webby nginx

# Check it's running
docker ps

What happens: Boxy wakes up and starts serving web pages! 🎉


🛑 Stopping Containers

Stopping is like telling your pet “time for bed!”

When you stop a container, it gets a gentle message: “Please finish what you’re doing and go to sleep.”

docker stop webby

How It Works

  1. Docker sends a SIGTERM signal (a polite “please stop”)
  2. Container has 10 seconds to clean up
  3. Then it peacefully goes to sleep

Example

# Stop our web server
docker stop webby

# Verify it stopped
docker ps -a

The container is now sleeping. It’s not gone — just resting! You can wake it up again with docker start.


⏸️ Pausing Containers

Pausing is like playing freeze tag!

When you pause a container, everything freezes in place. The app stops exactly where it was — mid-thought, mid-action.

docker pause webby

Why Pause Instead of Stop?

Pause Stop
⚡ Instant freeze 🕐 Graceful shutdown
💾 Keeps memory 💾 Loses memory
🔄 Instant resume 🔄 Needs restart

Example

# Freeze the container
docker pause webby

# Check status (shows "Paused")
docker ps

# Output shows:
# NAMES    STATUS
# webby    Up 5 min (Paused)

Use case: Pause when you need to quickly save system resources but want to continue exactly where you left off.


▶️ Unpausing Containers

Unpausing is like saying “unfreeze!” in freeze tag.

The container continues exactly where it left off — no restart, no reloading.

docker unpause webby

Example

# Unfreeze our container
docker unpause webby

# Check it's running again
docker ps

# Output shows:
# NAMES    STATUS
# webby    Up 6 min

Magic! Boxy unfreezes and continues serving web pages like nothing happened.


⚡ Killing Containers

Killing is the emergency stop button.

Sometimes a container won’t listen to “please stop.” That’s when you use kill — it’s immediate and forceful.

docker kill webby

Stop vs Kill

Stop Kill
🤝 Polite request 🔨 Immediate force
⏱️ Waits 10 seconds ⚡ Instant
💾 Clean shutdown ⚠️ May lose data

When to Use Kill

  • Container is frozen/unresponsive
  • Need immediate shutdown
  • Stop command times out

Example

# Try stop first
docker stop webby
# (If it hangs or doesn't respond...)

# Force kill
docker kill webby

Warning: Only use kill when stop doesn’t work. It’s like pulling the power cord!


🗑️ Removing Containers

Removing is saying goodbye forever.

When you remove a container, it’s gone. Poof! The container and its writable layer disappear.

docker rm webby

Important Rules

  1. Must be stopped first (or use -f flag)
  2. Data inside is lost (unless using volumes)
  3. Cannot undo!

Examples

# Remove a stopped container
docker rm webby

# Force remove a running container
docker rm -f webby

# Remove multiple containers
docker rm container1 container2

# Remove ALL stopped containers
docker container prune

Clean Up Command

# See what you'll remove
docker container prune --dry-run

# Actually remove all stopped
docker container prune -f

📋 Listing Containers

Listing is like taking attendance!

You need to know which containers exist and what they’re doing.

The Commands

# Show running containers only
docker ps

# Show ALL containers (including stopped)
docker ps -a

# Show only container IDs
docker ps -q

# Show latest container
docker ps -l

Understanding the Output

CONTAINER ID  IMAGE  STATUS         NAMES
abc123        nginx  Up 5 minutes   webby
def456        redis  Exited (0)     cache
Column What It Tells You
CONTAINER ID Unique identifier
IMAGE What blueprint was used
STATUS Current state
NAMES Friendly name

Filtering Examples

# Show only running
docker ps --filter status=running

# Show only stopped
docker ps --filter status=exited

# Show by name
docker ps --filter name=webby

🎯 Putting It All Together

Here’s Boxy’s complete life story in one script:

# 1. Birth - Create and start
docker run -d --name boxy nginx

# 2. Check he's alive
docker ps

# 3. Freeze tag!
docker pause boxy

# 4. Unfreeze!
docker unpause boxy

# 5. Time for bed
docker stop boxy

# 6. Wake up!
docker start boxy

# 7. Emergency stop (if needed)
docker kill boxy

# 8. Goodbye forever
docker rm boxy

🌟 Quick Reference

Action Command What Happens
Start docker start name Wake up
Stop docker stop name Gentle sleep
Pause docker pause name Freeze!
Unpause docker unpause name Unfreeze!
Kill docker kill name Force stop
Remove docker rm name Goodbye
List Running docker ps Who’s awake?
List All docker ps -a Everyone!

🎉 You Did It!

Now you understand how containers live, breathe, pause, and eventually say goodbye. You’re the master of the container lifecycle!

Remember: Containers are like pets. Treat them well, know when to let them rest, and clean up after them when they’re done!

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.