lucaslorentz / caddy-docker-proxy

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

Upstream from a Dev Container gives "Container is not in same network as caddy" #647

Closed carlgleisner closed 3 weeks ago

carlgleisner commented 1 month ago

Dear all 👋🏻

I'm trying to set up Dev Containers for a multitenant application with Caddy in front for local HTTPS and handling arbitrary subdomains.

I'd like to think that this would be a pretty appealing setup? 🤔 Haven't found any other reference projects or mentions, so I guess I'm totally off here. Still... would be sweet wouldn't it?

The Gist

Where I'm at is that I've successfully had caddy-docker-proxy doing a respond "Hello world" given devcontainer.localhost (defined in Hosts file) using labels from the Dev Container. However, trying to use caddy.reverse_proxy: "{{upstreams 4000}}" gives me a Container is not in same network as caddy message in the caddy-docker-proxy container.

The Setup

The Docker-Caddy-Proxy docker-compose.yml.

Please note that the network name used here is devcontainer_network.

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

networks:
  devcontainer_network:
    external: true

volumes:
  caddy_data: {}

The devcontainer_network is created by the Dev Container docker-compose.yml using the default network name:

services:
  devcontainer:
    build:
      context: .
      dockerfile: Dockerfile
    command: sleep infinity
    environment:
      - SSH_AUTH_SOCK="${SSH_AUTH_SOCK}"
    volumes:
      - ../..:/workspaces:cached
      - ${SSH_AUTH_SOCK}:${SSH_AUTH_SOCK}
    network_mode: service:db
    labels:
      caddy: devcontainer.localhost
      caddy.reverse_proxy: "{{upstreams 4000}}"
      #caddy.respond: / `Hello\nWorld` 200

  db:
    image: postgres:14
    restart: unless-stopped
    volumes:
      - postgres-data:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: postgres
      POSTGRES_USER: postgres
      POSTGRES_DB: postgres

volumes:
  postgres-data:

networks:
  default:
    name: devcontainer_network

Attempting a curl -v https://devcontainer.localhost from my host gives me a "Container is not in same network as caddy".

caddy-1  | {"level":"warn","ts":1722239905.152906,"logger":"docker-proxy","msg":"Container is not in same network as caddy","container":"0d29118c6d45032e479010e7647c9430f00f4bc2499364223d77dacad4957862","container id":"0d29118c6d45032e479010e7647c9430f00f4bc2499364223d77dacad4957862"}

Please note that the way Dev Containers are set up according to the documentation is using network_mode: service:db. I have tried creating networks such as caddy with docker network create caddy but haven't gotten that working with the communication between the devcontainer service and the db service above. I get the sense that network_mode: service:db is the supported way of doing networking with Dev Containers and have tried to stay with that.

Also please note that I've tried shutting everything down and doing a docker system prune -a before starting up again, but to no avail.

If anyone is interested in supporting this effort I'd be most grateful. I don't expect anyone to help out, but if there's interest, perhaps there's something of value to the community 🙂

bjeanes commented 1 month ago

I'm not in a dev container, but am using network_mode: service:foo on the container I am targeting and indeed I get this same error.

lucaslorentz commented 4 weeks ago

Could you please run docker network ls and verify that you have a network named devcontainer_network ? Docker compose usually adds prefix to networks, so I think it's likely that this is not really the name of your network. Try giving it a name as:

networks:
  devcontainer_network:
    name: devcontainer_network
    external: true
carlgleisner commented 3 weeks ago

Thanks for commenting @lucaslorentz 🙏🏻

I won't be able to test this anytime soon due to limited bandwidth, but I very much want to. I'll close the issue with this comment so that it's not left open.