303
303

Reputation: 1108

Node app fails to connect to MongoDB, but only in Docker

I have two apps:

The two apps interact flawlessly when my node app is run natively. Now I've put it inside a Docker container and when I start both together with docker compose up, the backend can not connect to MongoDB.

This is an excerpt of the startup sequence:

mongodb_1   | 2018-11-10T22:22:52.481+0000 I NETWORK  [initandlisten] waiting for connections on port 27017
[...]
backend_1   | 2018-11-10T22:23:48.119Z 'MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]'

This is my docker-compose.yml:

version: '2'
services:

  mongodb:
    image: bitnami/mongodb:latest
    expose:
      - 27017
    environment:
      - ALLOW_EMPTY_PASSWORD=yes

  backend:
    build: ./backend
    environment:
      API_HOST: http://localhost:3000/
      APP_SERVER_PORT: 3000
    expose:
      - 3000
    volumes:
      - ./backend:/app/backend
    links:
      - mongodb
    depends_on:
      - mongodb

This is my node call to the DB:

mongoose.connect('mongodb://localhost:27017/groceryList', {
  useNewUrlParser: true
});

I skimmed about 15 Stackoverflow questions asking the same and I am not getting the cause:

What am I missing?

Upvotes: 2

Views: 2257

Answers (2)

Gabriel Henrique
Gabriel Henrique

Reputation: 13

It's not possible to use localhost:27017 into a container to communicate with other because the scope "localhost" is self refering (localhost:27017 will look for the port 27017 into the container backend - not in mongodb container)

Then, you ned to put or the service name (mongodb) or IP of your machine

Upvotes: 0

Siyu
Siyu

Reputation: 12089

Your mongodb service is named mongodb not mongo.

Try

mongoose.connect('mongodb://mongodb:27017/groceryList', {
  useNewUrlParser: true
});

The generic form is 'mongodb://mongoServiceName:27017/dbname', this uses docker's automatic dns resolution for containers within the same network.

And as you may already know from other questions/answers, within a container, the url is relative to itself, therefore since there not mongodb running inside the backend container, it can't connect to it.

Upvotes: 3

Related Questions