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


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! ๐
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