0
0
Dockerdevops~15 mins

Container states (created, running, paused, stopped) in Docker - Deep Dive

Choose your learning style9 modes available
Overview - Container states (created, running, paused, stopped)
What is it?
Container states describe the different phases a Docker container can be in during its lifecycle. These states include created, running, paused, and stopped. Each state reflects what the container is currently doing or waiting to do. Understanding these states helps manage containers effectively.
Why it matters
Without knowing container states, managing containers can become confusing and error-prone. For example, trying to interact with a container that is stopped or paused without realizing it can cause failures or delays. Knowing these states helps you control container behavior, optimize resource use, and troubleshoot issues quickly.
Where it fits
Before learning container states, you should understand what containers are and basic Docker commands like creating and running containers. After mastering container states, you can learn about container networking, volumes, and orchestration tools like Docker Compose or Kubernetes.
Mental Model
Core Idea
A container’s state tells you exactly what it is doing or waiting to do at any moment in its lifecycle.
Think of it like...
Think of a container like a washing machine cycle: it can be ready to start (created), actively washing clothes (running), paused mid-cycle (paused), or finished and stopped (stopped).
┌───────────┐     start     ┌───────────┐
│ Created   │────────────▶│ Running   │
└───────────┘             └───────────┘
      ▲                       │   │
      │                       │   │ pause/resume
      │                       ▼   ▼
┌───────────┐             ┌───────────┐
│ Stopped   │◀────────────│ Paused    │
└───────────┘    stop     └───────────┘
Build-Up - 8 Steps
1
FoundationWhat is a Docker container
🤔
Concept: Introduce the basic idea of a Docker container as a lightweight, isolated environment.
A Docker container is like a small, self-contained box that holds an application and everything it needs to run. It shares the computer’s operating system but keeps the app separate from other apps. Containers start from an image and can be created, started, stopped, and removed.
Result
You understand that containers are isolated environments that run applications.
Understanding what a container is helps you grasp why it needs different states to manage its lifecycle.
2
FoundationBasic container lifecycle commands
🤔
Concept: Learn the commands to create, start, stop, and remove containers.
Common Docker commands: - docker create: makes a container but does not start it - docker start: runs a created or stopped container - docker stop: stops a running container - docker rm: removes a stopped container Example: $ docker create --name myapp nginx $ docker start myapp $ docker stop myapp $ docker rm myapp
Result
You can create and control containers using Docker commands.
Knowing these commands sets the stage to understand what state a container is in after each command.
3
IntermediateUnderstanding the Created state
🤔Before reading on: do you think a 'created' container is already running or waiting to start? Commit to your answer.
Concept: The created state means the container exists but is not running yet.
When you run 'docker create', Docker sets up the container but does not start it. The container is in the 'created' state, waiting for you to start it. It has an ID and configuration but no active processes.
Result
The container is ready but idle, consuming minimal resources.
Understanding 'created' helps you know when a container is ready to run but not yet active.
4
IntermediateWhat happens in the Running state
🤔Before reading on: do you think a running container can be paused or stopped? Commit to your answer.
Concept: Running means the container’s main process is active and doing work.
When you start a container, it moves to the running state. The container’s application or service is executing. You can interact with it, send requests, or check logs. It uses CPU and memory resources while running.
Result
The container is actively performing its task.
Knowing the running state clarifies when your app inside the container is live and responsive.
5
IntermediateHow Paused state works
🤔Before reading on: do you think a paused container still uses CPU resources? Commit to your answer.
Concept: Paused means the container’s processes are temporarily frozen but not stopped.
You can pause a running container with 'docker pause'. This freezes all processes inside, stopping CPU use but keeping memory allocated. It’s like pressing a pause button on a video. You can resume it later with 'docker unpause'.
Result
The container is inactive but can quickly resume without restarting.
Understanding pause helps manage resources without losing container state or data.
6
IntermediateStopped state explained
🤔Before reading on: does stopping a container remove it from the system? Commit to your answer.
Concept: Stopped means the container is not running and its processes have ended, but it still exists.
When you stop a container with 'docker stop', Docker sends a signal to end the main process gracefully. The container exits and moves to the stopped state. It no longer uses CPU or memory but remains on disk until removed.
Result
The container is inactive and can be restarted or removed.
Knowing stopped state helps you understand when a container is inactive but still available.
7
AdvancedState transitions and commands
🤔Before reading on: can you pause a container that is stopped? Commit to your answer.
Concept: Containers move between states using specific commands; some transitions are not allowed.
Valid transitions: - created → running (docker start) - running → paused (docker pause) - paused → running (docker unpause) - running → stopped (docker stop) - stopped → running (docker start) Invalid transitions: - paused → stopped directly (must unpause first) - stopped → paused (cannot pause a stopped container) Trying invalid transitions causes errors.
Result
You can predict and control container behavior by knowing allowed state changes.
Understanding allowed transitions prevents command errors and helps automate container management.
8
ExpertImpact of states on resource usage and orchestration
🤔Before reading on: do you think paused containers free all system resources? Commit to your answer.
Concept: Different states affect system resources and orchestration decisions in subtle ways.
Running containers consume CPU, memory, and network resources. Paused containers free CPU but keep memory allocated, which can affect host performance. Stopped containers use no CPU or memory but occupy disk space. Orchestration tools like Kubernetes consider these states to schedule workloads efficiently. For example, paused containers might delay resource cleanup, causing resource contention.
Result
You understand how container states influence system performance and orchestration behavior.
Knowing resource impact of states helps optimize container deployment and avoid hidden performance issues.
Under the Hood
Docker manages container states by controlling the container’s main process and its Linux namespaces and cgroups. When a container is created, Docker sets up the filesystem and namespaces but does not start the process. Starting runs the process inside isolated namespaces. Pausing uses the Linux freezer cgroup to suspend all processes without terminating them. Stopping sends a SIGTERM signal to gracefully end the process, then SIGKILL if needed.
Why designed this way?
This design leverages Linux kernel features for isolation and control, allowing lightweight and fast container management. Pausing was added to allow temporary suspension without losing state, useful for debugging or resource management. The clear states simplify user understanding and automation.
┌───────────────┐
│ Docker Engine │
└──────┬────────┘
       │ manages
       ▼
