如何连接到在Docker容器中运行的MongoDB? [英] How to connect to MongoDB running in Docker container?

查看:726
本文介绍了如何连接到在Docker容器中运行的MongoDB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在MacOS上运行一个Docker容器,该容器将按以下方式启动Postgres服务器和MongoDB服务器:

I'm running a Docker container on MacOS that starts a Postgres server and MongoDB server as follows:

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

输出显示MongoDB服务器已正确启动。然后我查找运行容器的VM的IP,如下所示:

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:

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时,使用

However, when I want to connect to the MongoDB in another terminal window, using

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

我已经看过类似的问题(例如,无法连接到在Docker容器中运行的mongoDB a>),但是建议的解决方案似乎不起作用。

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不相关,但与bindip有关在MongoDB的配置中。参见例如 mongodb.conf bind_ip = 127.0.0.1不起作用,但0.0.0.0可以起作用

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