Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JonL1/93c6b54cb422083db46fad7880cd091a to your computer and use it in GitHub Desktop.
Save JonL1/93c6b54cb422083db46fad7880cd091a to your computer and use it in GitHub Desktop.
Docker Remote API with client verification via daemon.json

Enable Docker Remote API with TLS client verification

Docker's Remote API can be secured via TLS and client certificate verification.
First of all you need a few certificates and keys:

  • CA certificate
  • Server certificate
  • Server key
  • Client certificate
  • Client key

Create certificate files

You can create these files as described in the official docs in Protect the Docker daemon socket.

You can also use my forked create-certs.sh script to create them.

If you choose this path follow this two steps before creating the certificates.

wget https://raw.githubusercontent.com/JonL1/linux-utils/master/cert-generate/create-certs.sh
chmod +x ./create-certs.sh

Next run the script like this:

  1. Create a CA with the password yourSecretPassword and 900 days until it wil expire. The cert files will be in the directory ./certs.
./create-certs.sh -m ca -pw yourSecretPassword -t certs -e 900
  1. Create server certificate and key with the password of step 1 yourSecretPassword, with the servername myserver.example.com and 365 days until it wil expire. The cert files will be in the directory ./certs.
./create-certs.sh -m server -h myserver.example.com -pw yourSecretPassword -t certs -e 365
  1. Create client certificate and key with the password of step 1 yourSecretPassword, with the clientname testClient (the name is interesting if you want to use authorization plugins later) and 365 days until it wil expire. The cert files will be in the directory ./certs.
./create-certs.sh -m client -h testClient -pw yourSecretPassword -t certs -e 365

Now you have a directory ./certs with certificates and keys for CA, server and client.

Enable Remote API with TLS

Make sure, you have a ca certificate and a server certificate with a server key.
Open or create the file /etc/docker/daemon.json. This is the main configuration file for Docker.
Take the content of the 2-daemon.json file of this gist and write it to /etc/docker/daemon.json. Edit the paths to your ca and server certificate files.

Restart your Docker engine with sudo service docker restart or with whaterver UI you are working with.

The Docker Remote API is ready to use. You can run Docker commands from a remote device by using the ca.pem and the client certificate and key.

{
"hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2376"],
"tls": true,
"tlscacert": "/root/certs/ca.pem",
"tlscert": "/root/certs/server-cert.pem",
"tlskey": "/root/certs/server-key.pem",
"tlsverify": true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment