Skip to content

Expose dockerd over TLS — mutual auth for remote daemons

Set up the Docker daemon to listen on a TCP port with mutual TLS, generate CA/server/client certs, and lock the API down so only authorized clients can drive it.

The Docker daemon’s API is full root on the host — anyone who can reach it can run privileged containers, mount the host’s root filesystem, and own the box. Exposing it over plain TCP is one of the classic ways to lose a server in under an hour because internet scanners look for port 2375 specifically. The supported way to make the daemon reachable for CI runners, dashboards, or remote build agents is mutual TLS on port 2376 — a CA you control, a server cert for the daemon, and per-client certs for every operator or service. This runbook covers the full setup, key hygiene, and the failure modes you’ll meet.

How to verify

Before changing anything, check whether the daemon is already listening on TCP and how:

# Is dockerd bound to anything beyond the Unix socket?
sudo ss -lntp | grep -E ':(2375|2376)'
sudo systemctl cat docker | grep -i hosts

# Daemon config
sudo cat /etc/docker/daemon.json 2>/dev/null

# Test the API from the client side once configured
curl --cacert ca.pem --cert cert.pem --key key.pem \
  https://docker-host.example.com:2376/version

Port 2375 is plain HTTP — a configuration error if present in production. Port 2376 by convention is TLS. The --tlsverify flag on both daemon and client enforces mutual auth (server validates client cert, client validates server cert).

What’s happening

A TLS-enabled Docker daemon validates clients with a certificate chain. You build a private CA, sign a server certificate for the daemon (containing the host’s DNS name or IP in the SAN), and sign one client certificate per operator. The daemon refuses any client whose cert isn’t signed by your CA. Clients refuse any daemon whose cert isn’t signed by the same CA.

This is the same primitive that the Kubernetes API server uses for kubectl auth. The model maps cleanly: one CA = one cluster of trust. Revoke a client by removing it from the CA’s accepted set (in practice: rotate the CA when an operator leaves, since Docker daemons don’t natively check CRLs).

There’s no built-in authorization layer beyond “valid client cert = full access”. If you need RBAC, put an authentication proxy (e.g. nginx with cert validation) in front and have it forward only specific API paths. For most teams, the right granularity is one daemon per environment, with each operator’s cert valid across all of them.

The procedure

  1. Generate a CA on a workstation you trust (not on the Docker host — keep the CA key out of any server that’s actually exposed):
mkdir docker-ca && cd docker-ca
openssl genrsa -aes256 -out ca-key.pem 4096
openssl req -new -x509 -days 3650 -key ca-key.pem -sha256 -out ca.pem \
  -subj "/CN=docker-ca.example.com"

Store ca-key.pem somewhere offline (encrypted USB, secret manager). You only need it again to issue new client/server certs.

  1. Generate the server certificate. The CN doesn’t matter — Docker only checks the SAN (subject alternative name) — so include every hostname and IP a client might use:
HOST=docker-01.example.com
openssl genrsa -out server-key.pem 4096
openssl req -subj "/CN=$HOST" -sha256 -new -key server-key.pem -out server.csr

cat > extfile.cnf <<EOF
subjectAltName = DNS:$HOST,DNS:docker-01,IP:10.0.1.42,IP:127.0.0.1
extendedKeyUsage = serverAuth
EOF

openssl x509 -req -days 365 -sha256 -in server.csr \
  -CA ca.pem -CAkey ca-key.pem -CAcreateserial \
  -out server-cert.pem -extfile extfile.cnf
  1. Generate a client certificate (one per operator or service):
openssl genrsa -out client-key.pem 4096
openssl req -subj '/CN=alice' -new -key client-key.pem -out client.csr

cat > extfile-client.cnf <<EOF
extendedKeyUsage = clientAuth
EOF

openssl x509 -req -days 365 -sha256 -in client.csr \
  -CA ca.pem -CAkey ca-key.pem -CAcreateserial \
  -out client-cert.pem -extfile extfile-client.cnf

