Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
OmnesPotens opened this issue Feb 20, 2025 · 1 comment

Comments

@OmnesPotens
Copy link

OmnesPotens commented Feb 20, 2025

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>
}
...
@francislavoie francislavoie transferred this issue from caddyserver/caddy Feb 20, 2025
@francislavoie
Copy link
Member

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants