Using Docker Swarm Secrets to Store and Rotate your SSL Certificates with Nginx
A tutorial on using Docker Swarm secrets to store your sensitive data by creating an Nginx docker service, and how to update it.
Join the DZone community and get the full member experience.Join For Free
What is Docker Swarm Secrets?
Docker Swarm has an excellent feature out of the box — Docker Swarm secrets. Using it, you can easily keep your sensitive data like credentials, TLS certificates, etc.
In terms of Docker Swarm services, a secret is a blob of data, such as a password, SSH private key, SSL certificate, or another piece of data that should not be transmitted over a network or stored unencrypted in a Dockerfile or in your application’s source code. You can use Docker secrets to centrally manage this data and securely transmit it to only those containers that need access to it.
So, if we want to use it to store our certificates, first we need a certificate. Here we have two options:
- Use a self-signed certificate.
- Buy SSL certificate.
We will use self-signed:
$ mkdir certs && sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ./certs/nginx.key -out ./certs/nginx.crt
The command above generates a certificate that expires in 1 year and places it in ./certs/ directory.
Now we have key and crt files, and we can already use them. But besides that, we should always monitor the certificate expiration date. Sure, there are a few ways to do it, but it is out of scope for this topic. Just keep in mind that you can use alerts (Prometheus + Blackbox exporter) of the certificate expiration date to trigger your script, which in its turn updates the secret with the certificate.
Next step, we need to create an Nginx docker service with our certificate. Here is a docker-compose file with a secrets section:
You should keep in mind that you cannot update docker secrets on the fly. It means that you should create a dummy secret every time and replace the old secret with a dummy secret. This is an example script of how to update an existing secret:
After script execution, we have updated the certificate inside the Nginx container.
Opinions expressed by DZone contributors are their own.