lucaslorentz / caddy-docker-proxy

Caddy as a reverse proxy for Docker
MIT License
2.84k stars 169 forks source link

Running Caddy Docker Proxy on host networking #432

Open PixelHir opened 1 year ago

PixelHir commented 1 year ago

Hi, is it possible to run caddy-docker-proxy on host networking so I don't need to assign it to same networks as the containers? I used to do that with traefik and wanted to replicate this on cdp but I'm encountering problems as below:

image I've setup a basic whoami container for testing purposes, but I am unable to access it

I'm able to access the said container from the host network image

My docker compose: image

OnigamiSama commented 1 year ago

Hello,

I run mine in network mode host for a few weeks and everything seems to work fine with this docker-compose.yml

services:
  caddy:
    image: lucaslorentz/caddy-docker-proxy:ci-alpine
    container_name: caddy
    network_mode: host
#    ports:
#      - 80:80
#      - 443:443
    environment:
      - CADDY_INGRESS_NETWORKS=caddy
#    networks:
#      - caddy
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - caddy_data:/data
    extra_hosts:
      - host.docker.internal:host-gateway
    restart: unless-stopped

#networks:
#  caddy:
#    external: true

volumes:
  caddy_data: {}
psyciknz commented 1 year ago

The container I’m trying to proxy has to be in host network (homebridge), what are the labels to get this to work, as mine in complaining they are not in the same network

OnigamiSama commented 1 year ago

Here is an exemple, my docker-compose for plexmediaserver in host mode networking with caddy-docker-proxy

services:
  plex:
    image: lscr.io/linuxserver/plex
    runtime: nvidia
    container_name: plex
    network_mode: host
    environment:
      - PUID=1000
      - PGID=1000
      - VERSION=docker
      - NVIDIA_VISIBLE_DEVICES=all
    volumes:
      - ~/docker/containers/plex/config:/config
      - /zdata/mediaserver/library:/mnt/mediaserver/library:ro
      - ~/docker/containers/plex/transcode:/tmp
    labels:
      caddy: sub.domain.tld
      caddy.reverse_proxy: host.docker.internal:32400
    restart: unless-stopped
psyciknz commented 1 year ago

hmm I can try this, but in a container I can't get host.docker.internal to resolve to anything. If I run this: docker run --net bridge --rm alpine nslookup gateway.docker.internal I can see it bouncing off my external DNS servers, and I don't quite know what --dns entry I can put in to override the network ones.

ulope commented 6 months ago

@psyciknz You need to tell the caddy container about the docker host hostname.

Taken from @OnigamiSama's example:

    extra_hosts:
      - host.docker.internal:host-gateway