Caddy with Docker Compose: networks, ports and 502 fixes
Run Caddy as a Docker Compose reverse proxy across two projects. Understand external networks, aliases and ports, then reproduce and fix a 502.

TL;DR: If Caddy and your application run on the same Docker Engine, they can share a user-defined network. Point Caddy at the application’s network alias and the port it actually listens on. Only the proxy needs a published port. Below is a runnable example with two separate Compose projects, plus a broken upstream you can reproduce and fix.
While getting this site onto my VPS, I stopped at something that looked like a missing piece in the Compose file. The site listens on port 8080. But there was no ports section. How was Caddy supposed to get to it?
Then there was the network alias. Should it be aakash-site or aakash-site:8080? And if I published 127.0.0.1:8080:8080, could Caddy just use localhost?
I mean, these are small questions until you have a running container, a healthy process and a browser showing a 502. Then knowing which machine a name or address belongs to becomes rather useful. 😄
This guide follows the setup behind this site: a static Astro build served by Nginx, with an existing Caddy container handling the incoming request. You can run the same network arrangement locally with a tiny HTML page.
What Caddy actually connects to
My site’s Nginx process listens on 8080 inside its container. The site container joins an existing Docker network through this part of its Compose file:
services:
site:
# Build and runtime settings omitted here.
networks:
caddy:
aliases:
- aakash-site
networks:
caddy:
external: true
name: ${CADDY_NETWORK:?Set CADDY_NETWORK in .env to your existing Docker network}
Caddy joins that same network. Its upstream is aakash-site:8080. The request route is:
Incoming request
|
v
Caddy container
|
| shared Docker network
v
aakash-site:8080
|
v
Nginx serves the built site
Both containers are on the same VPS and Docker Engine. A bridge network belongs to one Docker host. Creating another bridge with the same name on a second VPS doesn’t connect those servers. Docker describes that boundary in its bridge networking documentation.
For the connection between Tenwrite’s application and observability VPSs, I use a different path. The WireGuard two-VPS guide covers that tunnel and a runnable OpenTelemetry example.
The actual Caddy configuration lives with our infrastructure and includes certificate, header and routing settings for several services. The local example below keeps just the route we need to understand. We don’t need the whole observability stack to serve one page.
Why the site doesn’t need published ports
These three things have different jobs:
| Setting | What it does |
|---|---|
Nginx listen 8080 |
Makes the process accept connections on port 8080 inside its container. |
Compose ports |
Maps an address and port on the Docker host to a container port. |
Compose expose, or Dockerfile EXPOSE |
Describes intended container ports. It doesn’t start a listener or publish a host port. |
Caddy can reach a listening process on the shared network without a host-port mapping. Adding expose: [8080] isn’t required to make that connection work either. Docker’s port publishing guide explains the host-versus-container distinction.
If I add this to the site service:
ports:
- "127.0.0.1:8080:8080"
I create a route from the host’s loopback port 8080 to the site’s container port 8080. That is useful when a proxy runs directly on the host, or when I want a host-only preview. Our containerized Caddy already has a route through the shared network, so the extra mapping doesn’t help it.
There is another trap here. Inside an ordinary bridge-networked Caddy container, 127.0.0.1 means the Caddy container itself. It doesn’t mean the VPS host, and it doesn’t mean the Nginx container. Publishing Nginx on the host’s loopback doesn’t change that.
For this setup, Caddy’s destination is aakash-site:8080.
Run it locally with two Compose projects
Download the runnable Caddy and Docker Compose example, extract it and open a terminal inside caddy-docker-network.
You need Docker Engine, the Docker Compose plugin and curl. I tested the example on Linux with Docker Engine 29.6.2, Compose 5.3.1, Caddy 2.11.7 and Nginx 1.31.6. The Compose files pin the two image versions. The first run needs network access to pull them.
The download contains:
caddy-docker-network/
.env.example
compose.caddy.yaml
compose.site.yaml
Caddyfile
nginx.conf
index.html
verify.mjs
README.md
Copy .env.example to .env:
cp .env.example .env
Its values are:
CADDY_NETWORK=caddy-guide-edge
CADDY_HTTP_PORT=18080
SITE_UPSTREAM=aakash-site:8080
The network name is deliberately specific to this example. On my VPS, the site uses the existing Caddy network instead.
Here is compose.site.yaml from the download:
name: caddy-guide-site
services:
site:
image: nginx:1.31.6-alpine3.24
entrypoint: [nginx, -g, "daemon off;"]
user: nginx
restart: unless-stopped
read_only: true
cap_drop: [ALL]
security_opt: [no-new-privileges:true]
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./index.html:/usr/share/nginx/html/index.html:ro
tmpfs:
- /tmp:rw,noexec,nosuid,size=16m
networks:
edge:
aliases:
- aakash-site
networks:
edge:
external: true
name: ${CADDY_NETWORK:?Set CADDY_NETWORK in .env}
The included Nginx configuration listens on 8080 and serves index.html. Its writable temporary files go under /tmp, so the container can run as the Nginx user with a read-only root filesystem. These are the same runtime choices I use for the actual site.
There is no ports section on this service.
Now compose.caddy.yaml:
name: caddy-guide-proxy
services:
caddy:
image: caddy:2.11.7-alpine
restart: unless-stopped
read_only: true
cap_drop: [ALL]
# The official executable carries this file capability, even for port 8080.
cap_add: [NET_BIND_SERVICE]
security_opt: [no-new-privileges:true]
environment:
SITE_UPSTREAM: ${SITE_UPSTREAM:-aakash-site:8080}
ports:
- "127.0.0.1:${CADDY_HTTP_PORT:-18080}:8080"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile:ro
- caddy-data:/data
- caddy-config:/config
tmpfs:
- /tmp:rw,noexec,nosuid,size=16m
networks:
- edge
networks:
edge:
external: true
name: ${CADDY_NETWORK:?Set CADDY_NETWORK in .env}
volumes:
caddy-data:
caddy-config:
Caddy listens on 8080 in this example too. That is fine: each container has its own network namespace. The host maps 127.0.0.1:18080 to Caddy’s 8080. Caddy then connects to the site’s 8080. No ports are competing with each other.
The capability entry is there because the official Caddy executable carries NET_BIND_SERVICE as a file capability. Dropping it from the container’s capability set prevents that executable from starting, even though this example uses a high port.
The Caddyfile is three lines:
http://:8080 {
reverse_proxy {$SITE_UPSTREAM}
}
The explicit http:// lets us try the route locally without setting up a domain or trusting a development certificate.
Create the shared network once, then start the two projects:
docker network create caddy-guide-edge
docker compose -f compose.site.yaml up -d
docker compose -f compose.caddy.yaml up -d
curl --fail http://127.0.0.1:18080/
The page should contain:
<h1>Caddy found the site.</h1>
If 18080 is already occupied, change CADDY_HTTP_PORT in .env, run the Caddy up -d command again, and use the new port in your URL. If you choose a different CADDY_NETWORK, create that exact network name instead.
External network, alias and environment: which name goes where?
The two name: fields at the top of the Compose files give us two independent projects. Each can be started, replaced or stopped separately. Compose would normally give them separate default networks. We explicitly attach both services to the existing caddy-guide-edge network instead.
external: true means the network’s lifecycle is managed outside this Compose project. It doesn’t mean internet-facing. Compose expects the network to exist and won’t create or remove it for us. Docker’s Compose network reference documents that behaviour. Also, internal: true is a different setting; external alone says nothing about whether containers can make outbound requests.
The alias is just a hostname available to peers on that network:
aliases:
- aakash-site
The port goes in the upstream address:
aakash-site:8080
An alias doesn’t include the port. It also doesn’t create a public DNS record. My browser doesn’t resolve aakash-site; Caddy resolves it from inside Docker.
The service name site is available on the network too. I use the more specific alias because several independently deployed projects may have a service called site, web or api. Give each upstream a distinct name. Network aliases are scoped to networks, and sharing an alias between containers makes the chosen destination ambiguous.
There are two environment substitutions in this example:
.env -> Compose ${SITE_UPSTREAM:-aakash-site:8080}
-> container environment SITE_UPSTREAM
-> Caddyfile {$SITE_UPSTREAM}
Compose reads .env when resolving its YAML. It doesn’t automatically inject every .env entry into the container. The explicit environment entry passes the upstream value to Caddy. Caddy then expands its own {$SITE_UPSTREAM} syntax when loading the Caddyfile. These are separate steps, described in Compose interpolation and Caddyfile environment variables.
For the same reason, changing a Compose-provided environment value needs a container update with docker compose ... up -d. A Caddy config reload alone doesn’t replace the running container’s environment.
Check the connection from inside Caddy
The browser request passing is good. I also want to see whether Caddy can reach the upstream directly:
docker compose -f compose.caddy.yaml exec -T caddy \
wget -qO- http://aakash-site:8080/
That command runs inside the proxy container, resolves the alias and requests the site’s actual listening port. It should return the same HTML. Running curl http://aakash-site:8080 on your laptop tests a different DNS environment and isn’t the equivalent check.
To see who is attached to the network:
docker network inspect caddy-guide-edge
In the Containers section, you should see both this example’s Caddy and site containers. Then inspect each project’s status:
docker compose -f compose.caddy.yaml ps
docker compose -f compose.site.yaml ps
Caddy should show the loopback mapping to port 18080. The site should have no host-port mapping. You may still see 80/tcp next to Nginx because its base image declares that port. Image metadata doesn’t override the supplied Nginx config, which listens on 8080.
Try replacing the site while leaving Caddy running:
docker compose -f compose.site.yaml up -d --force-recreate
curl --fail http://127.0.0.1:18080/
Once Nginx has started again, the route should work. The replacement container gets the same alias; we didn’t need to look up or hardcode its IP address. This is still a single-instance deployment, so replacing that instance can briefly interrupt requests.
Make a 502 happen, then fix it
Well, let’s break something small and predictable. 😄 Point Caddy at port 8081, where this Nginx isn’t listening:
SITE_UPSTREAM=aakash-site:8081 docker compose -f compose.caddy.yaml up -d --force-recreate
curl -i http://127.0.0.1:18080/
docker compose -f compose.caddy.yaml logs --tail=10 caddy
Wait a moment for the recreated Caddy to start. The request should now return 502 Bad Gateway, and the proxy log should report a failed connection to port 8081. Caddy is reachable, but its upstream connection fails.
The variable at the beginning of that command applies only to that invocation. It doesn’t edit .env. Restore the original upstream from the file:
docker compose -f compose.caddy.yaml up -d --force-recreate
curl --fail http://127.0.0.1:18080/
With SITE_UPSTREAM=aakash-site:8080 still in .env, the page returns again. The Caddy reverse proxy reference covers upstream addresses and transports.
If you have Node.js 22.12 or newer, the download also includes an automated check:
node verify.mjs
It creates separately named temporary projects and a network, asks Docker for a free loopback port, and checks the actual requests. It verifies the working proxy, direct alias access, absence of published site ports, site replacement, wrong-port and localhost failures, and the repaired route. Then it removes its own containers, volumes and network. It doesn’t need to stop your manually running example.
If your route still doesn’t work
Follow the request in order. Otherwise it is easy to change three unrelated settings and lose track of what fixed it.
| Symptom | Where I would look first |
|---|---|
| Compose says the external network doesn’t exist | Compare CADDY_NETWORK with docker network ls. Create the chosen network before starting either project. |
| Host request gets connection refused | Check whether Caddy is running and whether you’re using its published host port. |
| Caddy logs a DNS lookup failure | Check alias spelling and whether both containers are attached to the same network. |
| Caddy resolves the name but gets connection refused | Check the upstream port and the application’s listening address. |
Upstream is localhost or 127.0.0.1 |
In this bridge setup, that points back to the proxy container. Use the application alias. |
| Direct upstream request works, public request gives 404 | Check the requested hostname and which Caddy site block handles it. |
A changed .env value seems ignored |
Check for a shell variable overriding it, then recreate the affected container. |
An application bound only to 127.0.0.1 inside its own container won’t accept connections arriving on the container’s network interface. Make it listen on an appropriate container interface, usually 0.0.0.0. The included Nginx listen 8080 already does that for IPv4.
Adding host ports won’t repair a misspelled alias, wrong upstream port or wrong listener. Test the failing hop first.
Put HTTPS at the public edge
For a directly exposed public domain, a minimal Caddy route can look like this:
example.com {
reverse_proxy aakash-site:8080
}
In that arrangement, point the domain’s DNS to your VPS, make TCP 80 and 443 reach Caddy, publish the corresponding Caddy container ports, and persist Caddy’s /data directory for certificate state. Caddy can then obtain and renew a public certificate automatically. Its automatic HTTPS documentation covers the requirements and challenge options.
Our infrastructure uses explicitly configured origin certificate files and imported settings for its Caddy sites. I would add a route to that existing proxy and follow its certificate setup. There is no reason to start a second Caddy competing for the same host ports just to deploy this site.
The local example’s 127.0.0.1:18080:8080 mapping is for trying the route on your machine. It isn’t the public 80/443 mapping above. Also, a browser using HTTPS to Caddy doesn’t automatically mean the next hop uses HTTPS: aakash-site:8080 uses HTTP on this local Docker network. HTTPS to an upstream is a separate transport choice.
Only attach services that the proxy needs to reach. A shared edge network doesn’t need your database just because the application uses one. Containers sharing a bridge can reach one another’s listening ports; an omitted ports section doesn’t isolate them from their network peers.
Clean up the example
From the example directory:
docker compose -f compose.caddy.yaml down --volumes
docker compose -f compose.site.yaml down
docker network rm caddy-guide-edge
The first command removes this demo proxy’s volumes too. The external network stays until we explicitly remove it after both projects stop.
The actual site keeps its own Compose project, and Caddy keeps its own infrastructure configuration. Rebuilding the site replaces the site container. The shared network and stable alias give the proxy somewhere to find it again.
This sits alongside the observability setup I wrote about earlier, but you can use the same arrangement for a personal site or another small service. One Docker host, a shared network, a named upstream and the right listening port. Once those are clear, the Compose file with no published application port makes rather more sense.