Skip to main content

Managing Container Lifecycle

1. Listing Containers

Command Description Options Example
docker ps List running containers -a to show all containers
-q to show only container IDs
docker ps -a
docker ps -a List all containers (running and stopped) -q to show only container IDs
--format to customize output format
docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.Status}}"
docker ps -q Show only container IDs -a to include stopped containers docker ps -q

2. Starting Stopped Containers

Command Description Example
docker start Starts one or more stopped containers docker start container_name
docker start -a Starts container and attaches to its stdout/stderr docker start -a container_name
docker start -i Starts container and attaches to its stdin, stdout, stderr docker start -i container_name

Example: Starting a Stopped Container

docker start my_web_app
Note: The container must already exist in a stopped state. Use docker ps -a to list all containers including stopped ones.

3. Stopping Running Containers

Command Description Options
docker stop Stops a running container gracefully by sending SIGTERM signal -t, --time seconds to wait before killing the container
docker kill Kills a running container immediately by sending SIGKILL signal -s, --signal signal to send (default: KILL)

Example: Stopping a Container Gracefully

docker stop my-container

Example: Stopping a Container with Timeout

docker stop -t 10 my-container

Example: Killing a Container Immediately

docker kill my-container
Note: docker stop allows the container to shut down gracefully, while docker kill forces it to stop immediately.

4. Restarting Containers

Option Values Description
--restart no Do not automatically restart the container (default)
--restart always Always restart the container if it stops or crashes
--restart unless-stopped Restart the container unless it was explicitly stopped by the user
--restart on-failure[:max-retries] Restart the container on failure, up to a maximum number of retries (default is 0)

Example: Restart policy with on-failure

docker run --restart on-failure:5 my-app

Example: Always restart container

docker run --restart always my-app
Note: The --restart policy is applied at container runtime and persists across Docker daemon restarts.
Warning: Using always or unless-stopped can lead to infinite restart loops if the container keeps crashing.

5. Pausing Containers

Command Description Notes
docker pause <container> Pause all processes in a container Container remains in the running state but all processes are suspended
docker unpause <container> Unpause a paused container Resumes all processes in the container

Example: Pausing and Unpausing a Container

docker pause my-container
docker unpause my-container
Note: Pausing a container does not save its state to disk. It only suspends the processes in memory.

6. Unpausing Containers

Command Description Notes
docker unpause Unpause one or more paused containers Container must be in 'paused' state

Example: Unpause a Container

docker unpause my-container
Note: The container must be in a paused state for this command to work. Use docker ps --filter status=paused to list all paused containers.
Warning: Unpausing a container does not restart it if it was stopped; it only resumes execution from where it was paused.

7. Killing Containers (docker kill)

Option Values Description
docker kill CONTAINER Kills one or more running containers
docker kill --signal SIGTERM or SIGKILL Specify the signal to send (default is SIGTERM)

Example: Killing a Container

docker kill my_container

Example: Killing a Container with Custom Signal

docker kill --signal SIGKILL my_container
Note: The docker kill command sends a signal to the main process inside the container. By default, it sends SIGTERM, which allows the process to shut down gracefully. If you want to force termination, use SIGKILL.

8. Removing Containers (docker rm)

Command Description Options
docker rm Remove one or more containers -f, -l, -v

Example: Remove a container by name

docker rm my-container

Example: Remove a container by ID

docker rm abc123456789

Example: Remove multiple containers

docker rm container1 container2 container3

Example: Force remove a running container

docker rm -f my-running-container

Example: Remove container and its volumes

docker rm -v my-container
Note: Use docker rm -f to remove a running container. By default, Docker will not remove a running container.
Warning: Removing a container will delete all its data. Ensure you have backed up any important information before removing.

9. Renaming Containers (docker rename)

Option Values Description
docker rename docker rename [OPTIONS] CONTAINER NEW_NAME Renames a running or stopped container to a new name

Example: Rename a Container

# Rename a container named "old_container" to "new_container"
docker rename old_container new_container
Note: The new name must be unique within the Docker daemon. If a container with the same name already exists, the command will fail.

10. Waiting for Container Exit (docker wait)

The docker wait command blocks until a container stops running and then prints the exit code of the container.

Option Values Description
container <container> Name or ID of the container to wait for

Example: Waiting for a container to exit

docker run -d --name my-app nginx
docker wait my-app
Note: The command returns the exit code of the container once it stops running.

11. Updating Running Container

Command Description Notes
docker update Update configuration of one or more running containers Can be used to change resource limits, restart policies, etc.

Example: Update Memory Limit of a Running Container

docker update --memory 1g container_name
Note: The docker update command does not restart the container, but it applies changes to the running container's configuration.
Warning: Updating a container's configuration while it is running may cause performance issues or instability if the changes are too aggressive (e.g., setting memory limits too low).

12. Creating Container Without Starting

Option Values Description
docker create --name container_name Creates a container with the specified name but does not start it.
docker create --image image_name Specifies the base image for the container.
docker create --volume volume_name:/mount/path Mounts a volume to the container's filesystem.
docker create --env KEY=VALUE Sets environment variables inside the container.

Example: Creating a Container Without Starting It

docker create --name my_container --image nginx:latest --env NODE_ENV=production
Note: The container created with docker create is in a "created" state and must be started using docker start.

Was this article helpful?