node.jsmongodbdockerboot2docker

Docker mongo image 'Connection refused' from other container


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?


Solution

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