Can you help me connect to MongoDB running with docker compose in Github Codespaces (from compass)

Hi there. My objective is to run our application in Github codespaces. This works great, but I can’t connect to the Mongodb instance, from my local machine with for example MongoDB Compass.

docker-compose.yml

version: '3.8'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        # Update 'VARIANT' to pick an LTS version of Node.js: 16, 14, 12.
        # Append -bullseye or -buster to pin to an OS version.
        # Use -bullseye variants on local arm64/Apple Silicon.
        VARIANT: '16'
    volumes:
      - ..:/workspace:cached
    init: true
    environment:
      EDDY_CLI_KEY_PATH: /workspace

    # Overrides default command so things don't shut down after the process ends.
    command: sleep infinity

    # Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
    network_mode: service:db
    # Uncomment the next line to use a non-root user for all processes.
    # user: node

    # Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
    # (Adding the "ports" property to this file will not forward from a Codespace.)

  # mongosetup:
  #   image: mongo:latest
  #   depends_on:
  #     - db
  #   restart: 'no'
  #   entrypoint: ['bash', '-c', "sleep 10 && mongo --host localhost:27017 --eval 'rs.initiate()'"]
  db:
    # Based on https://stackoverflow.com/q/67020325/3694288
    image: mongo:5
    restart: unless-stopped
    # deploy:
    #   replicas: 1
    volumes:
      - mongodb-data:/data/db
      - ./mongo-scripts:/docker-entrypoint-initdb.d/
      - type: bind
        source: ./mongod.conf
        target: /etc/mongod.conf
    # entrypoint: ['mongod', '--config', '/etc/mongod.conf']
    # command: ['--replSet', 'rs0', '--bind_ip_all']
    command: '--bind_ip_all --replSet rs0'
    # Uncomment to change startup options
    # environment:
    #   - PUID=1000
    #   - PGID=1000
    #   - MONGO_INITDB_ROOT_USERNAME=mongo
    #   - MONGO_INITDB_ROOT_PASSWORD=mongo
    #   - MONGO_INITDB_DATABASE=my-service
    #   - MONGO_REPLICA_SET_NAME=rs0
    ports:
      - 27017:27017
    # healthcheck:
    #   test: test $$(echo "rs.initiate().ok || rs.status().ok" | mongo -u $${MONGO_INITDB_ROOT_USERNAME} -p $${MONGO_INITDB_ROOT_PASSWORD} --quiet) -eq 1
    #   interval: 10s
    #   start_period: 30s
    # Add "forwardPorts": ["27017"] to **devcontainer.json** to forward MongoDB locally.
    # (Adding the "ports" property to this file will not forward from a Codespace.)

volumes:
  mongodb-data: null

devcontainer.json

// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.203.0/containers/javascript-node-mongo
// Update the VARIANT arg in docker-compose.yml to pick a Node.js version
{
    "name": "Node.js & Mongo DB",
    "dockerComposeFile": "docker-compose.yml",
    "service": "app",
    "workspaceFolder": "/workspace",

    // Set *default* container specific settings.json values on container create.
    "settings": {},

    // Add the IDs of extensions you want installed when the container is created.
    "extensions": [
        "vscode-icons-team.vscode-icons",
        "esbenp.prettier-vscode",
        "hediet.vscode-drawio",
        "bradlc.vscode-tailwindcss",
        "svelte.svelte-vscode",
        "dbaeumer.vscode-eslint"
    ],

    // Use 'forwardPorts' to make a list of ports inside the container available locally.
    "forwardPorts": [3000, 27017],

    // Use 'postCreateCommand' to run commands after the container is created.
    "postCreateCommand": "npm ci && npm run build:cli && npm link",
    "portsAttributes": {
        "3000": {
            "label": "Svelte (front-end)",
            "onAutoForward": "notify",
            "protocol": "http"
        }
    },

    // Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
    "remoteUser": "node",
    "features": {
		"git": "os-provided"
    }
}

The app itself can connect to the Mongodb instance, without issues. Port forwarding is made public like here:

However when I try to connect from MongoDB Compass running locally with:

mongodb://<code-space-name>-4q75x555hj7rv-27017.githubpreview.dev:80

I always get:

or

Would be so thankful for any points in the right direction :pray:

1 Like

Finally a good answer on SO - helping me how to connect with ngrok.