0

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!

2 Answers 2

2

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';

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

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