Connect to MongoDB docker container from Local PC

I have a MeteorJs application that I have deployed to Digital Ocean using MeteorUp. How can I connect to the MongoDB database using a MongoDB client from my local windows machine? I tried using MyDigitalOceanIP: 27017 but I am getting a “Connection failed” error.

Error:
Connection failed.
SERVER [MyDigitalOceanIP:27017] (Type: UNKNOWN) CONNECTING
Details:
Timed out after 5000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=MyDigitalOceanIP:27017, type=UNKNOWN, state=CONNECTING}]

docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
91502fe2c115 mup-todousingreactandmeteorjs:latest “/bin/sh -c 'bash $M…” 12 minutes ago Up 12 minutes 0.0.0.0:80->80/tcp ToDoUsingReactAndMeteorJs
2d4be51b46a2 mongo:3.4.1 “/entrypoint.sh mong…” 21 minutes ago Up 21 minutes 127.0.0.1:27017->27017/tcp mongodb

For good reason, the default setting for MUP is that the mongo container is only accessible from other containers on the same server, not from the public internet.

This means that to connect to mongo inside it’s container you need to connect over SSH and docker:
http://meteor-up.com/docs.html#accessing-the-database

Now if you want to use a GUI client, you might be able to connect over a SSH tunnel with port forwarding. From a quick test, it looks like the mongo container is contactable from the mup server, so if you forward port 27017 to your local machine it should work

EDIT: The ssh command for port forwarding looks like this:

ssh <username>@<hostname> -L 27017:localhost:27017

EDIT2: Just tried this on my local machine to a MUP server and was able to connect using MongoDB Compass

1 Like