简体   繁体   中英

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?

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.

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

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

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:
我的终端运行 docker

Mongodb Compass:
MongoDB指南针

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.

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

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.

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 .

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.

在此处输入图像描述

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM