user5161995
user5161995

Reputation:

How to dockerize a node.js talking to a local mongodb

Let's say you want to dockerize a node.js application. And this node.js app talks to an instance of MongoDB on the host machine. Is this a good practice? How is it done?

If it's not a good practice, then is it better to provide an instance of MongoDB inside your Docker container with the node.js scripts that talk to it?

Thanks!

Upvotes: 0

Views: 293

Answers (2)

user5161995
user5161995

Reputation:

Above answer is good practice. fwiw, you can also use the docker network ip as your in the mongo url.

Upvotes: 0

Bukharov Sergey
Bukharov Sergey

Reputation: 10185

The good practice is create Mongo DB container from official image and link your application and Mongo db with docker compose.

This is sample configuration for your project: docker-compose.yml file

version: '2'
services:
  web:
    image: node
    ports:
     - "80:80"
    volumes:
     - .:/code
    depends_on:
     - mongo

  mongo:
    image: mongo

Then you can connect to mongo from your app by url

var url = 'mongodb://mongo:27017/your_database';

Upvotes: 2

Related Questions