12

My node.js express app cannot connect to the MongoDB in a Docker. I'm not that familiar with Docker.

node.js connection:

import mongodb from 'mongodb';
...
mongodb.MongoClient.connect('mongodb://localhost:27017', ... );

Dockerfile:

FROM node:argon
RUN mkdir /app
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
EXPOSE 3000
CMD ["npm", "start"]

docker-compose.yml

version: “2”
 services:
  web:
   build: .
   volumes:
     — ./:/app
   ports:
   — “3000:3000”
   links:
    — mongo
   mongo:
    image: mongo
    ports:
      — “27017:27017”

Build command: docker build -t NAME .

Run command: docker run -ti -p 3000:3000 NAME

Connection error:

[MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]]
  name: 'MongoError',
  message: 'failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]'
5
  • DId you change the bind_ip in the MongoDB config in it's container? If not then it is only listening on localhost to that container only.
    – Neil Lunn
    Jul 6, 2017 at 1:24
  • what would be that @NeilLunn? Jul 6, 2017 at 1:26
  • I didnt set any MongoDB config file btw... @NeilLunn Jul 6, 2017 at 1:27
  • It will be in /etc/mongod.conf by default. You would need to bind to 0.0.0.0 in order to listen on "all" external interfaces. In depth "docker" config is not really my thing either, but you appear to be using a more "legacy" mode of "linking". And the documentation seems to suggest "Docker Networks" as the preferred method for exposing ports between containers
    – Neil Lunn
    Jul 6, 2017 at 1:37
  • For me it simply worked: "docker run --network=host myapp:1.0", .. my app was connected to a wrong network . Jan 19, 2022 at 19:52

4 Answers 4

28

Try:

mongodb.MongoClient.connect('mongodb://mongo:27017', ... );

Change your docker-compose.yml:

version: "2"

services:

  web:
    build: .
    volumes:
      - ./:/app
    ports:
      - "3000:3000"
    links:
      - mongo

  mongo:
    image: mongo
    ports:
      - "27017:27017"

And use some docker compose commands:

docker-compose down
docker-compose build
docker-compose up -d mongo
docker-compose up web
7
  • didnt work: docker-compose up -d mongo ERROR: yaml.scanner.ScannerError: mapping values are not allowed here in "./docker-compose.yml", line 2, column 10 Jul 6, 2017 at 1:34
  • I think you need to change the docker-compose.
    – messivanio
    Jul 6, 2017 at 1:52
  • Please, see my new answer. Maybe this can help you.
    – messivanio
    Jul 6, 2017 at 1:59
  • got this error: Recreating test_web_1 ... Recreating test_web_1 ... error ERROR: for test_web_1 no such image: sha256:2b0195350ef188fa3ca4fc41f3670fa1b7650ab0bd2cde74e5a0b6c95193c55b: No such image: sha256:2b0195350ef188fa3ca4fc41f3670fa1b7650ab0bd2cde74e5a0b6c95193c55b ERROR: for web no such image: sha256:2b0195350ef188fa3ca4fc41f3670fa1b7650ab0bd2cde74e5a0b6c95193c55b: No such image: sha256:2b0195350ef188fa3ca4fc41f3670fa1b7650ab0bd2cde74e5a0b6c95193c55b Jul 6, 2017 at 2:06
  • 1
    I tried this out since I was getting errors connecting to mongo on my local machine and it works perfectly fine. Want to udnerstand where is this db? Is the data persistent after I stop the container?
    – Anish
    Sep 15, 2019 at 18:59
9

Try this.

  1. When using linked docker containers you should use the name of the container in this case for example your connection to mongodb should be mongodb.MongoClient.connect('mongodb://mongo:27017', ... ); instead of mongodb.MongoClient.connect('mongodb://localhost:27017', ... );. The reason for changing it to mongo is because you used the links attribute to mongo in your docker-compose.yml. That would result to a hostname of mongo in your /etc/hosts of the web docker container. Reference linking-containers.
  2. The docker-compose.yml seems to be lacking an indention. On the mongo attribute should be the same level as web.

    version: '2'
    services:
      web:
       build: .
       volumes: ['./:/app']
       ports: [ '3000:3000' ]
       links: [ mongo ]
      mongo:
       image: mongo
       ports: [ '27017:27017' ]
    
  3. I tried your configuration using my docker what Ive done is update docker-compose.yml then I docker-compose build then docker-compose up. Logs of my local run

4
  • ive updated the question, is the same but with the mongo Jul 6, 2017 at 1:46
  • 1
    Why is still connecting to localhost:27017 ? It should be mongo:27017 if you have changed the connection from node.js to mongodb. In your node.js file it should be mongodb.MongoClient.connect('mongodb://mongo:27017', ... ); Jul 6, 2017 at 1:48
  • @MarceloFilho have you run just docker-compose up? Jul 6, 2017 at 1:52
  • Okay. Can you check in /etc/hosts of the web container if the IP of the mongo is added? Also what is the output of docker ps? Jul 6, 2017 at 2:04
1

I am not sure if you still have this question, but the datasources.json should be:

"host": "mongo" 

rather than "localhost".

In my logs I see:

mongo    |  NETWORK  [listener] connection accepted from 172.22.0.3:47880 #1 (1 connection now open)

As you can see, docker compose will NAT mongo to another V-LAN. The IP address 172.22.0.0 is an internal IP address used by the daemon to route a docker-compose image. So localhost is now not in the game.

At least, it works for me.

datasources.json

"mongoDS": {
    "host": "mongo",
    "port": 27017,

...

0

in my case works like this :

just link to the container from the command line. db is my up database container

sudo docker run -it --link db:db1 --publish 4000-4006:4000-4006 --name backend backend:latest

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.