Caddy reverse proxy on docker local test

1. Caddy version (caddy version):

v2.4.6

2. How I run Caddy:

Inside a Docker container, my node.js application sits in another container

a. System environment:

MacOs Monterey 12.2.1
Docker desktop 4.5.0

b. Command:

docker run -d --name caddy -p 80:80 -p 443:443 -p 2019:2019 <c>

c. Service/unit/compose file:

version: "3.9"
services:
  woodbit-pro:
    container_name: woodbit-pro
    build:
      context: .
      dockerfile: Dockerfile-pro
    expose:
      - "80"
      - "443"

d. My complete Caddyfile or JSON config:

{
  "apps": {
    "http": {
      "servers": {
        "localhost": {
          "listen": [":443"],
          "routes": [
            {
              "match": [
                {
                  "host": ["localhost"]
                }
              ],
              "handle": [
                {
                  "handler": "reverse_proxy",
                  "upstreams": [
                    {
                      "dial": "172.18.0.2:5001"
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    }
  }
}

3. The problem I’m having:

I am trying to run the app on localhost, it is accessible on localhost:5001, I want the caddy to do a reverse proxy to it, the ip address in the upstream is the ip of the docker container running the app.

4. Error messages and/or full log output:

It does not work, in Chrome network when going to localhost, it redirects to https://localhost but then responds with
Failed to load resource: the server responded with a status of 502 ()

5. What I already tried:

Tried a lot of variations, with no result.
Thanks for helping: my problem is of course lack of knowledge as a beginner.
But I also did not find a json example of reverse proxy to other docker container.

6. Links to relevant resources:

Rather stupid mistake! I used the docker ip of the caddy container 172.17.0.2:5001
It should have been 172.17.0.3:5001, the ip of the app container

It works!!
sorry my mistake

2 Likes

You can use the container name if they’re in the same network, instead of the IP. That way you don’t need to rely on Docker assigning the same IP address to the container (cause it might not if you spin more up)

1 Like

Thanks Francis, great tip!

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.