Caddy reverse_proxy for a single container
When exposing a single internal service through Caddy in Docker Compose, the reverse_proxy directive is the entry point. This note covers the minimal compose config, Caddyfile syntax, verification steps, and the common network trap causing 502 errors.
Preparation of compose file and network
For reverse_proxy to resolve the upstream container name, Caddy must be on the same user-defined Docker network. Below is the minimal docker-compose.yml:
Bring the services up:
If internal is set on the network definition, Caddy won’t reach the upstream by container name. Keep the network as a plain driver: bridge for internal name resolution.
Verify DNS resolution from inside Caddy:
You should see the app container IP on the same subnet.
Basic Caddyfile Configuration
Place the Caddyfile next to compose.yml. For a simple reverse_proxy to a single container, the content is:
If TLS is needed inside the network (self-signed), you can add:
The tls internal directive works only if Caddy is started with the caddy-dns plugin or manually with certificates. For fully internal traffic it’s often sufficient to disable TLS or use file certificates.
Table of common Caddyfile directives for reverse_proxy:
| Directive | Description | Example |
|---|---|---|
reverse_proxy | forwards request to upstream, resolves container names | reverse_proxy app:8080 |
header | adds/replaces response header | header Upstream-Name {upstream_name} |
encode | manages request/response body encoding | encode gzip |
path | limits upstream path | path /api/* reverse_proxy backend:8080 |
Verification of Functionality
After startup, check service accessibility through Caddy’s port:
If configured correctly, the response will contain headers from the upstream application.
Validate Caddy config without reload:
Reload Caddy after config change:
For diagnostics, view Caddy logs:
Caddy defaults to admin port 2019 for its API. You can run curl http://localhost:2019/config to inspect the currently loaded configuration.
Solving Typical Access Breakage
The most frequent cause of failure is upstream name resolution failure. Symptoms and remedies:
| Symptom | Cause | Remedy |
|---|---|---|
502 Bad Gateway | Caddy cannot resolve app | Verify both services are on the same network caddy-net |
502 Bad Gateway | Network declared as internal | Remove internal: from the network definition or use a custom bridge network without that flag |
502 Bad Gateway | Upstream port busy or not listening | Check app logs: docker compose logs app |
502 Bad Gateway | Invalid Caddyfile syntax | Run caddy validate, fix indentation and spaces |
If access still fails after network checks, restart with a clean Caddy data cache:
This resets auto-generated certificates and session state, often resolving issues after network changes.
At this point the practical scenario ends: you have a working reverse_proxy through Caddy in Docker Compose for a single container, know the verification commands, and understand the typical network trap that can be fixed in a minute.