Article Image
read

It's a fairly common need to extract a certificate and key from traefik to use it for other reasons. In my case, I needed to grab a certificate for an existing domain and use it for my mail-server.

There's a bunch of very complex examples to dump all certificates, and semi-official traefik-cert-dumper.

My method only uses jq (jq) in a one-liner.

The Snippet

jq -r '.le.Certificates[] | select(.domain.main==\"'example.com'\") | .certificate' /data/acme.json | base64 -d > /out/tls_cert.pem

This should be fairly self-explanatory, but it extracts the certificate of example.com and decodes the base64.

To extract the key, simply change the last jq expression .certificate to .key

As a scheduled docker service

version: "3.5"
services:
  certsync:
    image: stedolan/jq
    # Dumps both certificate and key for "example.com"
    entrypoint: |
      /bin/bash -c "
        jq -r '.le.Certificates[] | select(.domain.main==\"'example.com'\") | .certificate' /data/acme.json | base64 -d > /out/tls_cert.pem;
        jq -r '.le.Certificates[] | select(.domain.main==\"'example.com'\") | .key' /data/acme.json | base64 -d > /out/tls_key.pem;
      "
    volumes:
      - common_letsencrypt:/data:ro # Mount traefik volume as read-only
      - out:/out # And whichever volume you want to output on
    deploy:
      mode: global
      placement:
        constraints: [node.role==manager] # Only run on the manager node (Where traefik and its volume exists)
      restart_policy:
        delay: 24h # Re-run every 24 hours
      resources:
        limits: { cpus: '0.1', memory: '32M' }
        reservations: { cpus: '0.025', memory: '16M' }
Blog Logo

Christopher LaPointe


Published

Interested in Related Posts from this Site?

Virtual PDF Network Printer

May 04, 2023: For my [paperless-ngx](https://github.com/paperless-ngx/paperless-ngx) install, I wanted to be able to easily "print" to it from...

Set up your own mailserver with Maddy

February 10, 2022: About a year ago I wanted to expand beyond namecheap's simple "email forwarding" service and...

Docker Samba / CIFS Volume (Photoview)

July 12, 2021: Recently I had a need to mount a CIFS volume as a docker volume: The...

Self-Hosted Analytics: Docker & Privacy-First

July 02, 2021: I have a lot of little sites. Probably too many. Includes various personal sites, sites...

Docker Swarm Registry and Auto Garbage Collection on NFS

March 15, 2021: After moving Traefik to v2, I also updated the common registry infrastructure. Namely, this stack...

Image

Chris LaPointe

Another site of Code

Back to Overview