# Docker Engine (Part 3)

By default, Docker runs quietly in the background as a system service, listening strictly to local commands on the same machine. However, when you need to debug startup failures, connect to a remote Docker host from your laptop, or standardize daemon settings across production servers, you need to configure `dockerd` directly.

This guide breaks down how the Docker daemon communicates, how to safely expose it over a network using TLS encryption, and how to persist configurations cleanly using `daemon.json` without triggering `systemd` conflicts.

## 1\. Running `dockerd`: Background Service vs. Foreground Debugging

On modern Linux distributions, the Docker daemon is managed by `systemd` (`systemctl`), which runs `dockerd` in the background and restarts it automatically on boot:

```plaintext
sudo systemctl start docker
sudo systemctl status docker
sudo systemctl stop docker
```

### Troubleshooting in the Foreground

If the Docker service refuses to start or crashes silently, stop the `systemd` service and invoke the `dockerd` binary directly in the foreground to stream live logs straight to your terminal:

```plaintext
sudo systemctl stop docker
sudo dockerd
```

To capture verbose diagnostic output (storage driver initialization, network bridge setup, gRPC calls), pass the `--debug` (or `-D`) flag:

```plaintext
sudo dockerd --debug
```

## 2\. How the Docker CLI Talks to `dockerd` (Unix Socket)

When `dockerd` starts with default settings, it binds to a local **Unix domain socket** at:

```plaintext
/var/run/docker.sock
```

A Unix socket is an IPC file rather than a network port. Because of this:

- Communication is fast and never leaves the host machine.
- Only local processes (and users with `root` or `docker` group permissions) can send commands to the daemon.
- A Docker CLI installed on your laptop **cannot** reach a Docker host running on a remote cloud server out of the box.

## 3\. Exposing the Docker Daemon Over TCP (Remote Access)

Suppose you have Docker Engine running on a remote Linux server (`192.168.1.10`) and want to control it using the Docker CLI on your local laptop. You can instruct `dockerd` to listen on a TCP network interface using the `-H` (`--host`) flag.

### Step 1: Bind `dockerd` to a TCP Interface

On the remote Docker host (`192.168.1.10`), start `dockerd` with both the local Unix socket and a TCP socket on port `2375`:

```plaintext
sudo dockerd \
  -H unix:///var/run/docker.sock \
  -H tcp://192.168.1.10:2375

(Note: Including -H unix:///var/run/docker.sock ensures local CLI commands on the server itself continue working alongside remote connections.)
```

### Step 2: Point Your Local CLI to the Remote Host

On your laptop, set the `DOCKER_HOST` environment variable before running standard Docker commands:

```plaintext
export DOCKER_HOST="tcp://192.168.1.10:2375"
docker ps
docker images
```

> **Critical Security Warning:** Port `2375` serves **unencrypted traffic with zero authentication**. Never expose port `2375` on a public IP address or untrusted network. Automated scanners actively hunt for open port `2375` endpoints to deploy unauthorized crypto-mining containers and gain full root-equivalent access to the host.

## 4\. Securing Remote Access with TLS (Port `2376`)

Whenever you expose the Docker API over a network, you must encrypt traffic and enforce mutual authentication using **TLS (Transport Layer Security)** certificates.

By convention, Docker uses two distinct standard ports:

<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>Port</strong></p></td><td colspan="1" rowspan="1"><p><strong>Security Mode</strong></p></td><td colspan="1" rowspan="1"><p><strong>Usage</strong></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>2375</code></p></td><td colspan="1" rowspan="1"><p>Unencrypted HTTP</p></td><td colspan="1" rowspan="1"><p>Local/isolated testing only (never public).</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>2376</code></p></td><td colspan="1" rowspan="1"><p>Encrypted HTTPS (TLS)</p></td><td colspan="1" rowspan="1"><p>Standard port for secure remote daemon access.</p></td></tr></tbody></table>

### Configuring TLS on the Docker Host

Once you have generated a Certificate Authority (`ca.pem`), server certificate (`server-cert.pem`), and server key (`server-key.pem`), launch `dockerd` with `--tlsverify` enabled on port `2376`:

```plaintext
sudo dockerd \
  --tlsverify \
  --tlscacert=/etc/docker/certs/ca.pem \
  --tlscert=/etc/docker/certs/server-cert.pem \
  --tlskey=/etc/docker/certs/server-key.pem \
  -H unix:///var/run/docker.sock \
  -H tcp://192.168.1.10:2376
```

### Connecting Securely from the Client Laptop

Copy the client certificates (`ca.pem`, `cert.pem`, `key.pem`) to your laptop (typically inside `~/.docker/`), then connect using TLS flags or environment variables:

```plaintext
export DOCKER_HOST="tcp://192.168.1.10:2376"
export DOCKER_TLS_VERIFY="1"
export DOCKER_CERT_PATH="$HOME/.docker/certs"

docker ps
```

## 5\. Persisting Configuration with `/etc/docker/daemon.json`

Passing flags directly to `dockerd` on the command line works for quick tests, but it does not persist when Docker runs as a background service via `systemctl`.

Instead, Docker reads its configuration from a JSON file located at:

```plaintext
/etc/docker/daemon.json
```

This file does **not** exist by default—create it manually when you need custom daemon settings.

### Example `daemon.json` Configuration

Every CLI flag maps to a JSON key-value pair. Note that `hosts` accepts an **array** so the daemon can listen on multiple sockets simultaneously:

```json
{
  "debug": true,
  "hosts": [
    "unix:///var/run/docker.sock",
    "tcp://192.168.1.10:2376"
  ],
  "tls": true,
  "tlsverify": true,
  "tlscacert": "/etc/docker/certs/ca.pem",
  "tlscert": "/etc/docker/certs/server-cert.pem",
  "tlskey": "/etc/docker/certs/server-key.pem"
}
```

After saving `/etc/docker/daemon.json`, restart the service so `systemd` loads the new settings:

```plaintext
sudo systemctl restart docker
```

## 6\. Avoiding Directive Conflicts (`daemon.json` vs. CLI Flags)

A common pitfall when configuring `daemon.json` is specifying the **same option (directive)** in both `daemon.json` and the `dockerd` startup command.

If a setting conflicts—for example, `"debug": true` is set in `daemon.json` while `--debug=false` is passed on the command line—`dockerd` will refuse to start and throw an error:

Plaintext

```plaintext
unable to configure the Docker daemon with file /etc/docker/daemon.json:
the following directives are specified both as a flag and in the configuration file: debug: (from flag: false, from file: true)
```

### Fixing the `hosts` Conflict on `systemd` (Ubuntu/CentOS)

On Ubuntu and CentOS, the default `systemd` unit file starts Docker with `-H fd://` already baked into the `ExecStart` command:

```plaintext
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
```

If you add `"hosts": [...]` to `/etc/docker/daemon.json` without overriding that default flag, Docker will fail to start due to a `hosts` directive conflict. To resolve it cleanly:

1. Create a `systemd` drop-in override file:

   Bash

   ```plaintext
   sudo systemctl edit docker.service
   ```
2. Clear the existing `ExecStart` line and redefine it without `-H`:

   Ini, TOML

   ```plaintext
   [Service]
   ExecStart=
   ExecStart=/usr/bin/dockerd --containerd=/run/containerd/containerd.sock
   ```
3. Reload `systemd` and restart Docker:

   Bash

   ```plaintext
   sudo systemctl daemon-reload
   sudo systemctl restart docker
   ```
