All posts

The SSL Setup That Fought Itself

Updated November 26, 2026

Two interlocking rings, one teal and one violet, pulling against each other around a glowing keyhole

The container that was supposed to hand certificates to my web server was also trying to be a web server.

That was the root of two days of trouble in early June. My setup at the time had two services. One was nginx, the reverse proxy that terminates HTTPS for everything I host. The other was a certificate manager that requested and renewed a wildcard certificate from Let's Encrypt, the free certificate authority (CA), using DNS validation. That validation setup no longer exists in the form I describe here, so read this as how it worked at the time.

I should say how I got there. Early on, when I first wanted an SSL (the encryption behind HTTPS) service, the AI agent I was working with, an older version of Claude Code, kept pushing a manual route. It didn't build infrastructure. It just requested the certificates, probably with a curl call, and installed them. That didn't fit what I needed, so I built the certificate manager as its own service.

On paper, that's a clean split: nginx serves, the manager handles certificates. In practice, the certificate manager's image had nginx installed in it and published ports 80 and 443, the same ports the real proxy needed.

What Was Actually Going Wrong

When I sat down and listed the symptoms, there were five separate problems tangled together.

Port collisions. Both containers wanted 80 and 443. Whichever started first won, and the other failed or restarted. The two services couldn't reliably run at the same time, which is a strange property for a pair of services that only make sense together.

Two different certificate paths. The certificate manager wrote certificates to one directory. nginx expected them in another. The volume mappings papered over this in some places and not others, so sometimes nginx started and found its certificates, and sometimes it failed with SSL errors about missing files.

One container doing two jobs. Because the certificate manager was also serving web traffic, a change to one function could break the other. Debugging meant first figuring out which of its two roles was failing.

A startup race. Nothing guaranteed that certificates existed before nginx started. On a clean start, nginx could come up first, find nothing, and fail. The fix was a manual restart, which means the system needed me standing next to it to boot correctly.

Monitoring nobody was using. Inside the certificate manager I found a monitoring script that could already report certificate status. It just wasn't wired to anything.

Each problem on its own looks like a bug to patch. Together, they pointed at the design: one service had more than one job.

The Decision: One Job per Service

I broke the setup into four services, each with a single purpose and a clear order:

  1. Certificate init. Runs once at startup and blocks. It checks for a valid certificate, requests one if it's missing (with a few retries and increasing waits between them), and exits. If it fails, nothing after it starts, and the logs say why.
  2. Certificate manager. Renewals only. I rebuilt its image without nginx and without any published ports.
  3. nginx. Web serving and proxying only. It mounts the certificate volume read-only and depends on the certificate services.
  4. Certificate monitor. Watches and reports. It checks certificate expiry, alerts when a certificate is within 30 days of expiring, and exposes metrics. It never changes anything.

In Docker Compose terms, the ordering looks like this (simplified, generic names):

services:
  cert-init:
    image: cert-tool:latest
    command: ["init"]
    volumes:
      - certs:/etc/nginx/ssl
    restart: "no"

  cert-manager:
    image: cert-tool:latest
    command: ["renew-loop"]
    volumes:
      - certs:/etc/nginx/ssl
    depends_on:
      cert-init:
        condition: service_completed_successfully

  proxy:
    image: nginx:stable
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - certs:/etc/nginx/ssl:ro
    depends_on:
      cert-init:
        condition: service_completed_successfully

volumes:
  certs:

service_completed_successfully is the piece that removes the race. nginx won't start until the init container has exited cleanly, which means a certificate is on disk.

For the path problem I picked /etc/nginx/ssl/ everywhere. That's where nginx already looked, so standardizing on it changed the least. Every service that writes certificates writes there; every service that reads them reads there; only the services that write get write access.

Rolling It Out in Phases

I did it in three passes, and tested each one before starting the next.

The first pass added the init service and standardized the paths. That alone fixed the startup race. The second pass rebuilt the certificate manager without its web server and removed its port mappings, which ended the port fight. I tested three cases there: a fresh start with no certificate, a start with a valid certificate already present (it should be detected and left alone), and a full stop-and-start of the stack. All three behaved.

The third pass added the monitor, and that's where the most expensive bug of the whole project showed up.

The Crash Loop I Almost Didn't Notice

The monitor's metrics endpoint used the same default port as Prometheus, and I already had an observability stack running that held that port. The monitor failed to bind, exited, and restarted. Then it did it again. By the time I looked, it had restarted more than 108,000 times.

I didn't spot it from a metric. I noticed it in Portainer, where the container wouldn't start and stay running. When I investigated, I found the restart count.

The damage showed up on the host. The load average (roughly, how many processes are competing for the CPU at once) was above 4 on a machine that should have been close to idle, and the restart churn produced a storm of disk I/O. Moving the monitor to an unused port fixed it immediately. Load dropped to 0.06.

Two lessons came out of that. Check which ports are in use before assigning one (ss -tlnp on the host takes a second). And a restart policy without a limit hides a failure instead of reporting it. Watch restart counts:

docker ps --format '{{.Names}}\t{{.Status}}'
docker inspect --format '{{.RestartCount}}' <container>

Triage: What Was Not a Problem

Once everything was green, the monitor's log had one warning left: it couldn't reach the Docker socket, so it skipped container health checks. The quick fix would have been to mount the Docker socket into the monitor. I didn't. Access to the Docker socket is effectively root on the host, and a monitoring container that only needs to read certificate dates has no business holding that. I logged the warning as cosmetic and expected, with the reasoning written next to it, and moved on. A stray date-format error in the monitor's script went on the same list as log noise to clean up later.

Writing down why a warning is acceptable matters as much as fixing the ones that aren't. Otherwise, the next person (or you, in three months) sees the warning and "fixes" it by doing the risky thing.

The Result

The stack ended up as four small services using about 200 MB of memory between them. The wildcard certificate covered every subdomain, every site loaded with a valid certificate, the startup order was guaranteed, and I had an alert 30 days before any certificate could expire. A clean start of the whole stack no longer needed me.

What I'd Tell Someone Doing This Tomorrow

If a service is hard to debug, count its jobs. Mine had two, and every symptom traced back to that. Split it so each container does one thing, give them an explicit start order, and agree on one path for shared files. Then check your ports with ss -tlnp before you assign one, and add restart counts to whatever you look at every day. Mine reached 108,000 before I saw it.

About the author

Carlos Mendez

Carlos Mendez

IT support and systems, Maui, Hawaii

I come from IT support and systems work. I build and run my own infrastructure and the production websites for West Maui Tech, my business, and I build AI tooling on top of that stack. I write here about what I run, what broke, and how I fixed it.

Enjoyed this article?

Subscribe to get notified about new posts on software engineering, AI development, and infrastructure.

No spam, unsubscribe anytime.