connect robomongo to mongoDB docker container

2020-05-20 09:38发布

问题:

I'm running a NodeJS App with docker-compose. Everything works fine and I can see all my data by connecting to Mongo inside container. But when I connect to RoboMongo I don't see any data.

How can I deal with this problem?

回答1:

You should do a Robomongo SSH tunnel connection to MongoDB inside docker container. First of all you should install a ssh server inside your docker container.

https://docs.docker.com/engine/examples/running_ssh_service/


After that you should configure your connection in Robomongo. Inside "Connection Settings" there are configuration tabs of your Robomongo Connection.

Go to "SSH" Tab and configure your SSH connection to the docker container. After that go to "Connection" Tab and configure your connection to MongoDB as if it was in localhost scope.



回答2:

There is another way. You can

  1. SSH with Robomongo into your actual virtual server that hosts your docker applications (SSH tab, check "Use SSH tunnel" and complete the other fields accordingly)
  2. Now ssh into the same machine in your terminal.
  3. docker ps should show you your MongoDB container.
  4. docker inspect <mongo container id> will print out complete information about that container. Look for IPAddress in the end, that will give you the local IP of the container.
  5. In the "Connection" tab in Robomongo use that container IP to connect.

Another sidenote: Make sure that you don't expose your mongodb service ports in any way (neither Dockerfile nor docker-compose.yml), cause that will make your database openly accessible from everywhere. Assuming that you don't have set up a username / password for that service you will be scanned and hacked soon.



回答3:

The easiest way is to enable forwarding the Mongo Container itself, here's how my docker-compose looks like.

mongo:
  image: mongo
  restart: always
  ports:
    - 27017:27017


回答4:

Please note that maybe you won't be able to use ssh because it was just a problem of incompatibility between mongo and robomongo.

'Robomongo v8.5 and lower doesn't support MongoDB 3'. It has nothing to do with docker.



回答5:

In your docker-compose file, you can expose a port to the host.

For example, the following code will expose port 27017 inside the machine to the port 27018 in the host.

app:
  image: node
  volumes:
    - /app
  ports:
    - "27018:27017"

Then, if you have docker-machine installed and your machine is default, you can do in a terminal :

docker-machine ip default

It will give you the ip of your host, for example 192.168.2.3. The address of your database (host) will be 192.168.2.3 and the port 27018.

If your docker machine is not virtual and is your OS, the address of your database will be localhost and the port 27018.