node.js 来自其他容器的 Docker mongo 图像“连接被拒绝”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34711642/
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
Docker mongo image 'Connection refused' from other container
提问by Piu130
I'm new to docker. I'm trying to create a MongoDB container and a NodeJS container. My file looks:
我是码头工人的新手。我正在尝试创建一个 MongoDB 容器和一个 NodeJS 容器。我的文件看起来:
version: '2'
services:
backend:
image: node:5.11-onbuild
ports:
- "3001:3001"
volumes:
- .:/code
working_dir: "/code"
links:
- mongodb
mongodb:
image: mongo:3.3
expose:
- 27017
It should run npm installand then node ..
But docker-compose upends up with [MongoError: connect ECONNREFUSED 127.0.0.1:27017]while the command node ..
I think this is because of the bind_ip = 127.0.0.1in the file /etc/mongod.conf. Is this right?
它应该运行npm install然后node .。但docker-compose up以[MongoError: connect ECONNREFUSED 127.0.0.1:27017]while 命令结束node .。我认为这是因为bind_ip = 127.0.0.1文件中的/etc/mongod.conf. 这是正确的吗?
I use boot2docker on a Win10 system.
我在 Win10 系统上使用 boot2docker。
How can I solve this problem so that node can connect to the MongoDB?
如何解决这个问题,以便节点可以连接到 MongoDB?
回答by Thomasleveil
In your backend app, connect to mongodb:27017instead of 127.0.0.1:27017. Where 'mongodb' is the name of your container within docker-compose.yml.
在您的后端应用程序中,连接到mongodb:27017而不是127.0.0.1:27017。其中 'mongodb' 是 docker-compose.yml 中容器的名称。
回答by Marcin
I recently encountered similar issue. I am running docker toolbox under win 10 and this is how it worked for me:
我最近遇到了类似的问题。我在 win 10 下运行 docker 工具箱,这对我来说是这样的:
1) I had to verify which URL my default docker machine is using. This can be checked by running docker-machine lscommand. It will list available machines:
1) 我必须验证我的默认 docker 机器正在使用哪个 URL。这可以通过运行docker-machine ls命令来检查。它将列出可用的机器:
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default * virtualbox Running tcp://192.168.99.100:1234 v17.06.0-ce
rancher-client - virtualbox Stopped Unknown
rancher-server - virtualbox Stopped Unknown
2) When running mongodb image specify the port mapping
2)运行mongodb镜像时指定端口映射
docker run -d -it -p 27017:27017 mongo
3) At that point the valid mongo url would look something like this
3) 那时有效的 mongo url 看起来像这样
var dbhost = 'mongodb://192.168.99.100:27017/test
where 192.168.99.100was the default machine URL from the point 1)
192.168.99.100从点 1 开始的默认机器 URL在哪里)
Hope it helps someone.
希望它可以帮助某人。
回答by thaJeztah
Most likely, yes. 127.0.0.1 points to localhost insidethe mongodb container, so is not accessible from outside the container. Binding to 0.0.0.0 will probably work.
最有可能的,是的。127.0.0.1 指向mongodb 容器内的localhost ,因此无法从容器外部访问。绑定到 0.0.0.0 可能会起作用。
With the link you specified in the docker-compose.yml, your backend container should then be able to connect to the mongo container through mongodb:27017
使用您在 docker-compose.yml 中指定的链接,您的后端容器应该能够通过以下方式连接到 mongo 容器 mongodb:27017
回答by Daniel Andrei Minc?
You have to tell the container to use it's own IP Address instead of localhost.
您必须告诉容器使用它自己的 IP 地址而不是 localhost。
For example, let's assume you generated scaffold code with expressjs, you have to write in routes/index.js
例如,假设您使用 生成了脚手架代码expressjs,您必须写入routes/index.js
var mongodb = require('mongodb');
router.get('/thelist', function(req, res){
// Get a Mongo client to work with the Mongo server
var MongoClient = mongodb.MongoClient;
// Define where the MongoDB server is
var url = 'mongodb://172.17.0.5:27017/dbname';
// Connect to the server
MongoClient.connect(url, function (err, db) {
.........
where 172.17.0.5is the $CONTAINER_IP
这里172.17.0.5是$CONTAINER_IP
you can find the container ip via
$ docker inspect $CONTAINER_HOSTNAME | grep IPAddress
您可以通过以下方式找到容器IP
$ docker inspect $CONTAINER_HOSTNAME | grep IPAddress
If you still can't understand you can take a peek at my Docker NodeJS and MongoDB app
如果你还是不明白,你可以看看我的Docker NodeJS 和 MongoDB 应用程序

