How to Renew a Certbot Certificate in Docker Without Losing It on Redeploy
Persist Certbot state in Docker, test renewal from the scheduled environment, and reload the server that actually terminates TLS.
- https
- certbot
- docker
- guide
For Certbot in Docker, keep the certificate lineage, renewal configuration and account state outside the container. Then run renewal with the same challenge setup used to obtain the certificate. The proxy that serves HTTPS must be able to load the new files after Certbot finishes.
Persist the config tree, including archive targets
Certbot's default configuration directory is /etc/letsencrypt. Put the full directory on a persistent volume or an approved bind mount. In a standard Certbot setup, the current certificate paths in live refer to numbered files in archive. Mounting only the live directory into another container can leave those links pointing to files the container cannot see.
| State | Who needs access |
|---|---|
| Certbot configuration, account data and renewal files | The Certbot task. Keep the full tree private and back it up securely. |
| Active certificate, chain and matching private key | The process that terminates TLS, with only the access it needs. |
| Challenge state | The authenticator that will renew. Retain the same usable webroot, port arrangement or scoped credential. |
One design is to mount the full Certbot tree only in the renewal task, then copy the required certificate files to a protected serving directory through a host-owned deploy step. Another is a suitably restricted shared read-only mount for a single proxy that already has permission to read the private key. Test permissions from the actual proxy process. Back up the key material only under your organization's encrypted backup and access rules.
Test the real scheduled job
Start with an existing Certbot-managed test certificate and an image known to contain the required plugin. For example, after setting CERTBOT_IMAGE to that approved image reference, run docker run --rm --mount type=volume,src=certbot-config,dst=/etc/letsencrypt "$CERTBOT_IMAGE" renew --dry-run with the same additional mounts, plugin and networking that the production schedule will use. For a webroot setup, the authenticator writes to a path the web server exposes for the challenge. For a DNS plugin, the container needs controlled access to the credential file configured for that lineage. A missing mount can let a manual run work while the scheduled task fails.
Certbot's renewal documentation describes when deploy hooks run. A dry run tests issuance against the test service. The deploy hook usually needs a separate test. Certbot's --run-deploy-hooks dry-run option runs applicable hooks with the current active certificate after successful testing, rather than installing the staging certificate.
Verify the served certificate after a real renewal
Schedule Certbot outside the transient container, such as through your host's normal job runner. Give your serving process a narrow, host-owned reload path. Mounting the Docker daemon socket solely to reload the proxy gives the renewal container much wider control than it needs.
After a successful renewal, compare the new certificate's validity or fingerprint with what clients receive from the public endpoint. If the files changed but the live endpoint did not, check the serving path and reload first. If the files in live are absent after redeployment, recover the existing persistent volume and investigate mount configuration before requesting more certificates.
