Skip to content

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:

version: "3.9"
services:
  app:
    image: nginx:alpine
    ports: ["8080:80"]
    networks:
      - caddy-net

  caddy:
    image: caddy:latest
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
    ports:
      - "80:80"
      - "443:443"
    networks:
      - caddy-net

networks:
  caddy-net:
    driver: bridge

Bring the services up:

docker compose up -d
Tip

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:

docker compose exec caddy getent hosts app

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:

:80 {
    reverse_proxy app:8080
}

If TLS is needed inside the network (self-signed), you can add:

app.local:80 {
    reverse_proxy app:8080
    tls internal
}
Warning

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:

DirectiveDescriptionExample
reverse_proxyforwards request to upstream, resolves container namesreverse_proxy app:8080
headeradds/replaces response headerheader Upstream-Name {upstream_name}
encodemanages request/response body encodingencode gzip
pathlimits upstream pathpath /api/* reverse_proxy backend:8080

Verification of Functionality

After startup, check service accessibility through Caddy’s port:

curl -s http://localhost | head -n 5

If configured correctly, the response will contain headers from the upstream application.

Validate Caddy config without reload:

docker compose exec caddy caddy validate --config /etc/caddy/Caddyfile

Reload Caddy after config change:

docker compose restart caddy

For diagnostics, view Caddy logs:

docker compose logs -f caddy
Note

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:

SymptomCauseRemedy
502 Bad GatewayCaddy cannot resolve appVerify both services are on the same network caddy-net
502 Bad GatewayNetwork declared as internalRemove internal: from the network definition or use a custom bridge network without that flag
502 Bad GatewayUpstream port busy or not listeningCheck app logs: docker compose logs app
502 Bad GatewayInvalid Caddyfile syntaxRun caddy validate, fix indentation and spaces

If access still fails after network checks, restart with a clean Caddy data cache:

docker compose down
docker volume rm caddy_data
docker compose up -d

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.