简体   繁体   English

将 robomongo 连接到 mongoDB docker 容器

[英]connect robomongo to mongoDB docker container

I'm running a NodeJS App with docker-compose .我正在使用docker-compose运行NodeJS应用程序。 Everything works fine and I can see all my data by connecting to Mongo inside container.一切正常,我可以通过连接到容器内的 Mongo 来查看我的所有数据。 But when I connect to RoboMongo I don't see any data.但是当我连接到RoboMongo 时,我看不到任何数据。

How can I deal with this problem?我该如何处理这个问题?

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)使用 Robomongo SSH 进入托管 docker 应用程序的实际虚拟服务器(SSH 选项卡,选中“使用 SSH 隧道”并相应地完成其他字段)
  2. Now ssh into the same machine in your terminal.现在 ssh 进入终端中的同一台机器。
  3. docker ps should show you your MongoDB container. docker ps应该会显示你的 MongoDB 容器。
  4. docker inspect <mongo container id> will print out complete information about that container. docker inspect <mongo container id>将打印出有关该容器的完整信息。 Look for IPAddress in the end, that will give you the local IP of the container.最后查找IPAddress ,它将为您提供容器的本地 IP。
  5. In the "Connection" tab in Robomongo use that container IP to connect.在 Robomongo 的“连接”选项卡中,使用该容器 IP 进行连接。

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.另一个旁注:确保您没有以任何方式公开您的 mongodb 服务端口(无论是 Dockerfile 还是 docker-compose.yml),因为这将使您的数据库可以从任何地方公开访问。 Assuming that you don't have set up a username / password for that service you will be scanned and hacked soon.假设您没有为该服务设置用户名/密码,您很快就会被扫描和入侵。

The easiest way is to enable forwarding the Mongo Container itself, here's how my docker-compose looks like.最简单的方法是启用转发 Mongo 容器本身,这是我的 docker-compose 的样子。

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

You should do a Robomongo SSH tunnel connection to MongoDB inside docker container.您应该在 docker 容器内建立到 MongoDB 的 Robomongo SSH 隧道连接。 First of all you should install a ssh server inside your docker container.首先,您应该在 docker 容器中安装一个 ssh 服务器。

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


After that you should configure your connection in Robomongo.之后,您应该在 Robomongo 中配置您的连接。 Inside "Connection Settings" there are configuration tabs of your Robomongo Connection.在“连接设置”中有 Robomongo 连接的配置选项卡。

Go to "SSH" Tab and configure your SSH connection to the docker container.转到“SSH”选项卡并配置与 docker 容器的 SSH 连接。 After that go to "Connection" Tab and configure your connection to MongoDB as if it was in localhost scope.之后转到“连接”选项卡并配置与 MongoDB 的连接,就好像它在 localhost 范围内一样。

I was facing a different problem.我面临着不同的问题。 I had installed MongoDB locally.我在本地安装了 MongoDB。 So, when the MongoDB on docker was running, it was clashing with the one running on my host.因此,当 docker 上的 MongoDB 运行时,它与我主机上运行的 MongoDB 发生冲突。 I had installed it using brew .我已经使用brew安装了它。

So, I ran所以,我跑了

brew services stop mongodb-community

and then I restarted Robo3t.然后我重新启动了 Robo3t。 I saw the databases created in the MongoDB running on the docker.我看到在 docker 上运行的 MongoDB 中创建的数据库。

Voila!瞧!

Please note that maybe you won't be able to use ssh because it was just a problem of incompatibility between mongo and robomongo.请注意,也许您将无法使用 ssh,因为这只是 mongo 和 robomongo 之间不兼容的问题。

'Robomongo v8.5 and lower doesn't support MongoDB 3'. 'Robomongo v8.5 及更低版本不支持 MongoDB 3'。 It has nothing to do with docker.它与 docker 无关。

First log in with ssh Login details首先使用ssh登录登录详细信息

  1. ssh -i yourpemfile.pem username@ipaddress ssh -i yourpemfile.pem 用户名@ipaddress 在此处输入图片说明

  2. Check running container id for MongoDB检查 MongoDB 的运行容器 ID

    docker ps -a docker ps -a

  3. then check the mongo container id然后检查mongo容器ID

docker inspect container_id码头工人检查容器_id

在此处输入图片说明

  1. Then open robo3t然后打开robo3t

create new connection and add container id use ssh login details to connect to mongodb创建新连接并添加容器 id 使用 ssh 登录详细信息连接到 mongodb

在此处输入图片说明 在此处输入图片说明

In your docker-compose file, you can expose a port to the host.在您的 docker-compose 文件中,您可以向主机公开一个端口

For example, the following code will expose port 27017 inside the machine to the port 27018 in the host.例如,下面的代码会将机器内部的 27017 端口暴露给主机中的 27018 端口。

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并且您的机器是default ,您可以在终端中执行以下操作:

docker-machine ip default

It will give you the ip of your host, for example 192.168.2.3 .它将为您提供主机的 IP,例如192.168.2.3 The address of your database (host) will be 192.168.2.3 and the port 27018 .您的数据库(主机)的地址将是192.168.2.3和端口27018

If your docker machine is not virtual and is your OS, the address of your database will be localhost and the port 27018 .如果您的 docker 机器不是虚拟机而是您的操作系统,那么您的数据库地址将是localhost和端口27018

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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