I cannot get reverse proxy to work on localhost with docker-compose

1. Caddy version (caddy version):

caddy:2.0.0

2. How I run Caddy:

docker-compose up

a. System environment:

Docker

b. Command:

docker-compose up

c. Service/unit/compose file:

paste full file contents here

d. My complete Caddyfile or JSON config:

{
    #auto_https off
    #auto_https disable_redirects
    email   email@gmail.com
}


# reverse proxy trial
:8080 {
    reverse_proxy {
        #to s3zipper:8000
        to :8000	
    }
}

DO NOT REDACT anything except credentials

3. The problem I’m having:

I have a server that runs on port:8000. It can run directly on my localhost or inside docker successfully.
My main problem is that I cannot successfully get my caddy server to reverse proxy into it.
I am somehow missing something.

4. Error messages and/or full log output:

I seem to be getting different versions of this error

caddyserver    | {"level":"error","ts":1596482182.868769,"logger":"http.log.error","msg":"dial tcp: lookup s3zipper on 127.0.0.11:53: no such host","request":{"method":"GET","uri":"/","proto":"HTTP/1.1","remote_addr":"172.27.0.1:48230","host":"localhost:8080","headers":{"Connection":["keep-alive"],"Upgrade-Insecure-Requests":["1"],"User-Agent":["Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:79.0) Gecko/20100101 Firefox/79.0"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"],"Accept-Language":["en-US,en;q=0.5"],"Accept-Encoding":["gzip, deflate"]}},"duration":0.396482222,"status":502,"err_id":"cekmpfsx7","err_trace":"reverseproxy.(*Handler).ServeHTTP (reverseproxy.go:380)"}

caddyserver    | {"level":"error","ts":1596483060.308037,"logger":"http.log.error","msg":"dial tcp :8000: connect: connection refused","request":{"method":"GET","uri":"/","proto":"HTTP/1.1","remote_addr":"172.27.0.1:42714","host":"localhost:8080","headers":{"Sec-Fetch-Site":["none"],"Sec-Fetch-User":["?1"],"Sec-Fetch-Dest":["document"],"User-Agent":["Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36"],"Upgrade-Insecure-Requests":["1"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"],"Sec-Fetch-Mode":["navigate"],"Accept-Encoding":["gzip, deflate, br"],"Accept-Language":["en-GB,en;q=0.9,sw-TZ;q=0.8,sw;q=0.7,en-US;q=0.6"],"Connection":["keep-alive"],"Dnt":["1"]}},"duration":0.000345104,"status":502,"err_id":"670cjjeb2","err_trace":"reverseproxy.(*Handler).ServeHTTP (reverseproxy.go:380)"}

5. What I already tried:

I have made sure that server actually runs . I think the problem is dealing with the connection refused part.

6. Links to relevant resources:

docker-compose.yml

version: "3"

services:
  caddyserver:
    image: caddy:2.0.0
    container_name: caddyserver
    hostname: caddyserver
    #user: root
    # user: ${CURRENT_UID}
 
    ports:
      - 443:443
      - 80:80
      - 8080:8080

    volumes:
      - "./caddy_secrets/data_lets_encrypt_storage:/data"
      - "./caddy_secrets/config_storage:/config"
      - ./Caddyfile:/etc/caddy/Caddyfile

  mongo:
    image: mongo
    expose:
      - "27017"

  awszipper:
    build: .
    image: awszipper
    depends_on:
      - 'mongo'
    ports:
      - "8000:8000"
    volumes:
      - .:/go
 
volumes:
  mongo:

You’re using v2.0.0, the latest is v2.1.1, so please upgrade!

I think this should do the trick:

:8080 {
	reverse_proxy awszipper:8000
}

When running with Docker, the Docker network stack will be configured with the container/service names as valid domains to point to. s3zipper is not the name of your container, so Caddy can’t resolve that domain to anything. Using :8000 makes Caddy assume localhost (I think) but that will only point to services running in the same container as the Caddy container (i.e. nothing else). Each container gets their own IP address.

1 Like

Perfect!
This was the small win i needed.

2 Likes

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