Cannot connect to MongoDB via node.js in Docker

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

node.js connection:

  • Can Docker Autonomously Restart Containers and Commit Changes with New Image Tag?
  • Configuring ELK with Kitematic - Elastic Search won't start - localhost not available
  • Docker container on another IP and not reachable
  • Chrome Node in Selenium/Docker keeps crashing
  • Copy asset with location found via shell function
  • Can I mount a windows network path inside docker without -v option?
  • 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]'
    

  • How to keep vars between docker exec_run queries ?
  • How to create docker image from jenkins running on docker
  • Active directory accounts inside a windows container (server 2016 TP5)
  • Why I need a password to get access to the jupyter notebook when I use docker machine?
  • Jenkins docker-plugin - Job does not start (waiting for executor)
  • SpringBoot with Tomcat uses around 190mb of memory. What processes are running within Springboot?
  • 2 Solutions collect form web for “Cannot connect to MongoDB via node.js in Docker”

    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
    

    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

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.