I know this is an old thread, but it also seems to be the only place on the Internet I found that discusses the same problem I had recently. Above, @johnfo mentions --net=host
as a solution, and while it was not clear to me how to apply that in my case (a docker newbie here), their message helped me to figure it out. Posting here my case and my solution, hoping it helps someone coming from Google with the same or similar issue.
TL;DR:
Using network_mode: host
in compose.yaml
fixed the issue.
The details:
I have 3 docker containers on a single host machine. One container is a Caddy server that works as a reverse proxy for two websites (alpha.com
and beta.com
). The other two containers serve those websites on ports 3001 and 3002. I have a compose file to manage all three containers.
In this common setup, I have one special requirement: Alpha needs to connect to Beta using its external public URL https://beta.com/*
. Since that piece of code inside Alpha works both on the server-side (generating HTML pages), as well as on the client side (web app working in the browser), I can’t just replace the address with the internal docker hostname http://beta:3002/
. I need one “universal” URL that works both on the server and in the visitor’s browser.
# compose.yaml
services:
caddy:
container_name: caddy
image: caddy:latest
restart: unless-stopped
ports:
- "80:80"
- "443:443"
- "443:443/udp"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
- caddy_data:/data
- caddy_config:/config
alpha:
container_name: alpha
build:
context: ./alpha/
environment:
PORT: 3001
restart: unless-stopped
beta:
container_name: beta
build:
context: ./beta/
environment:
PORT: 3002
restart: unless-stopped
volumes:
caddy_data:
caddy_config:
# Caddyfile
alpha.com {
reverse_proxy alpha:3001
}
beta.com {
reverse_proxy beta:3002
}
With the config above, Alpha couldn’t connect the https://beta.com/*
. What helped me to fix it is to move docker network to the host level, using network_mode: host
in each of compose’s services. With this, I also removed no longer required setting ports
in the caddy service, and replaced hostnames in Caddyfile with localhost
. So now my configs look like this:
# compose.yaml
services:
caddy:
container_name: caddy
image: caddy:latest
restart: unless-stopped
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
- caddy_data:/data
- caddy_config:/config
network_mode: host
alpha:
container_name: alpha
build:
context: ./alpha/
environment:
PORT: 3001
restart: unless-stopped
network_mode: host
beta:
container_name: beta
build:
context: ./beta/
environment:
PORT: 3002
restart: unless-stopped
network_mode: host
volumes:
caddy_data:
caddy_config:
# Caddyfile
alpha.com {
reverse_proxy localhost:3001
}
beta.com {
reverse_proxy localhost:3002
}
And now Alpha can successfully connect to https://beta.com/*
from inside its container.
Hoping this helps!