samsung gather
samsung gather

Reputation: 653

connecting to Mongodb inside a docker with mongodb compass GUI

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?

Upvotes: 51

Views: 84324

Answers (14)

Avinash Khadsan
Avinash Khadsan

Reputation: 497

Here are the step.
1. start mongo db on docker container
docker run -d ^
-p 27018:27017 ^
-e MONGO_INITDB_ROOT_USERNAME=root ^
-e MONGO_INITDB_ROOT_PASSWORD=root ^
--name mongodb ^
-v mongodb-data:/data/swiggy-db ^
--net swiggy-nw ^
mongo
  1. Install Mongo Compose on local machine.
  2. use below URL in Mongo Compass enter image description here

enter image description here

Upvotes: 2

Tiago Peres
Tiago Peres

Reputation: 15421

In my case if I did as Massimiliano_Marcon

docker run --name mongodb -d -p 27017:27017 mongo

then was able to connect just fine with the string mongodb://localhost:27017

enter image description here

But then, if I tried to connect to a container that started with Testcontainers, only worked if I've used mongodb://localhost:27017?directConnection=true

enter image description here

Upvotes: 8

Arunodhayam
Arunodhayam

Reputation: 1

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

Upvotes: 0

Yogeshwar Chaudhari
Yogeshwar Chaudhari

Reputation: 503

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

Upvotes: 0

JP Lew
JP Lew

Reputation: 4439

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

Upvotes: 1

Py_Patience
Py_Patience

Reputation: 41

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.

Upvotes: 1

Alish Giri
Alish Giri

Reputation: 2238

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.

Upvotes: 8

Rishav Sinha
Rishav Sinha

Reputation: 359

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.

Upvotes: 0

Rochadsouza
Rochadsouza

Reputation: 978

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

Upvotes: 21

JorgeMadson
JorgeMadson

Reputation: 158

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

Upvotes: 2

Florian Ludewig
Florian Ludewig

Reputation: 5962

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

Upvotes: 21

vibi2605
vibi2605

Reputation: 77

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

Upvotes: 6

barakbd
barakbd

Reputation: 1102

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.

Upvotes: 42

parannoyd
parannoyd

Reputation: 109

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>).

Upvotes: -1

Related Questions