无法连接到在 docker 容器中运行的 mongoDB

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25828021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:09:54  来源:igfitidea点击:

Unable to connect to mongoDB running in docker container

mongodbdocker

提问by Chris G.

Following this example: https://docs.docker.com/engine/examples/mongodb/

按照这个例子:https: //docs.docker.com/engine/examples/mongodb/

When trying to connect to mongoDB with: mongo ip:27017(where ip is the name from boot2docker ip) + the port number from docker ps:

当尝试使用以下命令连接到 mongoDB 时:(mongo ip:27017其中 ip 是 boot2docker ip 中的名称)+ docker ps 中的端口号:

27017/tcp
or with -P
0.0.0.0:49155->27017/tcp

27017/tcp
或 -P
0.0.0.0:49155->27017/tcp

Either way I get the following errors:

无论哪种方式,我都会收到以下错误:

warning: Failed to connect to ip:27017, reason: errno:61 Connection refused

Error: couldn't connect to server ip:27017 (ip), connection attempt failed at src/mongo/shell/mongo.js:148 exception: connect failed

警告:无法连接到 ip:27017,原因:errno:61 连接被拒绝

错误:无法连接到服务器 ip:27017 (ip),在 src/mongo/shell/mongo.js:148 处连接尝试失败异常:连接失败

采纳答案by Chris G.

I was using port 27017 instead of 49155 (doh, port forwarding)

我使用的是端口 27017 而不是 49155(doh,端口转发)

0.0.0.0:49155->27017/tcp

0.0.0.0:49155->27017/tcp

Thanks to ZeissS

感谢蔡司

回答by falsecrypt

If you specified the correct port and still not able to connect to mongodb running in docker (like me), make sure you are using the service name (or container name) in your connection URL, e.g. mongodb://mongodb_service:27017/mydb, which is defined in your docker-compose.yml:

如果您指定了正确的端口,但仍然无法连接到在 docker 中运行的 mongodb(像我一样),请确保您在连接 URL 中使用了服务名称(或容器名称),例如mongodb://mongodb_service:27017/mydb,在您的docker-compose.yml:

services:
  mongodb_service:
    image: mongo

I was using the hostnamevalue and that's not the correct thing to do. You could verify this by looking at docker inspect mongodb_servicein the Aliasessection.

我正在使用该hostname值,但这不是正确的做法。你可以通过查看验证这一点docker inspect mongodb_serviceAliases部分。

回答by VMTrooper

If you are on a Mac and using Docker Machine, do the following:

如果您使用的是 Mac 并使用 Docker Machine,请执行以下操作:

1. Get the name of the VM running docker daemon
$ docker-machine ls

2. Get the VM's IP info
$ docker-machine env 

3. Connect with the mongo client to the VM IP and the mongo mapped port
$ mongo VM-IP:port