lucaslorentz / caddy-docker-proxy

Caddy as a reverse proxy for Docker
MIT License
2.86k stars 168 forks source link

Creating reverse proxy only works via Caddyfile, not labels #586

Open ericgroom opened 7 months ago

ericgroom commented 7 months ago

Probably a dumb configuration issue, but I think I have a minimal example and have double checked instructions. This reverse proxy works when configured via a Caddyfile, but not via the labels.

compose.yml

services:
  whoami0:
    image: traefik/whoami
    networks:
      - caddy
    labels:
      caddy: test.mydomain.dev
      caddy.reverse_proxy: "{{upstreams 80}}"
  caddy:
    build:
      context: .
      dockerfile: Dockerfile
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"
    environment:
      - CADDY_INGRESS_NETWORKS=caddy
      - CADDY_DOCKER_CADDYFILE_PATH=/etc/caddy/Caddyfile
      - GOOGLE_DOMAINS_ACCESS_TOKEN=${GOOGLE_DOMAINS_ACCESS_TOKEN}
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - caddy_data:/data
      - ./Caddyfile:/etc/caddy/Caddyfile
    networks:
      - caddy

volumes:
  caddy_data: {}

networks:
  caddy:
    external: true
    name: caddy

Dockerfile

FROM caddy:2.7.6-builder AS builder

RUN xcaddy build \
    --with github.com/caddy-dns/google-domains \
    --with github.com/lucaslorentz/caddy-docker-proxy/v2

FROM caddy:2.7.6

COPY --from=builder /usr/bin/caddy /usr/bin/caddy

Caddyfile

{
    email redacted@redacted.com
    acme_dns google_domains {env.GOOGLE_DOMAINS_ACCESS_TOKEN}
}

test.mydomain.dev {
    reverse_proxy whoami0 # caddy and docker are on the same network, so using hostname works fine
}

I've tried without a Caddyfile and get the same result; I'd like to keep it as I have some redirects I'd like to do outside of proxying to containers. I never see any log messages in the caddy container when spinning up containers using caddy labels so I assume it's not seeing them start up?

Any help debugging would be much appreciated!

francislavoie commented 7 months ago

What does "doesn't work" mean? What error do you see? What's in your logs? Show an example request with curl -v. Enable the debug global option.

funkyapple commented 6 months ago

This may not be related, but I had the same issue where the caddy-docker-proxy module was not loading. This resulted in labels not working. I fixed this by changing to the alpine version of the caddy docker image.

FROM caddy:builder AS builder

RUN xcaddy build \
    --with github.com/lucaslorentz/caddy-docker-proxy/v2

FROM caddy:alpine

COPY --from=builder /usr/bin/caddy /usr/bin/caddy

CMD ["caddy", "docker-proxy"]