Search code examples
mongodbdockerdocker-composeipmongodb-compass

Connecting to Mongo Docker container from Mongo Compass on local machine?


I have the following docker compose file that I use to spin up a mongo docker container which works as intended.

However, I cannot seem to connect to the container from compass on my local machine.

I executed docker inspect on my mongo container and got its IP and tried to connect using compass, but it didn't work - connection timed out.

Is it the IP of my docker network or the IP of the mongo container I need?

docker compose file:

version: "3"
services:
  pokerStats:
    image: pokerStats
    container_name: pokerStats
    ports:
      - 8080:8080
    depends_on: 
      - db
  db:
    image: mongo
    container_name: mongo
    volumes:
      - ./database:/data
    ports:
      - "27017:27017"

Solution

  • I was able to get this working using the top answer on this question.

    I had to change my docker-compose file to expose port 27018 on my local:

      db:
        image: mongo
        container_name: mongo
        volumes:
          - ./database:/data
        ports:
          - "27018:27017"
    

    And my connection details on Compass to be:

    enter image description here