Managing Docker Container Lifecycle and States (Beginner-Friendly Guide with Examples)

In Docker, containers are not just created and run—they go through different states during their lifecycle. Understanding these states helps you manage applications better, debug issues, and build reliable systems.

This guide explains container lifecycle in a simple way with diagrams, commands, and real-world examples. :contentReference[oaicite:0]{index=0}

Related internal topics: /docker-cli-commands, /docker-images-containers, /docker-architecture, /docker-troubleshooting.

1. What is Container Lifecycle?

The container lifecycle refers to all the stages a container goes through—from creation to deletion.

Unlike virtual machines, containers have multiple states that help in better control and management.

Simple Flow

Create → Run → Pause → Stop → Delete

2. Docker Container States

Each container moves through the following states:

1. Created

The container is created but not started.

Example

docker create nginx

This prepares the container but does not run it.

2. Running

The container is actively running and using system resources.

Example

docker start container_id

or directly:

docker run nginx

3. Paused

The container is temporarily stopped (no CPU usage but memory remains).

Example

docker pause container_id

4. Exited

The container has stopped running.

Example

docker stop container_id

5. Deleted

The container is removed from the system.

Example

docker rm container_id

3. Lifecycle Flow Diagram

[ Image ]
   |
   v
[ Created ]
   |
   v
[ Running ] <-----> [ Paused ]
   |
   v
[ Exited ]
   |
   v
[ Deleted ]

4. Important Lifecycle Commands

Create Container

docker create --name my-app nginx

Explanation:

  • Creates container
  • Does not start it

Start Container

docker start my-app

Moves container to running state.

Run Container (Shortcut)

docker run nginx

This command does both create and start.

Pause Container

docker pause my-app

Stops CPU usage temporarily.

Unpause Container

docker unpause my-app

Resumes container.

Stop Container

docker stop my-app

Gracefully stops container.

Kill Container

docker kill my-app

Force stops container immediately.

Remove Container

docker rm my-app

Deletes container permanently.

5. Stop vs Kill (Important)

docker stop

  • Graceful shutdown
  • Gives time to save data

docker kill

  • Immediate shutdown
  • May cause data loss

Example

docker stop my-app
docker kill my-app

6. Real-World Use Cases

1. Application Maintenance

Pause containers during system updates without stopping them completely.

2. CI/CD Pipelines

Containers are created, run tests, and then removed automatically.

Example

docker run --rm my-test-app

3. Microservices Scaling

If a service fails (exited), it can be restarted automatically.

4. Debugging Applications

Check exited containers to find errors.

7. Practical Example

Run a web server container:

docker run -d --name web -p 8080:80 nginx

Check running containers:

docker ps

Stop container:

docker stop web

Remove container:

docker rm web

8. Flow Chart: Container Lifecycle Commands

docker create
     |
     v
docker start
     |
     v
docker pause <--> docker unpause
     |
     v
docker stop
     |
     v
docker rm

9. Common Mistakes to Avoid

1. Leaving Exited Containers

They consume disk space.

Solution

docker system prune

2. Confusing Image and Container

Image = template, Container = running instance.

3. Using Kill Instead of Stop

May cause data loss.

4. Not Naming Containers

Hard to manage containers without names.

5. Ignoring Logs

Always check logs for debugging.

10. Interview Notes

What are container states?

Created, Running, Paused, Exited, Deleted.

Difference between stop and kill?

Stop = graceful, Kill = force.

Why container exits immediately?

Main process (PID 1) stopped.

How to remove running container?

docker rm -f container_id

What is docker run?

Creates and starts container.

11. Learning Path

  • /docker-logs-troubleshooting
  • /docker-compose-guide
  • /kubernetes-introduction

12. Summary

Docker containers go through multiple lifecycle states such as created, running, paused, exited, and deleted.

Understanding these states helps you manage applications efficiently, debug problems, and optimize resources.

Using the right commands like docker run, docker stop, and docker rm ensures smooth container management.

Mastering container lifecycle is essential for working with microservices and modern cloud applications.