# Caddy reverse_proxy for a single container

LLMS index: [llms.txt](/en/llms.txt)

---

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`:

```yaml
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:

```bash
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:

```bash
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`:

| 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:

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

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

Validate Caddy config without reload:

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

Reload Caddy after config change:

```bash
docker compose restart caddy
```

For diagnostics, view Caddy logs:

```bash
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:

| 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:

```bash
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.
