lucaslorentz / caddy-docker-proxy

Caddy as a reverse proxy for Docker
MIT License
2.6k stars 162 forks source link

`Failed to get Container Caddyfile` #614

Open Kenya-West opened 1 month ago

Kenya-West commented 1 month ago

I run your Caddy:

version: "3.8"
services:
  caddy:
    image: lucaslorentz/caddy-docker-proxy:ci-alpine
    container_name: caddy
    ports:
      - 80:80
      - 443:443
      - "443:443/udp"
    environment:
      - CADDY_INGRESS_NETWORKS=caddy
      - CADDY_DOCKER_CADDYFILE_PATH=/config/Caddyfile
    env_file:
      - .env
    networks:
      - caddy
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./caddy_data:/data
      - ./config/Caddyfile:/config/Caddyfile
      - ./logs:/var/log/caddy
      - ./static:/var/www/public
      - ./certs:/data/caddy/certificates/acme-v02.api.letsencrypt.org-directory
    restart: unless-stopped

networks:
  caddy:
    external: true

volumes:
  caddy_data: {}

with Caddyfile:

{$DOMAIN} {
    log {
        output file /var/log/caddy/{$DOMAIN}/{$DOMAIN}-access.log
    }

    root * /var/www/public
    file_server
    rewrite / /index.html

    reverse_proxy /{$FRP_CADDY_PATH}/frps-dashboard* http://{$EXTERNAL_IP}:{$FRPS_DASHBOARD_PORT} {

    }
    reverse_proxy /{$FRP_CADDY_PATH}/frpc-dashboard* http://{$EXTERNAL_IP}:{$FRPC_DASHBOARD_PORT} {

    }
}

All works fine until I try to add another container in caddy network like so:

---
version: "3.8"

services:
  xui:
    image: alireza7/x-ui
    container_name: x-ui
    hostname: ${XUI_HOSTNAME}
    volumes:
      - type: bind
        source: ./bin/config.json
        target: /app/bin/config.json
    environment:
      XRAY_VMESS_AEAD_FORCED: "false"
    tty: true
    restart: unless-stopped
    networks:
      - caddy
    labels:
      caddy: ${DOMAIN}
      caddy.0_handle_path: "/${XUI_DASHBOARD_PATH}*"
      caddy.0_rewrite: "* /${XUI_DASHBOARD_PATH}${uri}"
      caddy.0_reverse_proxy: "{{http://xui:${XUI_DASHBOARD_PORT}}}"
      caddy.1_handle_path: "/sub/*"
      caddy.1_reverse_proxy: "{{http://xui:2096}}"
      caddy.2_handle_path: "/${XUI_VLESS_WS_PATH}"
      caddy.2_reverse_proxy: "{{http://xui:${XUI_VLESS_WS_PORT}}}"

networks:
  caddy:
    external: true

It messages me with this error:

{"level":"error","ts":1715080837.0598302,"logger":"docker-proxy","msg":"Failed to get Container Caddyfile","container":"d08e9957493ed2312248ffc7bef3eca2d73fed5847f43e0b521d0d5c97f0db88","error":"template: :1: expected :="}

What is even this? It cannot be even googled. I know that something is being wrong with (the resulted one?) Caddyfile, but I do not know what is exactly. The id of container points to the docker-compose file above, so xui container has provided something to caddy with wrong attributes

Kenya-West commented 1 month ago

If I get rid of Caddyfile in Caddy's docker-compose.yml:

environment:
      - CADDY_INGRESS_NETWORKS=caddy
-      - CADDY_DOCKER_CADDYFILE_PATH=/config/Caddyfile

- then it works just fine. What is the problem with Caddyfile?

Kenya-West commented 1 month ago

I checked Caddy's container logs and now I get it: in-memory's Caddyfile has duplicated domain name:

kek.example.com
kek.example.com {
    handle_path /xui-dashboard*
    reverse_proxy 172.18.0.3:54444
    handle_path /sub*
    reverse_proxy 172.18.0.3:2099
    rewrite * /sub{uri}
    handle_path /xui-vless-ws*
    reverse_proxy 172.18.0.3:1488
}

This is the result of merging the pre-defined Caddyfile and the X-UI container which has the same domain defined.

How to avoid this?

francislavoie commented 1 month ago
      caddy.1_handle_path: "/sub/*"
      caddy.1_reverse_proxy: "{{http://xui:2096}}"
      caddy.2_handle_path: "/${XUI_VLESS_WS_PATH}"
      caddy.2_reverse_proxy: "{{http://xui:${XUI_VLESS_WS_PORT}}}"

This isn't right, you need to nest reverse_proxy inside of handle_path, by doing caddy.1_handle_path.reverse_proxy