Support Online
Skip to main content

Node.js and MongoDB Development Environment Setup with Docker Compose

What Will You Learn in This Guide?

This guide teaches you how to set up a Docker-based development environment for projects using Node.js and MongoDB.
With Docker Compose, you create a multi-container structure and ensure code synchronization and data persistence.
The goal is to create a consistent and portable development process.

Technical Summary

Main topic: Containerize Node.js application with MongoDB using Docker Compose
Purpose: To make the development environment isolated, maintainable and fast
Steps: Code refactor, environment variables, service definition, data persistence, testing


1. Preparing the Project and Updating Dependencies

nodemon is used to automatically detect code changes during development.

git clone https://github.com/do-community/nodejs-mongo-mongoose.git node_project
cd node_project
  • This command downloads the sample Node.js project to the local environment.

Add the following dependency to the package.json file:


"devDependencies": {
"nodemon": "^1.18.10"
}
  • This setting allows the application to restart in case of code changes.

2. Making the Application Container Compatible

  1. Environment variables are used instead of fixed settings.

  2. Port Configuration


const port = process.env.PORT || 8080;
  • This structure facilitates port change in different environments.

Database Information

  1. Database credentials are extracted from the code and moved to the .env file.

MONGO_USERNAME=genix_user
MONGO_PASSWORD=guclu_sifre
MONGO_PORT=27017
MONGO_DB=kopekbaligi_db
  • This file securely manages sensitive information.

3. Making the Database Connection Resilient

  1. Containers can be launched at different speeds.
  • A retry mechanism is added to prevent connection drops.

const options = {
useNewUrlParser: true,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 500,
connectTimeoutMS: 10000,
};
  • These settings make the MongoDB connection more stable.

4. Defining Services with Docker Compose

  1. Docker Compose allows you to manage all services from a single file.

Wait-for Script

  1. It prevents the application from starting before the database is ready.

docker-compose.yml


version: '3'
services:
nodejs:
build: .
volumes:
- .:/home/node/app
- node_modules:/home/node/app/node_modules
ports:
- "80:8080"
command: ./wait-for.sh db:27017 -- nodemon app.js

db:
image: mongo:4.1.8
volumes:
- dbdata:/data/db
  • This structure orchestrates Node.js and MongoDB services together.

5. Launching and Testing the Application


docker-compose up -d
  • This command starts all services in the background.

Check the app from browser:

http://server\_ip\_your_address


docker-compose down
  • This command deletes the containers, but the data is permanently preserved.

Frequently Asked Questions (FAQ)

1. Why are code changes reflected instantly? Bind mount and nodemon work together.

2. Why is the .env file not added to the image? For security, sensitive information is not kept in the image.

3. Will the data be lost when the container is deleted? No, it is permanent because Docker volume is used.


Result

Docker Compose standardizes the development environment. Node.js and MongoDB projects become more manageable. The same structure can be easily ported to the production environment.

You can try the GenixNode platform now to develop your Node.js projects on an isolated, secure and scalable infrastructure.