← Back to Questions
Docker

Docker container lifecycle explained

Learn Docker container lifecycle explained with simple explanations, real-time examples, interview tips and practical use cases.

Docker Container Lifecycle Explained

The Docker container lifecycle describes the complete journey of a container from creation to execution, pausing, stopping, restarting, and eventual removal.

Simple Definition: Docker container lifecycle represents the different states and transitions a container goes through while running inside Docker.

Why This Question is Important

This is one of the most important Docker, DevOps, Kubernetes, and Microservices interview questions asked by companies in USA, UK, India, and global cloud-native environments.

Interviewers ask this question to evaluate:

  • Docker fundamentals
  • Container runtime understanding
  • Production troubleshooting knowledge
  • DevOps operational concepts
  • Container orchestration basics
“Understanding the container lifecycle is critical for debugging production issues.”

What is a Docker Container?

A Docker container is a lightweight, isolated runtime environment created from a Docker image.

Basic Flow

Docker Image
      |
Container Created
      |
Container Running
    

Container Lifecycle Overview

Created
   |
Running
   |
Paused
   |
Stopped
   |
Restarted
   |
Removed
    

Main Docker Container Lifecycle States

State Description
Created Container exists but not running
Running Container process is active
Paused Container processes temporarily frozen
Stopped Container process terminated
Restarting Container restarting after failure or command
Exited Main process completed
Removed Container deleted from Docker host

Docker Container Lifecycle Internally

Docker CLI
     |
Docker Daemon
     |
containerd
     |
runc
     |
Linux Kernel
     |
Container Process
    

Step 1: Docker Image Exists

Every container starts from an image.

Example

docker pull nginx
    

Image Flow

Docker Registry
       |
Docker Image Pulled
       |
Stored Locally
    

Step 2: Container Creation

Docker creates a writable container layer on top of the image.

Command

docker create nginx
    

Created State

Image Exists
      |
Container Metadata Created
      |
Filesystem Prepared
      |
NOT Running Yet
    

What Happens Internally?

  • Filesystem layers prepared
  • Namespaces created
  • cgroups configured
  • Networking initialized
  • Container metadata stored

Step 3: Container Start

Docker starts the container’s main process.

Command

docker start container-id
    

Or Direct Run

docker run nginx
    

Running State Flow

Container Start Requested
        |
Docker Daemon Invokes containerd
        |
runc Creates Container Process
        |
Namespaces Applied
        |
Main Process Starts
        |
Container Running
    

Important Concept

A container lives only while its main process is running.

Example

Container Main Process:
nginx
    

If nginx exits, container stops.

Step 4: Running State

Container is actively executing workloads.

What Happens in Running State?

  • Application processes execute
  • CPU and memory consumed
  • Network traffic handled
  • Logs generated
  • Volumes mounted

Production Example

API Gateway Container
Portfolio Service Container
Payment Service Container
MySQL Container
Redis Container
    

Check Running Containers

docker ps
    

Step 5: Pause State

Pausing freezes container processes temporarily.

Command

docker pause container-name
    

Pause Flow

Container Running
      |
Processes Frozen
      |
CPU Usage Stops
      |
Memory Still Allocated
    

Resume Container

docker unpause container-name
    

When Pause is Useful

  • Debugging
  • Resource management
  • Temporary suspension

Step 6: Stop State

Stopping gracefully terminates the container.

Command

docker stop container-name
    

Stop Workflow

SIGTERM Sent
      |
Application Attempts Graceful Shutdown
      |
Timeout Reached?
      |
SIGKILL Sent if Needed
    

Important Signals

Signal Purpose
SIGTERM Graceful shutdown request
SIGKILL Force termination

Production Importance

Graceful shutdown prevents:

  • Data corruption
  • Lost transactions
  • Connection leaks

Step 7: Exited State

Container process completed execution.

Example

docker run ubuntu echo "Hello"
    

