Docker mongo image 'Connection refused' from other container

大憨熊 提交于 2019-11-28 21:22:20

问题


I'm new to docker. I'm trying to create a MongoDB container and a NodeJS container. My file looks:

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 install and then node .. But docker-compose up ends 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.1 in the file /etc/mongod.conf. Is this right?

I use boot2docker on a Win10 system.

How can I solve this problem so that node can connect to the MongoDB?


回答1:


In your backend app, connect to mongodb:27017 instead of 127.0.0.1:27017. Where 'mongodb' is the name of your container within docker-compose.yml.




回答2:


I recently encountered similar issue. I am running docker toolbox under win 10 and this is how it worked for me:

1) I had to verify which URL my default docker machine is using. This can be checked by running docker-machine ls command. It will list available machines:

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

docker run -d -it -p 27017:27017 mongo

3) At that point the valid mongo url would look something like this

var dbhost = 'mongodb://192.168.99.100:27017/test

where 192.168.99.100 was the default machine URL from the point 1)

Hope it helps someone.




回答3:


Most likely, yes. 127.0.0.1 points to localhost inside the mongodb container, so is not accessible from outside the container. Binding to 0.0.0.0 will probably work.

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




回答4:


You have to tell the container to use it's own IP Address instead of localhost.

For example, let's assume you generated scaffold code with expressjs, you have to write in 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.5 is the $CONTAINER_IP

you can find the container ip via $ docker inspect $CONTAINER_HOSTNAME | grep IPAddress

If you still can't understand you can take a peek at my Docker NodeJS and MongoDB app



来源:https://stackoverflow.com/questions/34711642/docker-mongo-image-connection-refused-from-other-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!