Unable to connect to mongodb running in a docker container using Compass

I am running an empty mongodb instance in a docker desktop container using: docker run --name mongodb -d -p 64000:64000 mongo

Am running Compass v1.30.1 and attempting to connect to the mongodb running on localhost using the connection string: mongodb://localhost:64000

In the Compass UI, I receive the following visible error: connection <monitor> to 127.0.0.1:64000 closed

I have looked at the Compass log file and can see these entries:

{"t":{"$date":"2022-03-23T01:04:58.599Z"},"s":"I","c":"COMPASS-CONNECT-UI","id":1001000004,"ctx":"Connection UI","msg":"Initiating connection attempt"}
{"t":{"$date":"2022-03-23T01:04:58.602Z"},"s":"I","c":"COMPASS-DATA-SERVICE","id":1001000014,"ctx":"Connection 2","msg":"Connecting","attr":{"url":"mongodb://localhost:64000/?readPreference=primary&appname=MongoDB+Compass&directConnection=true&ssl=false"}}
{"t":{"$date":"2022-03-23T01:04:58.616Z"},"s":"I","c":"COMPASS-CONNECT","id":1001000010,"ctx":"Connect","msg":"Resolved SRV record","attr":{"from":"mongodb://localhost:64000/?readPreference=primary&appname=MongoDB+Compass&directConnection=true&ssl=false","to":"mongodb://localhost:64000/?readPreference=primary&appname=MongoDB+Compass&directConnection=true&ssl=false"}}
{"t":{"$date":"2022-03-23T01:04:58.618Z"},"s":"I","c":"COMPASS-CONNECT","id":1001000009,"ctx":"Connect","msg":"Initiating connection","attr":{"url":"mongodb://localhost:64000/?readPreference=primary&appname=MongoDB+Compass&directConnection=true&ssl=false","options":*{"monitorCommands":true}*}}

Also disabled my firewall temporarily, with no effect.

Any help appreciated. Thanks in advance.

Hello @kidcoconut
Welcome to the community!!

By default the MongoDB runs on port 27017, if you wish to change the port for your machine, you can use the command as:

docker run --name -mongodb -d -p 64000:27017 mongo 
docker exec -it mongodb

and login to the shell.
For Compass, the following URL would work:

mongodb://localhost:64000

Please let us know, if you are able to connect in the above mentioned way or if more information is needed from our end.

Thanks
Aasawari

4 Likes

Hi @Aasawari ,
Thankyou for the warm welcome.

Some updates since my last note. In summary … I followed your tips with no success, then performed more analysis and troubleshooting. Things are working now, but I am left to think something was wrong with my Compass install and/or node_module libraries and dependencies.

For simplicity, I recreated a new docker container for mongodb using 27018:27017. There was no change re ability to connect through Compass. Same connection-monitor-closed error and log.

I then went into Compass and inspected the Dev Tools (toggle) and attempted to run some of the troubleshooting/test scripts. Loading the script did not work as there were errors/warnings re missing libraries under node_modules. I then shut everything down (docker/compass) and explicitly installed nvm and node (latest). I reinstalled Compass (win_msi), recreated the docker container for mongodb@27018:27017, and launched Compass. Success.

I’ll keep an eye on it and report back if I find anything else.

Pls note for the benefit of others reading this, your sample commands need some adjustment to make them run properly (typos, missing flags, etc).

Thanks again.

2 Likes

Hi Maam,
I have some kind of similar issue. Mongodb can be accessible from the container (with the command docker exec -it mongodb mongosh"). I can see dbs with the command “show dbs”, but the mongodb is not accessible from MongoDB Compass. The error it throwing is “timedout”. I have used container IP and 27017 port. What I could try to resolve the issue…
Thanks you in advance…

  • Gowtham