Managing Container Lifecycle
Understanding how to manage a container's lifecycle is crucial for effective Docker usage. In this step, we'll explore various commands to control container states and understand the container lifecycle.
Container Lifecycle States
A Docker container can exist in several states:
- Created: Container is created but not started
- Running: Container is currently running
- Paused: Container execution is paused
- Stopped: Container is stopped but still exists
- Removed: Container is deleted
Let's explore how to transition between these states.
Creating a Container Without Starting It
You can create a container without starting it using the create
command:
docker create --name test-container nginx
This creates a container but doesn't start it. Check its status:
docker ps -a
You should see the new container with a "Created" status:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f4ab92d1234 nginx "/docker-entrypoint.…" 5 seconds ago Created test-container
a72369167c21 nginx "/docker-entrypoint.…" 40 minutes ago Up 30 minutes 0.0.0.0:8080->80/tcp my-nginx
bc123def456a redis "docker-entrypoint.s…" 10 minutes ago Up 10 minutes 6379/tcp redis-server
Starting a Created Container
To start the created container:
docker start test-container
Verify it's running:
docker ps
Pausing and Unpausing Containers
Docker allows you to pause a container, which freezes all processes inside it:
docker pause test-container
Check its status:
docker ps
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f4ab92d1234 nginx "/docker-entrypoint.…" 2 minutes ago Up 1 minute (Paused) test-container
a72369167c21 nginx "/docker-entrypoint.…" 42 minutes ago Up 32 minutes 0.0.0.0:8080->80/tcp my-nginx
bc123def456a redis "docker-entrypoint.s…" 12 minutes ago Up 12 minutes 6379/tcp redis-server
To resume the container:
docker unpause test-container
Verify it's running again:
docker ps
Stopping and Restarting Containers
To stop a container gracefully (sending SIGTERM, then SIGKILL after a grace period):
docker stop test-container
To forcefully kill a container (sending SIGKILL):
docker start test-container ## Start it again first
docker kill test-container ## Then kill it
To restart a container (stops and starts it again):
docker start test-container ## Start it again first
docker restart test-container
Removing Containers
To remove a stopped container:
docker stop test-container ## Make sure it's stopped first
docker rm test-container
Verify it's gone:
docker ps -a | grep test-container
You should get no output, indicating the container has been removed.
Removing a Running Container
You can force-remove a running container:
docker run --name temp-container -d nginx
docker rm -f temp-container
Container Restart Policies
Docker allows you to set restart policies for containers:
docker run --name always-restart --restart always -d nginx
This container will restart automatically if it exits or if Docker restarts.
Check the restart policy:
docker inspect --format='{{.HostConfig.RestartPolicy.Name}}' always-restart
Output:
always
Let's stop and remove this container:
docker rm -f always-restart
Understanding these lifecycle commands gives you complete control over your Docker containers, allowing you to efficiently manage their states based on your application needs.