简体   繁体   English

将 Robo 3T 连接到 Docker MongoDB 容器

[英]Connecting Robo 3T to Docker MongoDB container

As part as an attempt to resolve a Hibernate OGM connection issue, I want to see if I connect from Robo 3T.作为尝试解决 Hibernate OGM 连接问题的一部分,我想看看我是否从 Robo 3T 连接。

I build my MongoDB image and start it running.我构建了我的 MongoDB 映像并开始运行。

docker ps:码头工人:

MacBook-Pro:GoStopHandle NOTiFY$ docker ps CONTAINER ID IMAGE MacBook-Pro:GoStopHandle NOTiFY$ docker ps 容器 ID 图像
COMMAND CREATED STATUS PORTS NAMES 0375a68b9988 gostophandlemongodb:latest命令创建状态端口名称 0375a68b9988 gostophandlemongodb:latest
"docker-entrypoint.s…" 5 seconds ago Up 4 seconds "docker-entrypoint.s..." 5 秒前 Up 4 seconds
0.0.0.0:32844->27017/tcp goStopHandleMongo 0.0.0.0:32844->27017/tcp goStopHandleMongo

The IP address of the containers is:容器的IP地址是:

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 0375a68b9988 172.17.0.2码头检查 -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 0375a68b9988 172.17.0.2

I've enter the 'mongo' shell:我已经进入了“mongo”外壳:

docker exec -it goStopHandleMongo mongo admin docker exec -it goStopHandleMongo mongo admin

I add a user & password:我添加一个用户和密码:

> db.createUser(
...   {
...     user: "NOTiFY",
...     pwd: "MyPassword",
...     roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
...   }
... )
Successfully added user: {
    "user" : "NOTiFY",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        },
        "readWriteAnyDatabase"
    ]
}

I then create a connection in Robo 3T:然后我在 Robo 3T 中创建一个连接: Robo 3T - 创建连接

I then set-up the 'Authentication':然后我设置了“身份验证”:

验证

When I try & connect I get:当我尝试连接时,我得到:

连接对话框

Any suggestions?有什么建议?

It wasn't until I had to reboot my Mac that I realised that I'd still had a local instance of MongoDB running, therefore the I wasn't connecting to my Docker instance of MongoDB but my local (Brew installed) instance.直到我不得不重新启动我的 Mac 时,我才意识到我仍然有一个 MongoDB 的本地实例在运行,因此我没有连接到我的 MongoDB 的 Docker 实例,而是我的本地(安装了 Brew)实例。

Once I'd seen @Artjom Zabelin answer here and "Don't forget to map port to host port"一旦我看到@Artjom Zabelin 在这里回答并“不要忘记将端口映射到主机端口”

docker run --name some-mongo -p 27017:27017 -d mongo

It connected to MongoDB running my container.它连接到运行我的容器的 MongoDB。

@Frank Yucheng Gu was correct that I needed 'localhost'. @Frank Yucheng Gu 是正确的,我需要“本地主机”。

To connect, you should simply connect with localhost:27017 on Robo3T.要连接,您只需在 Robo3T 上连接localhost:27017

The problem originates in the confusion between a container's internal IP and its external IP.该问题源于容器的内部 IP 与其外部 IP 之间的混淆。 The internal IP is your container's address in the docker network, which is isolated from you host network (unless explicitly bridged).内部 IP 是您的容器在 docker 网络中的地址,它与您的主机网络隔离(除非明确桥接)。 When you do the docker inspect command, you grabbed the container's IP on the docker network.当您执行docker inspect命令时,您在 docker 网络上获取了容器的 IP。

Your container is listening on 0.0.0.0:32844 in the docker network, and exposed to the host with a port mapping to port 27017. So if you have another container in the docker network , you should access your service with 172.17.0.2:32844 .您的容器正在侦听 docker 网络中的0.0.0.0:32844 ,并通过映射到端口 27017 的端口暴露给主机。因此,如果docker 网络中有另一个容器,您应该使用172.17.0.2:32844访问您的服务. But if you have anything outside of the docker network, you should access your mongodb with either localhost:27017 or [your host IP]:27017 .但是,如果您在 docker 网络之外有任何东西,您应该使用localhost:27017[your host IP]:27017访问您的 mongodb。

Hope this helps!希望这可以帮助!

In my case, I was running docker on Mac OSX and docker was using my host IP address, and as a result docker inspect CONTAINER_ID --format '{{ .NetworkSettings.IPAddress }}' would give me an empty string.就我而言,我在 Mac OSX 上运行 docker 并且 docker 使用我的主机 IP 地址,因此docker inspect CONTAINER_ID --format '{{ .NetworkSettings.IPAddress }}'会给我一个空字符串。 Docker host address on Mac OSX and Windows is host.docker.internal . Mac OSX 和 Windows 上的 Docker 主机地址是host.docker.internal So I was able to connect to the mongodb instance running in docker after setting the host address on robo 3t to host.docker.internal因此,在将host.docker.internal 3t 上的主机地址设置为host.docker.internal后,我能够连接到在 docker 中运行的 mongodb 实例

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

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