┌───────────────┐
│ Container     │
│ Lifecycle     │
│ Controller    │
└──────┬────────┘
       │ controls
       ▼
┌───────────────┐
│ Linux Kernel  │
│ Namespaces &  │
│ cgroups      │
└───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does stopping a container delete it from your system? Commit yes or no.
Common Belief:Stopping a container removes it completely from the system.
Tap to reveal reality
Reality:Stopping only halts the container’s process; the container still exists and can be restarted or removed later.
Why it matters:Mistaking stopped for removed can cause confusion when containers appear to persist after stopping.
Quick: Can you pause a container that is not running? Commit yes or no.
Common Belief:You can pause any container regardless of its state.
Tap to reveal reality
Reality:Only running containers can be paused; paused or stopped containers cannot be paused again.
Why it matters:Trying to pause a stopped container causes errors and wastes time troubleshooting.
Quick: Does pausing a container free all its system resources? Commit yes or no.
Common Belief:Pausing a container releases all CPU and memory resources immediately.
Tap to reveal reality
Reality:Pausing stops CPU use but keeps memory allocated, which can still impact system resources.
Why it matters:Assuming paused containers free all resources can lead to unexpected resource exhaustion.
Quick: Does creating a container mean it is running? Commit yes or no.
Common Belief:Once a container is created, it is already running.
Tap to reveal reality
Reality:Created containers exist but do not run until explicitly started.
Why it matters:Confusing created with running can cause failed attempts to interact with inactive containers.
Expert Zone
1
Paused containers keep memory allocated, which can cause memory pressure on the host if many are paused simultaneously.
2
Docker’s state management relies on Linux kernel features, so behavior can vary slightly across different kernel versions or host OS.
3
Some orchestration tools treat stopped containers differently, sometimes removing them automatically, which can surprise users expecting persistence.
When NOT to use
Using paused state is not suitable for long-term suspension; instead, stopping or removing containers is better to free resources. For orchestration, relying on paused containers can complicate scheduling and resource tracking. Alternatives include scaling down services or using snapshots for state preservation.
Production Patterns
In production, containers are usually kept running or stopped; pausing is rare and mainly used for debugging or maintenance. Automated scripts check container states to restart failed containers. Orchestrators monitor states to maintain desired service levels and resource efficiency.
Connections
Operating System Process States
Container states mirror OS process states like running, sleeping, or stopped.
Understanding OS process states helps grasp container states since containers are processes with isolation.
Virtual Machine Lifecycle
Both containers and VMs have lifecycle states, but containers are lighter and faster to change states.
Comparing containers to VMs clarifies why container states are simpler and more dynamic.
Traffic Light System
Container states control flow like traffic lights control cars: go (running), stop (stopped), pause (paused).
This connection helps understand how container states regulate activity and resource use.
Common Pitfalls
#1Trying to pause a container that is stopped.
Wrong approach:docker pause mycontainer
Correct approach:docker start mycontainer # then docker pause mycontainer
Root cause:Misunderstanding that only running containers can be paused.
#2Assuming stopping a container deletes it.
Wrong approach:docker stop mycontainer # expecting container to be gone
Correct approach:docker stop mycontainer docker rm mycontainer
Root cause:Confusing stopping with removing containers.
#3Leaving many containers paused to save resources long-term.
Wrong approach:docker pause container1 ... (many containers paused)
Correct approach:docker stop container1 ... (stop containers to free memory)
Root cause:Not realizing paused containers still consume memory.
Key Takeaways
Docker containers have distinct states: created, running, paused, and stopped, each reflecting their activity level.
Created containers exist but do not run until started; running containers actively execute processes.
Paused containers freeze processes to save CPU but keep memory allocated; stopped containers free CPU and memory but remain on disk.
Only certain state transitions are allowed; knowing these prevents command errors and aids automation.
Understanding container states helps optimize resource use, troubleshoot issues, and manage container lifecycles effectively.