๐Ÿ“… Day 26: Dockerfile + Docker Compose (Node.js + MongoDB)

Ritesh SinghRitesh Singh
2 min read

Today, I worked on building a Node.js + MongoDB project using Docker and Docker Compose. This task helped me understand how to containerize applications efficiently and link services together.


๐Ÿ—‚๏ธ Project Structure

node-mongo-app/
โ”œโ”€โ”€ app/
โ”‚   โ”œโ”€โ”€ server.js
โ”‚   โ””โ”€โ”€ ...other files
โ”œโ”€โ”€ Dockerfile
โ”œโ”€โ”€ docker-compose.yml
โ”œโ”€โ”€ package.json

๐Ÿ› ๏ธ Step 1: Dockerfile (Multi-stage)

# Base stage
FROM node:18-alpine AS base
WORKDIR /app
COPY package*.json ./
RUN npm install --production

# Copy source code
COPY ./app ./app
WORKDIR /app/app

# Final image
EXPOSE 3000
CMD ["node", "server.js"]

๐Ÿ” Explanation

  • Stage 1: Use node:18-alpine for a small image.

  • Install dependencies: npm install --production installs only needed modules.

  • COPY: Transfers app files to image.

  • EXPOSE: Documents port 3000.

  • CMD: Starts the app.


๐Ÿณ Step 2: docker-compose.yml

version: '3.8'

services:
  web:
    build: .
    container_name: node-mongo-app_web_1
    ports:
      - "3000:3000"
    depends_on:
      - mongo
    environment:
      - MONGO_URL=mongodb://mongo:27017/mydb

  mongo:
    image: mongo:6.0
    container_name: node-mongo-app_mongo_1
    volumes:
      - mongo-data:/data/db
    ports:
      - "27017:27017"

volumes:
  mongo-data:

๐Ÿ” Explanation

  • web: Builds from Dockerfile, connects to MongoDB.

  • mongo: Uses MongoDB 6.0 and mounts a volume.

  • depends_on: Ensures MongoDB starts first.


๐Ÿ“ฆ package.json

{
  "name": "node-mongo-app",
  "version": "1.0.0",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.18.2",
    "mongoose": "^7.6.1"
  }
}

๐Ÿ“„ app/server.js

const express = require('express');
const mongoose = require('mongoose');
const app = express();

const PORT = 3000;
const MONGO_URL = process.env.MONGO_URL || 'mongodb://localhost:27017/mydb';

mongoose.connect(MONGO_URL, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
})
.then(() => console.log('MongoDB Connected'))
.catch((err) => console.error('Mongo Error:', err));

app.get('/', (req, res) => {
  res.send('Hello from Node + Mongo App in Docker!');
});

app.listen(PORT, () => console.log(`Server running on port ${PORT}`));

โ–ถ๏ธ Commands I Practiced

Build and Run

docker-compose up --build

Stop and Remove Containers

docker-compose down

List All Containers

docker ps -a

Start Containers in Background

docker-compose up -d

Rebuild Containers

docker-compose up --build

๐Ÿง  Learnings & Observations

  • Learned how to use multi-stage builds in Docker.

  • Understood how services communicate within Docker networks.

  • Practiced common Docker Compose commands.

  • Managed persistent MongoDB data using volumes.


๐Ÿ”— Connect With Me


๐Ÿ“ธ Optional: Add Screenshot

Add a screenshot of the running app (localhost:3000) or terminal logs from docker-compose up.


โœ… End of Day 26. A solid foundation in Docker Compose! Ready for deploying complex stacks! ๐Ÿš€

0
Subscribe to my newsletter

Read articles from Ritesh Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ritesh Singh
Ritesh Singh

Hi, Iโ€™m Ritesh ๐Ÿ‘‹ Iโ€™m on a mission to become a DevOps Engineer โ€” and Iโ€™m learning in public every single day.With a full-time commitment of 8โ€“10 hours daily, Iโ€™m building skills in: โœ… Linuxโœ… Git & GitHubโœ… Docker & Kubernetesโœ… AWS EC2, S3โœ… Jenkins, GitHub Actionsโœ… Terraform, Prometheus, Grafana I post daily blogs on Hashnode, push projects to GitHub, and stay active on LinkedIn and Twitter/X. Letโ€™s connect, collaborate, and grow together ๐Ÿš€ #100DaysOfDevOps #LearningInPublic #DevOps