website icon indicating copy to clipboard operation
website copied to clipboard

[Docs] Specify that using reverse_proxy for Docker containers should utilize the *internal* container port

Open OmnesPotens opened this issue 10 months ago • 1 comments

I have been scrambling to figure out why my local Docker Compose setup utilizing Caddy as a reverse proxy wasn't working and it all boiled down to a single, simple Caddyfile change (using the internal container port instead of the host port).

Link to relevant docs: https://caddyserver.com/docs/running#docker-compose Link to forum post coment that gave the proper solution: https://caddy.community/t/caddy-foundry-docker-connection-issues/21265/2

I hope this breakdown helps others who may have been banging their heads against the same problem.

I was under the impression that my Caddyfile should look like:

...
localhost {
	reverse_proxy app:3030 <--- <container name>:<host machine port>
}
...

With a Dockerfile like:

caddy:
    ...
    restart: unless-stopped
    environment:
      - ACME_AGREE=true
    volumes:
      - /caddy/Caddyfile:/etc/caddy/Caddyfile
      - /caddy/site:/srv
      - caddy_data:/data
      - caddy_config:/config
    ports:
      - 80:80
      - 443:443
      - 443:443/udp
    depends_on:
      - app
app:
    ...
    container_name: app
    ports:
      - 3030:3000
    restart: unless-stopped

But that just resulted in my app being unreachable. In reality all I needed was to utilize the internal container port that was being used by the app.

So my Dockerfile could look like:

caddy:
    ...
    restart: unless-stopped
    environment:
      - ACME_AGREE=true
    volumes:
      - /caddy/Caddyfile:/etc/caddy/Caddyfile
      - /caddy/site:/srv
      - caddy_data:/data
      - caddy_config:/config
    ports:
      - 80:80
      - 443:443
      - 443:443/udp
    depends_on:
      - app
app:
    ...
    container_name: app
   ### Don't even need to bind ports
    # ports:
     #  - 3030:3000
    restart: unless-stopped

With a Caddyfile like:

...
localhost {
	reverse_proxy app:3000<--- <container name>:<internal container port>
}
...

OmnesPotens avatar Feb 20 '25 20:02 OmnesPotens

I moved the issue to https://github.com/caddyserver/website which is where the docs live.

I agree the Docker docs can mention some tips like this regarding proxying and ports 👍

PR welcome if you want to take a crack at adding it!

francislavoie avatar Feb 20 '25 21:02 francislavoie