chmod 0400 ca-key.pem server-key.pem client-key.pem
chmod 0444 ca.pem server-cert.pem client-cert.pem
  1. Install ca.pem, server-cert.pem, and server-key.pem on the Docker host:
sudo install -o root -g root -m 0700 -d /etc/docker/tls
sudo install -o root -g root -m 0444 ca.pem server-cert.pem /etc/docker/tls/
sudo install -o root -g root -m 0400 server-key.pem /etc/docker/tls/
  1. Configure the daemon. The cleanest way is /etc/docker/daemon.json — never touch the systemd unit directly:
{
  "hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2376"],
  "tls": true,
  "tlsverify": true,
  "tlscacert": "/etc/docker/tls/ca.pem",
  "tlscert": "/etc/docker/tls/server-cert.pem",
  "tlskey": "/etc/docker/tls/server-key.pem"
}

If you set hosts here, you must also remove the -H flag from systemd or dockerd will refuse to start due to duplicate host definitions:

sudo mkdir -p /etc/systemd/system/docker.service.d
sudo tee /etc/systemd/system/docker.service.d/override.conf <<EOF
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd
EOF

sudo systemctl daemon-reload
sudo systemctl restart docker
  1. Lock the firewall down so only authorized client networks can reach 2376:
sudo ufw allow from 10.0.0.0/8 to any port 2376 proto tcp
sudo ufw deny 2376/tcp
sudo ufw reload

Even with mTLS, narrowing the network exposure reduces noise from scanners and limits the blast radius of any future TLS bug.

  1. On the client, install ca.pem, client-cert.pem, and client-key.pem into ~/.docker/:
mkdir -p ~/.docker
mv ca.pem ~/.docker/ca.pem
mv client-cert.pem ~/.docker/cert.pem
mv client-key.pem ~/.docker/key.pem
chmod 0400 ~/.docker/key.pem

export DOCKER_HOST=tcp://docker-01.example.com:2376
export DOCKER_TLS_VERIFY=1
docker version

Or create a docker context (cleaner — no env exports drifting between shells), see Docker contexts — operate multiple hosts:

docker context create prod \
  --docker "host=tcp://docker-01.example.com:2376,ca=$HOME/.docker/ca.pem,cert=$HOME/.docker/cert.pem,key=$HOME/.docker/key.pem"
docker --context prod ps
  1. Rotate certificates on a calendar — at minimum yearly, and immediately when an operator leaves. Keep a renewal script in source control next to the CA:
# Each year on the daemon
openssl x509 -req -days 365 -sha256 -in server.csr \
  -CA ca.pem -CAkey ca-key.pem -CAserial ca.srl \
  -out server-cert.pem -extfile extfile.cnf
sudo cp server-cert.pem /etc/docker/tls/
sudo systemctl reload docker

Common pitfalls

  • daemon.json with hosts conflicts with the default dockerd -H fd:// in the systemd unit. Without the override, dockerd refuses to start with 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: hosts.
  • Generating a server cert without the daemon’s actual hostname in the SAN. Clients fail with x509: certificate is valid for X, not Y.
  • chmod 0400 on key files is non-negotiable — Docker refuses to load keys with broader permissions.
  • Leaving port 2375 (plain HTTP) open by accident. Always cross-check ss -lntp after restart; if you see 2375 listening, the daemon isn’t enforcing TLS.
  • Putting the CA key on the same host as the daemon. If that host is compromised, the attacker can mint client certs at will. Keep the CA key offline or in a secret manager.
  • Using a self-signed cert on the server but not distributing the CA to clients. Curl with --insecure proves the daemon is reachable but isn’t a real test — clients without the CA can’t validate it.

We run mTLS-protected daemons on every multi-operator Docker host we manage, with CA keys in a vault and a rotation calendar that fires before client certs expire. It’s the minimum that lets a CI fleet or a dashboard reach a daemon without standing up SSH bastions for every caller, and it’s a standard part of our managed operations hardening pass.