如何连接到Docker容器中运行的MongoDB?

时间:2016-06-05 16:55:29

标签: mongodb docker connection

我在MacOS上运行Docker容器,启动Postgres服务器和MongoDB服务器,如下所示:

docker run -p 80:80 -p 27017:27017 dockertest
 * Starting PostgreSQL 9.3 database server
  ...done.
 * Starting database mongodb
  ...done.

输出显示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

端口似乎已映射:

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

但是,当我想在另一个终端窗口中使用

连接到MongoDB时
mongo 192.168.99.100:27017

我收到以下错误:

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

我已经看过类似的问题(例如,Unable to connect to mongoDB running in docker container),但建议的解决方案似乎不起作用。

1 个答案:

答案 0 :(得分:7)

问题与Docker无关,而是与MongoDB配置中的bindip有关。例如,参见mongodb.conf bind_ip = 127.0.0.1 does not work but 0.0.0.0 works