Not able to get caddy working as reverse proxy in docker compose

1. The problem I’m having:

Hello,
I am trying to get caddy working with photoview in docker (docker compose). Photoview uses port 8000 and I setup caddy as a reverse proxy 80 → 8000. I have been working on this for days and also searching for answers…

If expose port 8000 to test photoview directly, I get html returned for:
curl -vL 127.0.0.1:8000

** To test photoview I adding port mapping**

ports:
  - 8000:8000

but it fails on port 80 (from host)

2. Error messages and/or full log output:

curl -vL 127.0.0.1:80
*   Trying 127.0.0.1:80...
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1
> User-Agent: curl/7.81.0
> Accept: */*
> 
* Recv failure: Connection reset by peer
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer
"level":"info","ts":1696989107.3244312,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
{"level":"info","ts":1696989107.326113,"logger":"admin","msg":"admin endpoint started","address":"localhost:2019","enforce_origin":false,"origins":["//localhost:2019","//[::1]:2019","//127.0.0.1:2019"]}
{"level":"warn","ts":1696989107.326193,"logger":"http.auto_https","msg":"server is listening only on the HTTP port, so no automatic HTTPS will be applied to this server","server_name":"srv0","http_port":80}
{"level":"info","ts":1696989107.3262837,"logger":"tls.cache.maintenance","msg":"started background certificate maintenance","cache":"0xc0001c2a80"}
{"level":"info","ts":1696989107.3264422,"logger":"tls","msg":"cleaning storage unit","description":"FileStorage:/data/caddy"}
{"level":"info","ts":1696989107.3264537,"logger":"http.log","msg":"server running","name":"srv0","protocols":["h1","h2","h3"]}
{"level":"info","ts":1696989107.326467,"logger":"tls","msg":"finished cleaning storage units"}
{"level":"info","ts":1696989107.3265903,"msg":"autosaved config (load with --resume flag)","file":"/config/caddy/autosave.json"}
{"level":"info","ts":1696989107.3266044,"msg":"serving initial configuration"}

3. Caddy version:

Docker version 24.0.5, build 24.0.5-0ubuntu1~22.04.1
caddy is latest

4. How I installed and ran Caddy:

docker compose

a. System environment:

Docker version 24.0.5, build 24.0.5-0ubuntu1~22.04.1
kubuntu 22.04

b. Command:

c. Service/unit/compose file:

version: "3"

services:
  db:
    image: mariadb:10.5
    container_name: db
    restart: always
    environment:
      - MYSQL_DATABASE=photoview
      - MYSQL_USER=photoview
      - was password
      - MYSQL_RANDOM_ROOT_PASSWORD=1
    volumes:
      - db_data:/var/lib/mysql

  photoview:
    image: viktorstrate/photoview:2
    container_name: photoview
    restart: always
    #ports:
    #  - 8000:8000
    expose:
      - 8000

    depends_on:
      - db

    environment:
      - PHOTOVIEW_DATABASE_DRIVER=mysql
      - PHOTOVIEW_MYSQL_URL=photoview:photosecret@tcp(db)/photoview
      - PHOTOVIEW_LISTEN_IP=127.0.0.1
      - PHOTOVIEW_LISTEN_PORT=8000
      - PHOTOVIEW_MEDIA_CACHE=/app/cache
      
    volumes:
      - api_cache:/app/cache
      - /home/gdan/photos/:/photos:ro
        #networks:
        #- proxy-network

  caddy:
    image: caddy:latest
    container_name: reverse_proxy
    restart: unless-stopped
    ports:
      - 80:8000

    depends_on:
      - photoview

    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - ./site:/srv
      - caddy_data:/data
      - caddy_config:/config
        #networks:
        #- proxy-network
        
volumes:
  db_data:
  api_cache:
  caddy_data:
  caddy_config:

    #networks:
    #proxy-network:

d. My complete Caddy config:

:80
reverse_proxy 127.0.0.1:8000

and tried based on some suggested similar topics

:80 {
        reverse_proxy photoview:8000
}

5. Links to relevant resources:

You have this backwards – this is binding to 80 on the host, connecting to 8000 inside the Caddy container. You have Caddy listening for 80 but it’s going to be receiving requests on port 8000.

Switch that to - 80:80, or switch your Caddyfile to :8000. And make sure your curl command uses whatever port you choose the bind on the host (left side).

hi
I am re-reading how ports work in compose to understand what I have backwards.
I understood it to be host:container

thank you

Correct, but the port you bind to on the container needs to match what’s actually running inside the container. You had Caddy listening on port 80, but packets are coming in on port 8000, so they’re not connected.

1 Like

I have it working: THANK YOU :slight_smile:

Here is the working caddy and yaml for other to see:

:80 {
        reverse_proxy photoview:8000
}

caddy file listens to host port 80
forwards to photoview (IP) instance on port 8000

version: "3"

services:
  db:
    image: mariadb:10.5
    container_name: db
    restart: always
    environment:
      - MYSQL_DATABASE=photoview
      - MYSQL_USER=photoview
      - MYSQL_PASSWORD=
      - MYSQL_RANDOM_ROOT_PASSWORD=1
    volumes:
      - db_data:/var/lib/mysql

  photoview:
    image: viktorstrate/photoview:2
    container_name: photoview
    restart: always

    depends_on:
      - db

    environment:
      - PHOTOVIEW_DATABASE_DRIVER=mysql
      - PHOTOVIEW_MYSQL_URL=photoview:photosecret@tcp(db)/photoview
      - PHOTOVIEW_LISTEN_IP=127.0.0.1
      - PHOTOVIEW_LISTEN_PORT=8000
      - PHOTOVIEW_MEDIA_CACHE=/app/cache
      
    volumes:
      - api_cache:/app/cache
      - /home/gdan/photos/:/photos:ro

  caddy:
    image: caddy:latest
    container_name: reverse_proxy
    restart: unless-stopped
    ports:
      - 80:80

    depends_on:
      - photoview

    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - ./site:/srv
      - caddy_data:/data
      - caddy_config:/config
        
volumes:
  db_data:
  api_cache:
  caddy_data:
  caddy_config:

ports - host:container
listen to host on 80
caddy container is also listening on 80 (but caddy file sends it to phtoview:8000)

did i say that correctly?

1 Like

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