CaddyV2 in docker able to resolve some docker containers by hostname but not others

1. Caddy version (caddy version):

Whatever the caddy image being pulled by the below docker compose:

2. How I run Caddy:

Docker

a. System environment:

Raspberry Pi 3 with Docker.

b. Command:

c. Service/unit/compose file:

version: "3.8"
services:
  caddy:
    image: caddy
    container_name: caddy
    hostname: caddy
    env_file: ../.env
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    environment:
      - DOMAIN
      - DOMAIN_LOCAL
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - ./data:/data
      - ./config:/config

networks:
  default:
    external:
      name: $NETWORK

d. My complete Caddyfile or JSON config:

{
    # acme_ca https://acme-staging-v02.api.letsencrypt.org/directory
}

portainer.{$DOMAIN} portainer.{$DOMAIN_LOCAL} {
    reverse_proxy portainer:9000
}

home.{$DOMAIN} home.{$DOMAIN_LOCAL} {
    reverse_proxy homeassistant:8123
}

3. The problem I’m having:

When i replace homeassistant with 192.168.1.100 (which is the local IP of the Raspberry Pi running caddy, portainer, and home assistant docker containers) in the home. route the caddy reverse proxy works perfect and resolves to the home assistant docker container.

But the portainer:9000 works fine! It perfectly routes the portainer. subdomian to the docker container on the Pi.

Here is my docker-compose for portainer which works great with the caddyFile

version: '3.8'

services:
  portainer:
    image: portainer/portainer
    container_name: portainer
    hostname: portainer
    env_file: ../.env
    command: -H unix:///var/run/docker.sock
    restart: unless-stopped
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./data:/data
    ports:
      - 9000:9000

networks:
  default:
    external:
      name: $NETWORK

Here is my homeassistant docker-compose:

version: '3.8'
services:
  homeassistant:
    image: homeassistant/raspberrypi3-homeassistant:stable
    container_name: homeassistant
    hostname: homeassistant
    env_file: ../.env
    network_mode: host # exposes all ports, needed for multicast DNS
    restart: always
    volumes:
      - ./config:/config
    devices:
      - /dev/ttyUSB0:/dev/ttyUSB0:rwm
      - /dev/ttyUSB1:/dev/ttyUSB1:rwm

networks:
  default:
    external:
      name: $NETWORK

As you can see they are both connected to the same docker network which i have created externally and both reference using the the same .env file.

4. Error messages and/or full log output:

502 bad gateway when i try to use homeassistant:8123 instead of 192.168.1.100:8123.

5. What I already tried:

I have found out that putting the local IP works, this doesn’t seem to be caused by home assistant because keeping it’s config the same and changing the reverse proxy route works.

Is this due to the network_mode: host?

6. Links to relevant resources:

Is this due to the network_mode: host?

Probably, yes. I think the homeassistant container won’t exist in the network Caddy is in, so it won’t be able to connect. So you’ll need to use the host IP to connect to it, I guess.

I’m sure others have run into a similar issue with homeassistant, this isn’t something specific to Caddy, really. It’s more of a general Docker thing.

1 Like

My bad, thank you for the information! I 'll ask around on the docker forums. But it looks like this might be the culprit since i think network_mode: host makes it impossible for a container to be on a docker network.

Thanks again! Caddy Rules!!! SOOOOO much easier than nginx and automatic HTTPS is. dream :heart_eyes:

2 Likes

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