Docker Container Lifecycle
Every Docker container goes through a lifecycle of states. Understanding these states and how to transition between them is essential for effective container management. This guide covers all container states: created, running, paused, stopped, and deleted.
A Docker container progresses through several distinct states during its lifetime. Understanding these states helps you manage containers effectively, debug issues, and optimize resource usage.
docker ps -a. The STATUS column shows the state: "Up" for running, "Exited" for stopped, "Paused" for paused, "Created" for created but not started.
A container enters the Created state when you use docker create or docker container create. In this state, the container's filesystem has been prepared, but no processes are running. The container exists on disk but consumes minimal resources. This is useful when you want to prepare a container to run later, or when you want to inspect its configuration before starting.
You can also create a container implicitly using docker run --name mycontainer nginx—the container goes through the Created state briefly before transitioning to Running. To see containers in the Created state, use docker ps -a and look for STATUS "Created".
# Create a container without starting it
docker create --name mynginx nginx
# List all containers (including created)
docker ps -a
# Inspect a created container's configuration
docker inspect mynginx
# Start a created container
docker start mynginx
The Running state is the primary active state of a container. In this state, the container's main process is executing. The container consumes CPU, memory, and other resources. You can interact with running containers through docker exec, view their logs, and access their network ports.
A container enters the Running state from Created via docker start, or directly from an image via docker run. From Running, a container can transition to Paused (docker pause), Stopped (docker stop or docker kill), or continue running until its main process exits naturally.
# Run a container (creates and starts)
docker run -d --name web nginx
# Start an existing created container
docker start web
# List only running containers
docker ps
# Execute commands inside a running container
docker exec web ls -la
# View logs from a running container
docker logs -f web
The Paused state freezes all processes inside a container using cgroups freezer. In this state, the container's processes are suspended and not scheduled by the CPU. The container's memory is preserved, and network connections are held. This is different from Stopped (which terminates processes) and is useful for temporarily suspending workloads without losing state.
Paused containers cannot be interacted with via docker exec, and their logs are frozen. Use docker unpause to resume normal operation. Paused containers still consume memory but not CPU cycles.
# Pause a running container
docker pause web
# List containers (paused containers show "Up (Paused)")
docker ps
# Unpause a container
docker unpause web
# Verify container is running again
docker ps
A container enters the Stopped (or Exited) state when its main process completes or is terminated. This can happen naturally (the process finished its work), via docker stop (graceful shutdown with SIGTERM), or via docker kill (immediate SIGKILL). Stopped containers still exist on disk—their filesystem and configuration are preserved, but they consume no CPU or memory.
You can restart a stopped container with docker start, which resumes from the same state (including any data written to the container's filesystem). This is useful for containers that need to run periodically or for debugging after a failure.
# Gracefully stop a container (SIGTERM, then SIGKILL after 10s)
docker stop web
# Immediately kill a container (SIGKILL)
docker kill web
# List all containers including stopped
docker ps -a
# Restart a stopped container
docker start web
# View exit code of a stopped container
docker inspect web --format='{{.State.ExitCode}}'
docker inspect to see why a container stopped. The State.ExitCode and State.Error fields provide valuable debugging information.
Deletion is the final state in a container's lifecycle. When you delete a container with docker rm, the container's filesystem and metadata are removed from disk. Deleted containers cannot be recovered. Any data written to the container's writable layer (not in volumes) is permanently lost.
You can only delete containers that are stopped (or use -f to force delete a running container). Use docker container prune to remove all stopped containers at once, which is useful for cleaning up after testing or development.
# Remove a stopped container
docker rm web
# Force remove a running container (stops it first)
docker rm -f web
# Remove all stopped containers
docker container prune
# Remove all containers (running and stopped)
docker rm -f $(docker ps -aq)
# Clean up everything (containers, images, networks, cache)
docker system prune
| From State | To State | Command |
|---|---|---|
| (None) | Created | docker create <image> |
| Created | Running | docker start <container> |
| (None) | Running | docker run <image> |
| Running | Paused | docker pause <container> |
| Paused | Running | docker unpause <container> |
| Running | Stopped | docker stop <container> |
| Running | Stopped | docker kill <container> |
| Stopped | Running | docker start <container> |
| Created/Stopped | Deleted | docker rm <container> |
| Running | Deleted | docker rm -f <container> |
# 1. CREATE a container (Created state)
docker create --name test nginx
docker ps -a | grep test
# CONTAINER ID IMAGE STATUS NAMES
# abc123 nginx Created test
# 2. START the container (Running state)
docker start test
docker ps | grep test
# abc123 nginx Up 2 seconds test
# 3. PAUSE the container (Paused state)
docker pause test
docker ps | grep test
# abc123 nginx Up 2 seconds (Paused) test
# 4. UNPAUSE the container (Back to Running)
docker unpause test
# 5. STOP the container (Stopped/Exited state)
docker stop test
docker ps -a | grep test
# abc123 nginx Exited (0) 2 seconds ago test
# 6. START again (Running from Stopped)
docker start test
# 7. DELETE the container (Deleted state)
docker stop test
docker rm test
docker ps -a | grep test
# (no output - container is gone)
docker stop sends SIGTERM, allowing the container to shut down gracefully (default 10 seconds). docker kill sends SIGKILL, immediately terminating the process. Always try stop first; use kill only for unresponsive containers.docker create but never started it. Use docker start <container> to run it, or docker rm <container> to remove it.docker rm, its filesystem and metadata are permanently removed. However, if you used volumes for persistent data, that data is still available to new containers.docker run ubuntu echo hello prints "hello" and exits. Use docker run -it ubuntu bash for interactive sessions, or run a long-running process like a web server.--rm flag with docker run: docker run --rm nginx. The container is automatically deleted when it stops, perfect for one-off tasks.docker commit to create a new image from a modified container (not recommended for production).docker inspect <container> --format='{{json .State}}' to see the exit code and error message. Common exit codes: 0 (success), 1 (general error), 137 (SIGKILL), 143 (SIGTERM).Understanding the container lifecycle gives you complete control over your Docker environments. Master these states and transitions to manage containers with confidence.