如何连接到在 Docker 容器中运行的 MongoDB?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37644465/
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
How to connect to MongoDB running in Docker container?
提问by MaVe
I'm running a Docker container on MacOS that starts a Postgres server and MongoDB server as follows:
我在 MacOS 上运行一个 Docker 容器,它启动 Postgres 服务器和 MongoDB 服务器,如下所示:
docker run -p 80:80 -p 27017:27017 dockertest
* Starting PostgreSQL 9.3 database server
...done.
* Starting database mongodb
...done.
The output shows that the MongoDB server is started correctly. Then I lookup the IP of the VM in which the container is running, like this:
输出显示 MongoDB 服务器已正确启动。然后我查找运行容器的 VM 的 IP,如下所示:
docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default * virtualbox Running tcp://192.168.99.100:2376 v1.11.2
The ports seem to be mapped:
端口似乎已映射:
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
39e5198e4693 dockertest "/bin/sh -c 'service " 3 minutes ago Up 3 minutes 0.0.0.0:80->80/tcp, 0.0.0.0:27017->27017/tcp drunk_nobel
However, when I want to connect to the MongoDB in another terminal window, using
但是,当我想在另一个终端窗口中连接到 MongoDB 时,使用
mongo 192.168.99.100:27017
I get the following error:
我收到以下错误:
MongoDB shell version: 2.6.4
connecting to: 192.168.99.100:27017/test
2016-06-05T18:48:19.050+0200 warning: Failed to connect to 192.168.99.100:27017, reason: errno:61 Connection refused
2016-06-05T18:48:19.052+0200 Error: couldn't connect to server 192.168.99.100:27017 (192.168.99.100), connection attempt failed at src/mongo/shell/mongo.js:148
exception: connect failed
I already had a look at similar questions (e.g., Unable to connect to mongoDB running in docker container) but the proposed solution does not seem to work.
我已经看过类似的问题(例如,无法连接到在 docker 容器中运行的 mongoDB),但建议的解决方案似乎不起作用。
采纳答案by MaVe
The problem was not related to Docker, but to the bindip in the configuration of MongoDB. See e.g., mongodb.conf bind_ip = 127.0.0.1 does not work but 0.0.0.0 works
问题与Docker无关,而是与MongoDB配置中的bindip有关。参见例如mongodb.conf bind_ip = 127.0.0.1 不起作用但 0.0.0.0 起作用