Skip to main content
In this guide, we’ll explore how to manage Docker containers by stopping, pausing, resuming, and removing them. You’ll also learn how Linux signals map to Docker commands, plus best practices for cleaning up containers to reclaim resources.

1. Linux Signals Refresher

Linux processes respond to signals for control and shutdown. Below is a quick overview:
SIGTERM is the preferred way to stop a process because it allows cleanup. SIGKILL should be used only if the process does not terminate gracefully.

2. Docker Container Equivalents

Docker uses similar primitives at the container level. The table below shows the mappings:

2.1 Running an HTTPD Container

2.2 Pause and Resume

2.3 Stop (SIGTERM then SIGKILL)

Docker sends SIGTERM, waits the default 10 seconds, then sends SIGKILL if the container is still running.

3. Sending Custom Signals

You can target any signal to a container’s main process:

4. Removing a Container

Containers must be stopped before removal:
Attempting to remove a running container yields an error:

5. Batch Stopping and Removing

When managing multiple containers, leverage docker ps -q and docker ps -aq:

6. Pruning Stopped Containers

To delete all stopped containers and free disk space:
docker container prune permanently deletes all stopped containers. There is no undoing this action.

7. Automatic Cleanup with —rm

For ephemeral containers, use --rm to remove them automatically after exit:
This is ideal for one-off tasks, CI jobs, or simple shell commands.

Watch Video