Here are some simple instructions for configuring a local docker engine to use TLS encryption and self-signed certificates. Next steps will be to create a Linux based CA, free IPA, and integrate it all with Docker
Step 1: Generate the certificates that you need. Replace <username> with your local, NON-ROOT, user that you have granted access to the docker group.
mkdir -m 700 ~<username>/TLScd ~<username>/TLSopenssl genrsa -aes256 -out ca-key.pem 2048openssl req -new -x509 -days 900 -key ca-key.pem -sha256 -out ca.pemopenssl genrsa -out server-key.pem 2048openssl req -subj "/CN=$(hostname)" -new -key server-key.pem -out server.csrecho "subjectAltName = IP:127.0.0.1,IP:$(hostname -i)" > extfile.cnfopenssl x509 -req -days 900 -in server.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out server-cert.pem -extfile extfile.cnfopenssl genrsa -out client-key.pem 2048openssl req -subj '/CN=client' -new -key client-key.pem -out client.csrecho "extendedKeyUsage = clientAuth" > extfile2.cnfopenssl x509 -req -days 900 -in client.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out client-cert.pem -extfile extfile2.cnfopenssl x509 -text -in server-cert.pem
Step 2: start the docker service, and verify that it runs okay with these certificates.
sudo su -systemctl stop dockercd ~<username>/TLSdockerd --tlsverify --tlscacert=ca.pem --tlscert=server-cert.pem --tlskey=server-key.pem
Step 3: Open another terminal window. You will need it for the testing. Connect to the same server using the user account
mkdir ~/.dockercd ~/TLSmv ca.pem ~/.dockermv client-cert.pem ~/.docker/cert.pemmv client-key.pem ~/.docker/key.pemdocker -H tcp://127.0.0.1:2376 --tlsverify info (should now give you the expected result)
Step 4: Now, go back to the previous terminal window, and ctrl-c to terminate docker. Now you will have to modify the daemon service file to tell docker to start using these certificates.
systemctl stop docker.servicecd ~<username>/TLSmkdir /etc/dockercp *.pem /etc/docker/.cd /etc/dockerchmod 400 *cd /usr/lib/systemd/system- vim docker.service. Change the ExecStartline to this:
- ExecStart=/usr/bin/dockerd -H=0.0.0.0:2376 –tlsverify –tlscacert=/etc/docker/ca.pem –tlscert=/etc/docker/server-cert.pem –tlskey=/etc/docker/server-key.pem –containerd=/run/containerd/containerd.sock
systemctl daemon-reloadsystemctl start docker.service- You have just setup the docker service to start up in TLS only mode. Verify that it works by going to the <username> user and typing docker info. You should not get any result. Don’t worry, this is expected. We will solve this in the next step.
Now, you have to add the environment variables for all users to point to the right TCP port, and use TLS.
- As root,
cd /etc/profile.d. && vim docker.sh - add the following lines to the file and write/quit.
- DOCKER_HOST=127.0.0.1:2376
- DOCKER_TLS_VERIFY=1
- reboot the system. upon first login with non-root user, you should be able to run docker info from the bash prompt, and get a positive result.