Docker compose localhost 502 error

Hi all!

Just wondering if someone would be kind enough to help, we’re setting up running Caddy locally for a bunch of stuff and we’re just setting up a Docker compose file to spin up our sites locally. Once we have everything up and running I can navigate to localhost:7000/login and everything works fine, however if I hit the configured Caddy url I get 502 Bad Gateway.

My Dockerfile looks like the following:

version: '3.4'

services:

 app_a:
    image: app-a
    command: ./node_modules/.bin/nodemon .
    expose:
      - "7000"
    ports:
      - 7000:8080

  app_b:
    image: app-b
    command: ./node_modules/.bin/nodemon .
    expose:
      - "7100"
    ports:
      - 7100:8081

  proxy:
    image: abiosoft/caddy
    volumes:
      - ./Caddyfile:/etc/Caddyfile
    expose:
      - "1234"
    ports:
      - 1234:1234
    depends_on:
      - app_a
      - app_b

And my Caddyfile is as follows:

localhost:1234 {
  proxy /app-a localhost:7000
  proxy /app-b localhost:7100
}

If i navigate to or curl localhost:1234 I see the caddy proxy running successfully, however localhost:1234/app-a or app-b returns me a 502.

I’m hoping it’s something trivial I’m doing wrong.

Thanks

Ok, so I’ve just solved this and I though I’d share the answer. It looks like I was being a little silly when I was creating the Caddyfile, if you change the name to the image-name and the port to the Docker port it should work. Here is my updated file:

localhost:1234 {
  proxy /app-a app_a:8080
  proxy /app-b app_b:8081
}

I hope this helps anyone who looks at this.

1 Like

Looks like you got it, @Garfty.

The reason this happens is that a Docker container considers itself to be its own host, so references to localhost refer to itself rather than the actual host running Docker.

Alternately, if you were to run Caddy with network_mode: "host", then references to localhost would work as long as the exposed port maps were correct.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.