Skip to main content

Docker Engine (Part 7)

Stopping, Pausing, and Removing Docker Containers: Signals, Cgroups, and Cleanup

Updated 6 min readMarkdown
Part of seriesDemystifying Docker

Stopping or removing a Docker container might look simple on the surface, but knowing what happens under the hood prevents corrupted data, hung shutdowns, and wasted disk space on your host.

In this guide, we will break down how Linux process signals work, how Docker translates those signals into stop, pause, and kill commands, and how to cleanly delete individual or bulk containers from your system.

1. Linux Process Signals: The Foundation

Before looking at Docker commands, it helps to understand how Linux controls regular processes (such as an Apache httpd web server running directly on a host). In Linux, you control a running process by sending it signals using the kill command—either by signal name or by its numeric ID:

Signal

Number

Behavior

Can Process Ignore It?

SIGSTOP

19

Pauses (freezes) the process immediately while preserving its memory state.

No

SIGCONT

18

Resumes a paused process from the exact point where it stopped.

Can be caught/handled

SIGTERM

15

Politely asks the process to shut down, allowing time to close files and finish requests.

Yes

SIGKILL

9

Forcefully terminates the process immediately via the kernel.

No

Sending Signals on a Linux Host

Suppose httpd is running with PID 11663 (or found dynamically using pgrep):

# Pause the process (preserves state in memory)
kill -SIGSTOP $(pgrep httpd)

# Resume the paused process
kill -SIGCONT $(pgrep httpd)

# Politely request termination (allows graceful cleanup)
kill -SIGTERM $(pgrep httpd)

# Forcefully kill an unresponsive process immediately
kill -SIGKILL $(pgrep httpd)

2. How Docker Pauses and Stops Containers

Docker maps these lifecycle concepts directly to containers, with one clever kernel-level twist for pausing.

Pausing and Unpausing (pause / unpause)

Suppose you start a background web container named web:

docker container run -d --name web httpd

To freeze and resume all processes inside that container:

# Freeze the container
docker container pause web

# Resume the container
docker container unpause web

Why Docker Uses freezer Cgroups Instead of SIGSTOP/SIGCONT

You might expect docker container pause to send SIGSTOP and SIGCONT under the hood—but it doesn't.

  • Processes inside a container can detect SIGCONT and run custom signal handlers when resumed, leading to unpredictable behavior.
  • Instead, Docker uses the Linux kernel's freezer cgroup subsystem. When paused, the kernel suspends CPU scheduling for every process in that container's cgroup. The processes remain completely unaware that they were frozen and resume cleanly when unpaused.

Stopping Containers Gracefully (stop)

To stop a running container:

docker container stop web

Under the hood, docker container stop performs a two-step shutdown:

  1. Docker sends SIGTERM (signal 15) to the container's main process (PID 1), giving your application time to close database connections, flush logs, and exit cleanly.
  2. Docker waits for a 10-second grace period (customizable with -t <seconds>). If the container is still running after the grace period expires, Docker sends SIGKILL (signal 9) to terminate it immediately.

(Note: If your application inside the container doesn't forward SIGTERM to PID 1—for example, when wrapped in an improper shell script—docker stop will always hang for 10 seconds before force-killing it.)

Sending Custom Signals (kill)

If you want to skip the grace period or send a specific Linux signal (like SIGHUP to reload configuration files) directly to PID 1 inside a container, use docker container kill:

# Immediately send SIGKILL (default)
docker container kill web

# Send a custom signal such as SIGHUP or SIGTERM
docker container kill --signal=SIGHUP web

3. Removing Containers and Reclaiming Disk Space

When you pause or stop a container, its filesystem directory under /var/lib/docker/containers/<container-id>/ remains intact on disk. That is why you can restart a stopped container later without losing its writable layer—and why stopped containers continue consuming disk space.

Removing a Single Container (rm)

To permanently delete a stopped container and wipe its directory from /var/lib/docker/containers/:

docker container rm web

By default, Docker prevents you from deleting a running container. You must stop it first—or pass -f (--force), which sends SIGKILL and removes it in one step:

# Standard two-step removal
docker container stop web
docker container rm web

# Force stop + remove in one command
docker container rm -f web

4. Bulk Cleanup & Self-Destructing Containers

In active development or CI/CD environments, stopped containers pile up quickly. Use these three patterns to keep your Docker host clean:

1. Stop and Remove All Containers via Subshells ($())

Combine docker container ls -q (which outputs only Container IDs) with shell command substitution:

# Stop all currently running containers
docker container stop $(docker container ls -q)

# Remove ALL containers (running + stopped)
docker container rm $(docker container ls -aq)

2. Clean Up All Stopped Containers (prune)

To delete every stopped container at once without touching running ones—and see exactly how much disk space was freed—use prune:

docker container prune

(Add -f to skip the confirmation prompt in automated scripts: docker container prune -f.)

3. Auto-Remove Ephemeral Containers (--rm)

When running one-off tasks, scripts, or quick tests where you don't need the container afterward, pass the --rm flag to docker container run:

docker container run --rm ubuntu echo "Task complete!"

As soon as the container finishes executing and exits, Docker automatically deletes it and cleans up its writable layer.

Quick Reference Cheat Sheet

Action

Command

Under the Hood

Pause container

docker container pause <name>

Suspends processes via Linux freezer cgroup

Unpause container

docker container unpause <name>

Resumes cgroup scheduling

Graceful stop

docker container stop <name>

Sends SIGTERM → waits 10s → sends SIGKILL

Force kill / custom signal

docker container kill --signal=<SIG> <name>

Sends specified signal directly to PID 1

Remove stopped container

docker container rm <name>

Deletes /var/lib/docker/containers/<id>

Remove all stopped containers

docker container prune

Wipes all exited containers & reports freed disk space

Auto-remove on exit

docker container run --rm <image>

Deletes container immediately once its process exits

No comments yet. Be the first to comment.