lucaslorentz / caddy-docker-proxy

Caddy as a reverse proxy for Docker
MIT License
2.61k stars 163 forks source link

Reverse proxy doesn't work when using internal network #582

Closed chinnuu05 closed 5 months ago

chinnuu05 commented 5 months ago

I'm using caddy-docker-proxy to serve my Django web app but am encountering some strange networking issues

Here's my compose file, it places caddy and django on an external network together

version: "3.7"
services:

  web:
    build: .
    command: sh -c "gunicorn notobackend.wsgi:application --bind 0.0.0.0:8000"
    restart: always
    ports:
      - "8000:8000"
    environment:
      - POSTGRES_DB=supadb
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    networks:
      - caddy
      - internal
    labels:
      caddy: supa.blog
      caddy.reverse_proxy: "{{upstreams 8000}}"
    env_file:
      - .env
    depends_on:
      - db 

  db:
    image: postgres:latest 
    restart: always
    ports:
      - 5432
    environment:
      - POSTGRES_DB=supadb
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    volumes:
      - './data/db:/var/lib/postgresql/data'

  caddy:
      image: lucaslorentz/caddy-docker-proxy:ci-alpine
      ports:
        - 80:80
        - 443:443
      environment:
        - CADDY_INGRESS_NETWORKS=caddy
      networks:
        - caddy
      volumes:
        - /var/run/docker.sock:/var/run/docker.sock
        - caddy_data:/data
      restart: unless-stopped

networks:
        caddy:
                external: true
        internal:
                internal: true
                driver: bridge

volumes:
  caddy_data: {}

22ewEXxOye

When deploying this compose file it works as long as I don't assign a network to my db container. I get a 500 Error but that's because Django cant find the db.

chrome_y99KmaTZiz

So I assigned an internal network to db to make it visible to Django, and this is where the problems arise. Instead of Caddy proxying requests to my django web app like it did before, I get a Not Found error.

Removing the

networks:
   - internal

on the db container fixes it and Caddy shows me the Django 500 error again

But if I try to assign the internal network to my db container, Caddy immediately gives a "Not Found" and stops proxying to Django. Why does this happen?

francislavoie commented 5 months ago

For reference, it was answered here: https://caddy.community/t/using-caddy-in-docker-compose-upstreams-breaks-when-using-internal-network/22585

TL;DR the 404 is not coming from Caddy, but from the Django app itself. There's no problem with Caddy here.