After command finishes:

Container Exits
    

Check Exited Containers

docker ps -a
    

Exit Codes

Exit Code Meaning
0 Successful exit
1 Application error
137 OOMKilled
143 Graceful shutdown

Step 8: Restarting State

Docker restart policies may restart containers automatically.

Restart Flow

Container Crash
      |
Restart Policy Triggered
      |
Container Restarted
    

Example

docker run --restart=always nginx
    

Restart Policies

Policy Behavior
no No restart
always Always restart
unless-stopped Restart unless manually stopped
on-failure Restart on errors

Step 9: Remove State

Container is deleted permanently.

Command

docker rm container-name
    

Remove Flow

Container Stopped
      |
Metadata Deleted
      |
Writable Layer Removed
      |
Container Gone
    

Important Note

Removing a container does NOT remove:

  • Docker image
  • Volumes (unless explicitly removed)

Container Lifecycle and Storage

Without Volume

Container Removed
      |
Data Lost
    

With Volume

Container Removed
      |
Volume Persists
      |
Data Preserved
    

Container Lifecycle in Production

CI/CD Pipeline
      |
Docker Image Built
      |
Container Created
      |
Health Checks
      |
Traffic Routed
      |
Rolling Updates
      |
Old Containers Removed
    

Container Lifecycle in Kubernetes

Kubernetes automates lifecycle management.

Kubernetes Lifecycle Flow

Pod Created
      |
Container Started
      |
Health Monitored
      |
Failure Detected
      |
Container Recreated
    

Real-Time Production Incident Example

Problem

Payment service containers restarted continuously.

Symptoms

  • Container exits every few seconds
  • Restart loops
  • High CPU usage

Root Cause

Missing DB_PASSWORD environment variable
    

Lifecycle Behavior

Container Starts
      |
Application Fails
      |
Container Exits
      |
Restart Policy Triggers
      |
Loop Repeats
    

Fix

  • Correct environment variables
  • Startup validation added
  • Monitoring alerts configured

Useful Docker Lifecycle Commands

Command Purpose
docker create Create container
docker start Start container
docker stop Stop container
docker restart Restart container
docker pause Pause container
docker unpause Resume container
docker rm Remove container

Container Lifecycle Diagram

          +-----------+
          |  Created  |
          +-----------+
                 |
                 v
          +-----------+
          | Running   |
          +-----------+
           /   |    \
          /    |     \
         v     v      v
     Paused  Stopped Restarting
         |      |        |
         +------+--------+
                 |
                 v
             Removed
    

Container Lifecycle Best Practices

  1. Use health checks
  2. Implement graceful shutdown
  3. Use restart policies carefully
  4. Monitor exit codes
  5. Use persistent volumes
  6. Enable centralized logging
  7. Handle SIGTERM properly

Common Interview Mistakes

  • Confusing image lifecycle with container lifecycle
  • Ignoring pause state
  • Not understanding exit codes
  • Ignoring graceful shutdown
  • Thinking containers persist forever

Interview Answer

The Docker container lifecycle represents the various states a container goes through from creation to execution, pausing, stopping, restarting, and removal.

A container is created from a Docker image, starts running its main process, may be paused or stopped, can restart automatically using restart policies, and is eventually removed when no longer needed.

Docker internally manages the lifecycle using components like Docker Daemon, containerd, runc, Linux namespaces, and cgroups.

Quick Summary Table

Lifecycle Stage Description
Created Container prepared
Running Application executing
Paused Processes frozen
Stopped Application terminated
Restarting Container restarting
Removed Container deleted

Useful Internal Links

Final Conclusion

The Docker container lifecycle is the foundation of container runtime behavior and plays a critical role in deployment, monitoring, scaling, recovery, and troubleshooting in modern cloud-native environments.

Understanding the lifecycle helps DevOps engineers and developers build resilient, scalable, and production-ready containerized systems.

Why this Docker question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.