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.


version: '3.8'

      context: .
      dockerfile: Dockerfile
        # 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'
      - ..:/workspace:cached
    init: true
      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()'"]
    # Based on
    image: mongo:5
    restart: unless-stopped
    # deploy:
    #   replicas: 1
      - 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_DATABASE=my-service
      - 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.)

  mongodb-data: null


// For format details, see For config options, see the README at:
// 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": [

    // 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:
    "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:


I always get:


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.