简体   繁体   English

无法连接到在本地Docker容器中运行的MongoDB

[英]Cannot connect to MongoDB running in local Docker container

I have this command to start MongoDB in a container: 我有以下命令在容器中启动MongoDB:

docker run -p 27017:27017 --name cdt-mongo mongo

this is from these docs: 这是从这些文档:

https://hub.docker.com/_/mongo/ https://hub.docker.com/_/mongo/

I actually think the 我实际上认为

-p 27017:27017

is superfluous. 是多余的。 anyway. 无论如何。

Locally, I run the container in one terminal, and then in another terminal I issue the mongo command, and I get: 在本地,我在一个终端上运行容器,然后在另一终端上执行mongo命令,得到:

$ mongo
MongoDB shell version v3.4.2
connecting to: mongodb://127.0.0.1:27017
2017-05-02T11:27:22.864-0700 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2017-05-02T11:27:22.866-0700 E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:237:13
@(connect):1:6
exception: connect failed

anybody know why I cannot connect to mongodb running in the container? 有人知道为什么我无法连接到在容器中运行的mongodb吗?

because I am on MacOS, I should not connect to 因为我在MacOS上,所以我不应该连接到

localhost:27017

but instead use the host/ip address of the docker-machine VM 但改为使用docker-machine VM的主机/ IP地址

HOST=$(docker-machine ip)

and then connect to it like so: 然后像这样连接到它:

mongo --host $HOST

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

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