first, let's create some directories to work in
mkdir -p ./docker_certs/ca ./docker_certs/server ./docker_certs/client
cd ./docker_certs
# Generate CA private key
openssl genpkey -algorithm RSA -out ca/local-root-ca.key -aes256
# Generate CA certificate
openssl req -new -x509 -days 3650 -key ca/local-root-ca.key -sha256 -out ca/local-root-ca.pem
sudo cp ca/local-root-ca.pem /etc/pki/ca-trust/source/anchors/local-root-ca.pem
sudo update-ca-trust
openssl genpkey -algorithm RSA -out server/docker-daemon.key
openssl req -new -key server/docker-daemon.key -out server/docker-daemon.csr
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
IP.1 = 127.0.0.1
IP.2 = 192.168.122.1
IP.3 = 192.168.85.30
openssl x509 -req -days 3650 \
-in server/docker-daemon.csr \
-CA ca/local-root-ca.pem \
-CAkey ca/local-root-ca.key \
-CAcreateserial \
-out server/docker-daemon.pem \
-extfile server/server-ext.cnf
openssl x509 -noout -text -in server/docker-daemon.pem
./ca/local-root-ca.key
: CA private key./ca/local-root-ca.pem
: CA certificate./server/docker-daemon.key
: Server private key./server/docker-daemon.pem
: Server certificate
sudo mkdir -p /etc/docker/certs.d
sudo cp ./server/docker-daemon.pem /etc/docker/certs.d/
sudo cp ./server/docker-daemon.key /etc/docker/certs.d/
sudo cp ./ca/local-root-ca.pem /etc/docker/certs.d/ca.pem
{
"tls": true,
"tlscert": "/etc/docker/certs.d/docker-daemon.pem",
"tlskey": "/etc/docker/certs.d/docker-daemon.key",
"tlscacert": "/etc/docker/certs.d/ca.pem"
}
sudo systemctl restart docker
Now the Docker daemon should be using the generated certificates for secure communication.
openssl genpkey -algorithm RSA -out client/docker-client.key
openssl req -new -key client/docker-client.key -out client/client.csr
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = clientAuth
openssl x509 -req -days 3650 \
-in client/client.csr \
-CA ca/local-root-ca.pem \
-CAkey ca/local-root-ca.key \
-CAcreateserial \
-out client/docker-client.pem \
-extfile client/client-ext.cnf
docker-client.key
: Client private keydocker-client.pem
: Client certificatelocal-root-ca.pem
: CA certificate (same as for the server)
Use the following curl command, providing the CA certificate, client certificate, and client key:
curl \
--cacert ca/local-root-ca.pem \
--cert client/docker-client.pem \
--key client/docker-client.key \
https://192.168.122.1:2375
Store your client certificates and keys in a dedicated directory:
mkdir -p ~/.docker/certs
cp client/docker-client.pem ~/.docker/certs/
cp client/docker-client.key ~/.docker/certs/
cp ca/local-root-ca.pem ~/.docker/certs/
Use the docker context command to create a new context with the specified certificates:
docker context create docker-tls \
--description "Context for secure Docker daemon at 192.168.122.1" \
--docker "host=tcp://192.168.122.1:2375,ca=~/.docker/certs/local-root-ca.pem,cert=~/.docker/certs/docker-client.pem,key=~/.docker/certs/docker-client.key"
List all available Docker contexts to verify the new context has been created:
docker context ls
You should see an output similar to:
NAME DESCRIPTION DOCKER ENDPOINT KUBERNETES ENDPOINT ORCHESTRATOR
default * Current DOCKER_HOST based configuration unix:///var/run/docker.sock swarm
docker-tls Context for secure Docker daemon at 192.168.122.1 tcp://192.168.122.1:2375 swarm
docker context use docker-tls
docker info
F*cking great! Many thanks!