# Docker Engine (Part 7)

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:

<table style="min-width: 100px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><td colspan="1" rowspan="1"><p><strong>Signal</strong></p></td><td colspan="1" rowspan="1"><p><strong>Number</strong></p></td><td colspan="1" rowspan="1"><p><strong>Behavior</strong></p></td><td colspan="1" rowspan="1"><p><strong>Can Process Ignore It?</strong></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>SIGSTOP</code></p></td><td colspan="1" rowspan="1"><p><code>19</code></p></td><td colspan="1" rowspan="1"><p>Pauses (freezes) the process immediately while preserving its memory state.</p></td><td colspan="1" rowspan="1"><p>No</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>SIGCONT</code></p></td><td colspan="1" rowspan="1"><p><code>18</code></p></td><td colspan="1" rowspan="1"><p>Resumes a paused process from the exact point where it stopped.</p></td><td colspan="1" rowspan="1"><p>Can be caught/handled</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>SIGTERM</code></p></td><td colspan="1" rowspan="1"><p><code>15</code></p></td><td colspan="1" rowspan="1"><p>Politely asks the process to shut down, allowing time to close files and finish requests.</p></td><td colspan="1" rowspan="1"><p><strong>Yes</strong></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>SIGKILL</code></p></td><td colspan="1" rowspan="1"><p><code>9</code></p></td><td colspan="1" rowspan="1"><p>Forcefully terminates the process immediately via the kernel.</p></td><td colspan="1" rowspan="1"><p><strong>No</strong></p></td></tr></tbody></table>

### Sending Signals on a Linux Host

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

```plaintext
# 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`:

```plaintext
docker container run -d --name web httpd
```

To freeze and resume all processes inside that container:

```plaintext
# 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:

```plaintext
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`:

```plaintext
# 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/`:

```plaintext
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:

```plaintext
# 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:

```plaintext
# 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`:

```plaintext
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`:

```plaintext
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

<table style="min-width: 75px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><td colspan="1" rowspan="1"><p><strong>Action</strong></p></td><td colspan="1" rowspan="1"><p><strong>Command</strong></p></td><td colspan="1" rowspan="1"><p><strong>Under the Hood</strong></p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Pause container</strong></p></td><td colspan="1" rowspan="1"><p><code>docker container pause &lt;name&gt;</code></p></td><td colspan="1" rowspan="1"><p>Suspends processes via Linux <code>freezer</code> cgroup</p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Unpause container</strong></p></td><td colspan="1" rowspan="1"><p><code>docker container unpause &lt;name&gt;</code></p></td><td colspan="1" rowspan="1"><p>Resumes cgroup scheduling</p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Graceful stop</strong></p></td><td colspan="1" rowspan="1"><p><code>docker container stop &lt;name&gt;</code></p></td><td colspan="1" rowspan="1"><p>Sends <code>SIGTERM</code> → waits 10s → sends <code>SIGKILL</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Force kill / custom signal</strong></p></td><td colspan="1" rowspan="1"><p><code>docker container kill --signal=&lt;SIG&gt; &lt;name&gt;</code></p></td><td colspan="1" rowspan="1"><p>Sends specified signal directly to PID 1</p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Remove stopped container</strong></p></td><td colspan="1" rowspan="1"><p><code>docker container rm &lt;name&gt;</code></p></td><td colspan="1" rowspan="1"><p>Deletes <code>/var/lib/docker/containers/&lt;id&gt;</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Remove all stopped containers</strong></p></td><td colspan="1" rowspan="1"><p><code>docker container prune</code></p></td><td colspan="1" rowspan="1"><p>Wipes all exited containers &amp; reports freed disk space</p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Auto-remove on exit</strong></p></td><td colspan="1" rowspan="1"><p><code>docker container run --rm &lt;image&gt;</code></p></td><td colspan="1" rowspan="1"><p>Deletes container immediately once its process exits</p></td></tr></tbody></table>
