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>/TLS
cd ~<username>/TLS
openssl genrsa -aes256 -out ca-key.pem 2048
openssl req -new -x509 -days 900 -key ca-key.pem -sha256 -out ca.pem
openssl genrsa -out server-key.pem 2048
openssl req -subj "/CN=$(hostname)" -new -key server-key.pem -out server.csr
echo "subjectAltName = IP:127.0.0.1,IP:$(hostname -i)" > extfile.cnf
openssl x509 -req -days 900 -in server.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out server-cert.pem -extfile extfile.cnf
openssl genrsa -out client-key.pem 2048
openssl req -subj '/CN=client' -new -key client-key.pem -out client.csr
echo "extendedKeyUsage = clientAuth" > extfile2.cnf
openssl x509 -req -days 900 -in client.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out client-cert.pem -extfile extfile2.cnf
openssl 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 docker
cd ~<username>/TLS
dockerd --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 ~/.docker
cd ~/TLS
mv ca.pem ~/.docker
mv client-cert.pem ~/.docker/cert.pem
mv client-key.pem ~/.docker/key.pem
docker -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.service
cd ~<username>/TLS
mkdir /etc/docker
cp *.pem /etc/docker/.
cd /etc/docker
chmod 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-reload
systemctl 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.