Docker Compose Setup Guide

Node.js + MongoDB Example - Full Stack Setup in Under 5 Minutes

Updated 2025
5 min read
Beginner

What You'll Build

In this guide, you'll set up a Node.js backend connected to a MongoDB database using Docker Compose. This configuration is perfect for modern web applications, microservices, and API-driven projects.

By the end, you’ll have a complete multi-container setup where Node.js and MongoDB communicate seamlessly through Docker’s internal networking — ready for local development or production.

What You'll Need

Prerequisites

  • Docker & Docker Compose installed

    Install Docker Compose →

  • Basic terminal and Node.js knowledge

    Familiar with npm and command-line tools.

  • 2GB+ free disk space

    For Docker images and MongoDB data volume.

Docker Compose YAML File

Complete docker-compose.yml

Copy this file to your project root and adjust environment variables as needed.

version: '3.9'

services:
  mongodb:
    image: mongo:7
    container_name: mongodb
    restart: always
    ports:
      - "27017:27017"
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: password
      MONGO_INITDB_DATABASE: appdb
    volumes:
      - mongo_data:/data/db
    healthcheck:
      test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
      interval: 10s
      timeout: 5s
      retries: 5

  app:
    build: .
    container_name: nodeapp
    restart: always
    ports:
      - "3000:3000"
    environment:
      - MONGO_URL=mongodb://root:password@mongodb:27017/appdb?authSource=admin
    depends_on:
      mongodb:
        condition: service_healthy

volumes:
  mongo_data:

Pro Tip

MongoDB uses a persistent Docker volume here — your data will survive container restarts. The `depends_on` health check ensures Node.js waits until MongoDB is ready.

Service Breakdown

MongoDB Service

  • Image: mongo:7 (latest stable)
  • Port: 27017 (default MongoDB port)
  • Database: appdb
  • User: root
  • Password: password

Connection URI

mongodb://root:password@localhost:27017/appdb?authSource=admin

Node.js App Service

This service builds from your local Dockerfile and connects to MongoDB using the internal service name mongodb.

Example Dockerfile

# Dockerfile
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install --production
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

Run the Setup

Step-by-Step Instructions

# Start containers
docker compose up -d

# Check status
docker compose ps

# View logs
docker compose logs -f

# Connect to MongoDB shell
docker exec -it mongodb mongosh -u root -p password

# Check collections
show dbs
use appdb

Troubleshooting Common Issues

MongoDB Connection Failed

Make sure your app uses mongodb://mongodb:27017 instead of localhost inside containers.

Data Lost After Restart

Verify your volume configuration and check with docker volume ls. The named volume mongo_data ensures persistence.

Docker Compose Best Practices for Node + MongoDB

  • ✅ Use environment variables for database credentials
  • ✅ Add health checks for both MongoDB and Node.js
  • ✅ Use a dedicated Dockerfile for your Node.js app
  • ✅ Keep app logs mounted to a local volume for debugging
  • ✅ Use specific image tags (e.g., mongo:7, node:20-alpine)