36

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?

Piu130
  • 1,288
  • 3
  • 16
  • 28

5 Answers5

93

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.

Thomasleveil
  • 95,867
  • 15
  • 119
  • 113
  • 1
    Ah ok I understand. Instead of the IP we use the link name inside a docker container. Thanks! – Piu130 Jan 11 '16 at 09:58
  • 25
    For clarification for other people having the issue. Within your node app: ```mongoose.connect('mongodb://db:27017/whatever');``` Where 'db' is the name of your container within docker-compose.yml - i.e. ```depends_on: - db db: image: mongodb``` – colbyJax Jul 07 '16 at 15:16
  • @colby jax I am using mongoose.connect() as u mentioned. But I am getting error when I use docker-compose up – N15 Feb 05 '18 at 10:52
  • 2
    this gives me ENOTFOUND mongodb mongodb:27017 – Stepan Yakovenko Jun 18 '18 at 18:56
  • Even i'm facing the same error and i couldn't find any workaround/fix for this yet. Please let us know – amitam Mar 02 '19 at 19:11
  • i use my container name for that but it does not works – amdev Sep 28 '20 at 23:24
  • 1
    @a_m_dev it should be the docker compose service name. I fixed my answer – Thomasleveil Sep 29 '20 at 04:39
  • It's the name of the container which will replace ```localhost```. i.e. ```mongodb://your_mongodb_container_name:27017/```. You can check this container name using the command ```docker ps -a``` in your console – Abercrombie Dec 10 '20 at 20:47
  • yes "localhost" -> should be replace with container name which is "mongo" – AmitNayek Dec 27 '20 at 16:05
16

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.

Marcin
  • 161
  • 1
  • 3
  • I do not have the `docker-machine` command available, but I managed to check the address of my mongo image using `docker inspect mongo`, where mongo is the name of the instance – Ramses Sep 25 '17 at 21:14
  • @Marcin I built one image (sample nodejs application) when I try to run that application with my localdb. I am getting error.I followed ur steps .. docker run -d -it -p 27017:27017 mongo this is for running mongodb I am running the nodejs application by docker run -d it -p 8088:8088 nitikishu/samplenodejs now I want to connect mongodb with my application what command should I run In my docker ip address 192.168.99.100:8088 my application is running but mongodb is not working in localhost:27017 this message is displaying. – Vinoth Jan 24 '18 at 10:23
  • and It looks like you are trying to access MongoDB over HTTP on the native driver port. Now what should I do? using which host and command to connect the mongodb with the application – Vinoth Jan 24 '18 at 10:24
  • @Marcin var dbhost = 'mongodb://192.168.99.100:27017/test will it connnect the db? – N15 Feb 06 '18 at 07:46
  • i was able to find the `IPAddress` of my mongo docker using `docker inspect mongo-container` command, and using `IPAddress` it worked, but the node.js code would need to have something dynamic name, not some hard-coded IP. So what should be used? i tried with `mongo-container-name`, didn't work – amitam Mar 02 '19 at 19:14
6

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

thaJeztah
  • 27,738
  • 9
  • 73
  • 92
5

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

Daniel Andrei Mincă
  • 4,446
  • 2
  • 19
  • 30
0

I've changed

mongodb://admin:admin@127.0.0.1:27017/admin

to

mongodb://admin:admin@mongo:27017/admin

and it works

Thanks for the answer https://stackoverflow.com/a/34711892/3841211

aH6y
  • 436
  • 4
  • 8