All posts

Hosting an API with Podman Quadlet, Caddy and Cloudflare Origin CA

Four undocumented behaviors that cost me hours — silent permission failures, TLS challenges behind a CDN, and the exact setup that finally worked.

Podman Quadlet and rootless deploys don't fall into place on the first try. The ecosystem assumes you already know systemd, SELinux, and user namespaces; and when something breaks on a volume mount or a certificate, the error messages rarely point to the actual cause. Here's what I ran into setting up a database and a reverse proxy for an API on the same VM.

Don't trust Let's Encrypt behind a strict CDN

If your traffic already goes through a global proxy like Cloudflare with Full Strict encryption enabled, letting Caddy issue Let's Encrypt certificates will cause problems. Caddy tries to resolve the HTTP-01 challenge, which conflicts with the CDN's edge rules.

The cleaner path is Cloudflare Origin CA.

caddyfile
api.yourdomain.com {
    tls /etc/caddy/certs/cloudflare-origin.pem /etc/caddy/certs/cloudflare-origin-key.pem
 
    handle_path /core/* {
        reverse_proxy host.containers.internal:8080
    }
 
    handle {
        respond 404
    }
}

The certificate is static, generated by the CDN itself with up to 15 years validity, and guarantees end-to-end encryption without the fragility of automatic renewals blocked by the proxy. handle_path strips the route prefix before forwarding to the API, and the explicit respond 404 prevents Caddy from masking nonexistent routes with an empty 200 OK.

Web ports require a kernel adjustment on the host

Running Caddy via rootless Podman will fail immediately when it tries to bind to ports 80 and 443. Unprivileged containers can't touch ports below 1024.

Instead of working around it with iptables rules or high-port redirects, set the host's sysctl to allow rootless access starting from port 80:

bash
# /etc/sysctl.conf or /etc/sysctl.d/
net.ipv4.ip_unprivileged_port_start=80

Systemd dependencies are not all equal

When you hand container lifecycle to systemd via Quadlet, startup order matters. But Requires and After need to be used carefully.

For the API, the database is a hard dependency. If Postgres doesn't come up, the API will crash-loop.

ini
# core-api.kube
[Unit]
After=network-online.target postgres.service
Requires=postgres.service

Don't do the same for the reverse proxy. Caddy should start even if the application is down:

ini
# caddy.container
[Unit]
After=network-online.target core-api.service
# No Requires= here

This keeps the domain alive when the backend dies. Caddy returns a 502 Bad Gateway, which isolates the failure and makes it observable.

hostPath and SELinux don't mix — use Secrets instead

The first instinct when a container needs a credential file is to mount it directly from the host using hostPath. On a system with SELinux in Enforcing mode, that instinct will get you a silent Permission Denied.

Files mapped via hostPath carry the host filesystem context — something like user_home_t or config_home_t. The container process runs as container_t, and the SELinux policy blocks that process from reading those contexts by default. You can fix it with semanage and restorecon, but then you're relabeling files manually every time the credential rotates.

The clean fix is to never use hostPath for credentials. Declare a Kubernetes Secret in your deployment manifest instead:

yaml
apiVersion: v1
kind: Secret
metadata:
  name: firebase-credentials
stringData:
  service-account.json: |
    { "type": "service_account", ... }

Then mount it in the pod spec:

yaml
volumes:
  - name: firebase-credentials
    secret:
      secretName: firebase-credentials
containers:
  - volumeMounts:
      - name: firebase-credentials
        mountPath: /secrets
        readOnly: true

When podman kube play processes a Secret volume, it manages the mount through its own internal storage subsystem, often backed by tmpfs. Because the mount originates from inside the container engine rather than from an arbitrary path on the host filesystem, the SELinux policy already allows container_t to read it. The host file context is irrelevant.

This also means the credential never sits as a static file on the VM disk. It lives in memory for the lifetime of the pod.

Note that this is specific to .kube Quadlet files backed by podman kube play. Pure .container Quadlet units do support the ,Z suffix on volume mounts to trigger automatic relabeling, which is how caddy.container handles its cert volume. The Kubernetes manifest spec has no equivalent syntax for hostPath, which is exactly why switching to Secret is the right move there.