java12399900
java12399900

Reputation: 1671

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"

Upvotes: 2

Views: 11046

Answers (2)

java12399900
java12399900

Reputation: 1671

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

Upvotes: 8

theBittor
theBittor

Reputation: 834

You are mapping port 27017 from your host machine to port 27017 on the container. So the following connection string should work mongodb://localhost:27017

Upvotes: 2

Related Questions