1. The problem I’m having:
Hi!
By default, when a service is scaled in Docker Compose
using the deploy.replicas
property (as can be seen in the example in the docker-compose.yml
file), multiple instances of the same service are created and registered in the Docker DNS
server with the same domain name but pointing to different IPs
.
In this scenario, when configuring a reverse proxy to that service in Caddy Server
, it is usually done as follows:
:80 {
reverse_proxy whoami:8000
}
With that configuration, when several consecutive calls are made to the final service through Caddy Server
(calling localhost:8080
), the requests are always resolved towards the same service and the same IP is always used.
~ ❯ curl http://localhost:8080
I'm fa9fca8a5293
~ ❯ curl http://localhost:8080
I'm fa9fca8a5293
~ ❯ curl http://localhost:8080
I'm fa9fca8a5293
~ ❯ curl http://localhost:8080
I'm fa9fca8a5293
~ ❯ curl http://localhost:8080
I'm fa9fca8a5293
~ ❯ curl http://localhost:8080
I'm fa9fca8a5293
~ ❯ curl http://localhost:8080
I'm fa9fca8a5293
On the other hand, if a dynamic upstream
is used, as shown in the following configuration, the requests are balanced between the different instances created.
:80 {
reverse_proxy {
lb_policy round_robin
dynamic a {
name whoami
port 8000
refresh 1s
}
}
}
~ ❯ curl http://localhost:8080
I'm ece316ecfe32
~ ❯ curl http://localhost:8080
I'm b5cfbdb0ce4c
~ ❯ curl http://localhost:8080
I'm 8c63835eace1
~ ❯ curl http://localhost:8080
I'm b5cfbdb0ce4c
~ ❯ curl http://localhost:8080
I'm ece316ecfe32
~ ❯ curl http://localhost:8080
I'm b5cfbdb0ce4c
~ ❯ curl http://localhost:8080
I'm b5cfbdb0ce4c
The question is: when simply using reverse_proxy whoami:8000
, does Caddy Server cache the first IP returned by the DNS
server and does not make a balancing between the different entries that are registered in the Docker
DNS
server? To make a balancing between the different instances the only solution is to use a dynamic upstream
? Services like Nginx
by default perform that balancing between all DNS
entries for a service, hence my question.
Thank you very much.
2. Error messages and/or full log output:
N/A
3. Caddy version:
v2.8.4
running in docker container
4. How I installed and ran Caddy:
Using docker with the following image:
caddy:latest
a. System environment:
- OS:
Ubuntu 24.04
- Docker Version: `Docker version 27.3.1, build ce12230``
b. Command:
docker compose up
c. docker-compose.yml:
services:
caddy:
image: caddy:latest
container_name: caddy
ports:
- 8080:80
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
whoami:
image: jwilder/whoami
deploy:
replicas: 3
d. Caddyfile:
- With standard reverse proxy
:80 {
reverse_proxy whoami:8000
}
- With dynamic upstream reverse proxy
:80 {
reverse_proxy {
lb_policy round_robin
dynamic a {
name whoami
port 8000
refresh 1s
}
}
}
5. Links to relevant resources:
Another post with a related question: Loadbalance docker compose replicas