Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
carlgleisner opened this issue Jul 29, 2024 · 3 comments

Comments

@carlgleisner
Copy link

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
Copy link

bjeanes commented Jul 31, 2024

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
Copy link
Owner

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
Copy link
Author

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.

@carlgleisner carlgleisner closed this as not planned Won't fix, can't repro, duplicate, stale Aug 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants