DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Smart Deployment Strategies for Modern Applications
  • Solving the Mystery: Why Java RSS Grows in Docker on M1 Macs
  • How We Diagnosed a Hidden Scheduler Failure in a Docker Swarm Cluster Serving 2 Million Users
  • Java Backend Development in the Era of Kubernetes and Docker

Trending

  • How Reactive Scaling Drains Your Cloud Budget Without Warning
  • Top JavaScript/TypeScript Gen AI Frameworks for 2026
  • A Deep Dive into Tracing Agentic Workflows (Part 1)
  • Real-Time AI Inference at Scale Using Cloud Run, GPUs, and Vertex AI
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Using Docker Swarm Secrets to Store and Rotate your SSL Certificates with Nginx

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.

By 
Pavel Varfalameev user avatar
Pavel Varfalameev
·
Dec. 07, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
8.5K Views

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:

YAML
 




xxxxxxxxxx
1
36


 
1
version: '3.4'
2

          
3
services:
4
  ingress_nginx:
5
    image: ingress_nginx
6
    build:
7
      context: .
8
    ports:
9
      - "80:80"
10
      - "443:443"
11
    networks:
12
      - network
13
    deploy:
14
      mode: global
15
      restart_policy:
16
        condition: any
17
        delay: 5s
18
      update_config:
19
        delay: 30s
20
        parallelism: 1
21
        failure_action: rollback
22
    secrets:
23
      - source: nginx_key
24
        target: /etc/nginx/nginx.key
25
      - source: nginx_cert
26
        target: /etc/nginx/nginx.crt
27

          
28
secrets:
29
  nginx_key:
30
    file: ./certs/nginx.key
31
  nginx_cert:
32
    file: ./certs/nginx.crt
33

          
34
networks:
35
  network:
36
    driver: overlay



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:

Shell
 




xxxxxxxxxx
1
26


 
1
# Create dummy secret
2
echo "<<< Creating dummy secret >>>" 
3
docker secret create dummy_key nginx.key
4
docker secret create dummy_crt nginx.crt
5

          
6
# Delete old certificate and key from docker secret and replace them with dummy
7
echo "<<< Delete old certificate and key from service and replace them with dummy >>>" 
8
docker service update \
9
    --secret-rm ${stack}_nginx_key \
10
    --secret-rm ${stack}_nginx_cert \
11
    --secret-add source=dummy_key,target=/etc/nginx/nginx.key \
12
    --secret-add source=dummy_crt,target=/etc/nginx/nginx.crt \
13
    ${stack}_ingress_nginx
14
    
15
echo "<<< Delete old certificate from secrets >>>"
16
docker secret rm  ${stack}_nginx_key
17
docker secret rm  ${stack}_nginx_cert
18

          
19
# Deploy service with new secrets
20
echo "<<< Create secret with new certificate and update service >>>"
21
docker stack deploy --compose-file docker-compose.yml $stack
22

          
23
# Delete dummy secrets
24
echo "<<< Delete dummy certificate >>>"
25
docker secret rm dummy_key
26
docker secret rm dummy_crt


After script execution, we have updated the certificate inside the Nginx container.

Docker (software)

Opinions expressed by DZone contributors are their own.

Related

  • Smart Deployment Strategies for Modern Applications
  • Solving the Mystery: Why Java RSS Grows in Docker on M1 Macs
  • How We Diagnosed a Hidden Scheduler Failure in a Docker Swarm Cluster Serving 2 Million Users
  • Java Backend Development in the Era of Kubernetes and Docker

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook