44

I have a mongodb database running on the default port 27017 in a docker container.

Is there a way to connect to the database with the mongodb compass GUI running natively on my ubuntu OS?

samsung gather
  • 583
  • 1
  • 4
  • 8

12 Answers12

37

docker run -p 27018:27017 and then connect from Compass on your host with port 27018. I don't see a reason to expose all ports.

barakbd
  • 988
  • 10
  • 16
  • 6
    To avoid confusion, exact command is(on Mac OS): `docker run --name -p 27018:27017 -d mongo:` this command does download docker image and deploy/start container – Ram Jun 01 '21 at 21:13
  • 1
    `27107:27107` should work too. Why change the host port? – OneCricketeer Jul 25 '22 at 22:42
  • 1
    Worked for me perfectly. I ran `docker run --name -p 27017:27017 -d mongo:` (on Mac OS). I was then able to use `mongodb://localhost:27017/` as my Connection String in MongoDB Compass – Oscar Jan 08 '23 at 18:28
16

Replace localhost with your IP address in the connection string, eg, my IP address is 10.1.2.123 then I have mongodb://10.1.2.123:27017?readPreference=primary&appname=MongoDB%20Compass&ssl=false.

Saw this here: https://nickjanetakis.com/blog/docker-tip-35-connect-to-a-database-running-on-your-docker-host

Rochadsouza
  • 888
  • 10
  • 10
15

With docker-compose you just have to expose the port 27017. When You hit "Connect" in the GUI it will auto-detect this connection.

version: "3"
services:
  mongo-database:
    container_name: mongo-database
    image: mongo:4
    ports:
      - 27017:27017
Florian Ludewig
  • 4,338
  • 11
  • 71
  • 137
7

Yes we can run

Steps:

  1. Pull/Restart the docker container mongodb

  2. Enter the bash shell

    docker exec -it mongodb bash
    
  3. Now open the mongodb compass community and with same default connection just click connect and the docker container's mongodb will be connected to compass community.

My terminal running docker:
My terminal running docker

Mongodb Compass:
Mongodb Compass

Giulio Caccin
  • 2,962
  • 6
  • 36
  • 57
vibi2605
  • 79
  • 1
  • 2
  • 3
    I don't understand your answer. Do I need to first open the bash shell before connecting to mongo DB using compass? Can't we do it without it? – Muhammad Tariq Jan 03 '21 at 12:13
6

Use docker inspect or docker desktop to inspect and find the exposing port

docker inspect your_container_name

and find this section

       "Ports": {
            "27017/tcp": [
                {
                    "HostIp": "0.0.0.0",
                    "HostPort": "27012"
                }
            ]
        },

and then connect using this url string

mongodb://localhost:27012/?readPreference=primary&appname=MongoDB%20Compass&ssl=false

Do not pass in replica set name if you are using one otherwise connection will fail. This is if you have deployed a replica set instead of turning your standalone to a replica set.

Leave a comment if you don't know how to deploy a replica set and I can leave a docker-compose file to set up and deploy replica set.

Alish Giri
  • 1,855
  • 18
  • 21
  • But why did you map host 27012 to container port 27017 in the first place? – OneCricketeer Jul 25 '22 at 22:45
  • I tried the other answers, but based on the replies of the first question this worked for me. I'm using Windows 11, and using docker inspect worked for me. Connected using 0.0.0.0:27107 – Vicente Matus Dec 09 '22 at 21:44
2

I could connect the compass on windows to a docker using these tags at the end:

mongodb://user:password@localhost:27017/dbname?authSource=dbname&readPreference=primary&gssapiServiceName=mongodb&appname=MongoDB%20Compass&ssl=false

JorgeMadson
  • 140
  • 10
1

Just open compass and inside connect add the credentials if you have used envs like

ME_CONFIG_MONGODB_ADMINUSERNAME=admin

and hit connect.No addition settings required. Or you can use mongo-express which a web based UI tool for monodb.

Rishav Sinha
  • 331
  • 1
  • 5
  • 15
1

Run command sudo docker ps it will show docker containers you have where you can find the port number of mongodb the run the command sudo mongodb-compass it will open the mongodb compass

If you are connecting locally so general hostname is : localhost and then just put the port number and click on connect.

1

I was also having trouble connecting to my local MongoDB using Compass, but discovered it was an SSL problem. By default, Compass sets SSL to "System CA". However, if you try that with your dockerized Mongo, your Mongo logs will show you this error:

Error receiving request from client: SSLHandshakeFailed: SSL handshake received but server is started without SSL support. Ending connection from 172.17.0.1:45902 (connection id: 12)
end connection 172.17.0.1:45902 (0 connections now open)

Therefore, to connect, I had to click "Fill in connection fields individually" then set the SSL field to "None". For reference, I ran Mongo using this: docker run -p 27017:27017 --name some-mongo mongo:4.0. No authentication necessary.

enter image description here

JP Lew
  • 4,121
  • 2
  • 32
  • 45
1

This solution worked for me.

Run the docker container using:

docker run -d --name mongo-db -v ~/mongo/data:/data/db -p 27017:27017 mongo

-v is for mapping the local volume to the docker writable space. This will keep the data even when the container is destroyed.

MongoDB connection string Compass GUI:

mongodb://localhost:27017

  • This doesn't really add anything to the other answers beyond saying how to persist the database, which wasn't part of the question – OneCricketeer Jul 25 '22 at 22:46
  • It looks like a complete answer to me. I came across similar issues while connecting to Mongo and then with persistence. This covers everything that one could possibly need to get going. – Yogesh Chaudhari May 28 '23 at 00:21
  • Question was only asking about connection, not persistence – OneCricketeer May 28 '23 at 03:36
0

Run your mongo container with 'publish-all-ports' option (docker run -P). Then you should be able to inspect the port exposed to the host via docker ps -a and connect to it from Compass (just use your Hostname: localhost and Port: <exposed port>).

parannoyd
  • 111
  • 2
0

Use the --net=host option for Docker container shares its network namespace with the host machine.

docker run -it --net=host -v mongo_volume:/data/db --name mongo_example4 -d mongo

So now we can connect the mongodb with compass using mongodb://localhost:27017

Other hand to connect, simply get the docker container IPAddress using the docker inspect command and use that ip address instead of localhost

mongodb://172.17.0.2:27017