# Docker Engine (Part 10)

Few errors stop a deployment workflow faster than seeing:

```plaintext
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
```

When this happens—or when Docker containers fail to start unexpectedly—having a structured troubleshooting checklist saves you from blindly restarting servers. In this guide, we will walk through diagnosing Docker daemon connectivity errors, inspecting service logs, reclaiming exhausted disk space, and dynamically toggling **Debug Mode** (`debug: true`) without interrupting running containers.

## 1\. Check Remote Connection Settings (`DOCKER_HOST`)

If you are running Docker CLI commands from a remote client (such as your laptop or a CI/CD runner) targeting a remote Docker host, verify your environment configuration first:

- Confirm the `DOCKER_HOST` environment variable points to the right IP address and port:

```plaintext
echo $DOCKER_HOST
```

- Verify you are using the matching port for your TLS setup:
  - `2375` — Unencrypted TCP traffic (local/private networks only).
  - `2376` — Encrypted TLS traffic (requires `DOCKER_TLS_VERIFY=1` and valid client certificates).
- Test network reachability to the host port (`nc -zv <host_ip> 2376`).

If you are working locally on the Docker host—or your remote network settings are verified—move on to checking the daemon service itself.

## 2\. Check the Docker Service Status & Systemd Logs

On Linux systems, `dockerd` runs as a background service managed by `systemd`.

### Step 1: Check Service State

Run `systemctl status` on the Docker host:

```plaintext
sudo systemctl status docker
```

- `active (running)`**:** The daemon is up (if local CLI commands still fail, check if your user lacks permissions on `/var/run/docker.sock`).
- `inactive (dead)` **or** `failed`**:** The daemon stopped or crashed during startup.

### Step 2: Inspect Daemon Startup Logs (`journalctl`)

To find out *why* the service failed to start, query `systemd`'s journal logs for the `docker` unit:

```plaintext
sudo journalctl -u docker.service -n 50 --no-pager
```

Look for two of the most common root causes in these logs:

1. **Invalid JSON or Directive Conflicts in** `/etc/docker/daemon.json`**:** A trailing comma, syntax error, or duplicate flag (such as defining `"hosts"` in `daemon.json` when `-H fd://` is already passed in `docker.service`) will immediately prevent `dockerd` from starting.
2. **Storage Driver or Disk Errors:** Failures initializing `overlay2` or writing metadata to `/var/lib/docker`.

## 3\. Check Disk Space & Reclaim Storage (`prune`)

Docker stores all images, container layers, build caches, and volumes inside `/var/lib/docker`. When heavy image pulls or large container logs fill up the underlying partition, the Docker daemon can crash or refuse to start new containers.

### Check Available Disk Space

Verify free space on the filesystem hosting `/var/lib/docker`:

```plaintext
df -h /var/lib/docker
```

### Reclaim Unused Docker Storage

Inspect how much disk space Docker objects are consuming:

```plaintext
docker system df
```

Clean up unused resources safely:

```plaintext
# Remove all stopped containers
docker container prune

# Remove dangling/unused images (add -a to remove all images not tied to a container)
docker image prune

# Or reclaim unused containers, networks, and dangling images in one step
docker system prune
```

## 4\. Enabling Docker Daemon Debug Mode Live

By default, Docker logs only high-level lifecycle events (such as container creation or exit messages) to `/var/log/messages` or `/var/log/syslog` and `journalctl`. When diagnosing subtle networking, storage, or runtime issues, you need verbose **debug logs**.

### Step 1: Verify Current Debug Mode

Check whether debug mode is currently enabled:

```plaintext
docker system info | grep -i "debug mode"
# Output: Debug Mode: false
```

### Step 2: Set `"debug": true` in `/etc/docker/daemon.json`

Open `/etc/docker/daemon.json` (create it if it doesn't exist) and set `"debug"` to `true`:

JSON

```plaintext
{
  "debug": true
}
```

### Step 3: Reload the Configuration Without Stopping Containers

Instead of running `systemctl restart docker` (which stops running containers unless Live Restore is enabled), **reload** the configuration cleanly:

**Option A — Using** `systemctl reload`**:**

```plaintext
sudo systemctl reload docker
```

**Option B — Sending** `SIGHUP` **directly to the** `dockerd` **PID:**

Bash

```plaintext
sudo kill -SIGHUP $(pidof dockerd)
```

### Step 4: Verify & Inspect Detailed Logs

Confirm that debug mode is now active:

Bash

```plaintext
docker system info | grep -i "debug mode"
# Output: Debug Mode: true
```

Now, when you run a container (`docker container run --rm hello-world`), inspect your system logs (`sudo tail -f /var/log/messages` on RHEL/CentOS or `sudo journalctl -u docker.service -f` on Ubuntu) to see step-by-step `level=debug` entries detailing image layer mounts, network sandbox creation, and `containerd` calls.

*(Note: Once you finish troubleshooting, set* `"debug": false` *in* `/etc/docker/daemon.json` *and send* `SIGHUP` *again—debug logging generates high log volume and can fill up* `/var/log` *if left on permanently.)*

## Quick Troubleshooting Workflow Summary

<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>Symptom</strong></p></td><td colspan="1" rowspan="1"><p><strong>Diagnostic Command</strong></p></td><td colspan="1" rowspan="1"><p><strong>Fix</strong></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>Cannot connect to the Docker daemon</code></p></td><td colspan="1" rowspan="1"><p><code>sudo systemctl status docker</code></p></td><td colspan="1" rowspan="1"><p>Start service (<code>systemctl start docker</code>) or fix <code>$DOCKER_HOST</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Service fails on startup</strong></p></td><td colspan="1" rowspan="1"><p><code>sudo journalctl -u docker.service -n 50</code></p></td><td colspan="1" rowspan="1"><p>Fix syntax or flag conflicts in <code>/etc/docker/daemon.json</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>No space left on device</strong></p></td><td colspan="1" rowspan="1"><p><code>df -h /var/lib/docker</code> &amp; <code>docker system df</code></p></td><td colspan="1" rowspan="1"><p>Free disk space with <code>docker container prune</code> &amp; <code>docker image prune</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Need verbose runtime logs</strong></p></td><td colspan="1" rowspan="1"><p>Set <code>"debug": true</code> in <code>/etc/docker/daemon.json</code></p></td><td colspan="1" rowspan="1"><p>Reload live with <code>sudo systemctl reload docker</code> or <code>kill -SIGHUP $(pidof dockerd)</code></p></td></tr></tbody></table>
