Ankit | Jr DevOps Engineer | akv280501@gmail.com
π File Breakdown & Architecture
1. docker-compose.yml
Purpose: Defines multi-container architecture
version: '3.8'
services:
app:
build: .
container_name: node_app
ports:
- "3000:3000"
environment:
- DB_URI=mongodb://mongo:27017/testdb
depends_on:
- mongo
mongo:
image: mongo:latest
container_name: mongo_db
ports:
- "27017:27017"
volumes:
- mongodata:/data/db
volumes:
mongodata:
Key Concepts:
Service Discovery: Containers communicate via service names (mongo)
Port Mapping: 3000:3000 exposes Node.js to host machine
Declarative Setup: Infrastructure-as-Code
2. Dockerfile
Layer-by-Layer Optimization:
FROM node:18-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
Best Practices:
Layer Caching: Separate
COPY package.json
+RUN npm install
speeds up rebuildsMinimal Images: Alpine reduces attack surface
Explicit Ports: Documentation for other developers
3. package.json
Dependency Management:
{
"name": "node-mongo-app",
"version": "1.0.0",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.18.2",
"mongoose": "^7.4.3"
}
}
Why It Matters:
npm start
becomes the container's PID 1 process
Version pinning (^4.18.2
) prevents breaking changes
4. server.js
const express = require('express');
const mongoose = require('mongoose');
const app = express();
const PORT = 3000;
// Connect to MongoDB -> 'mongodb://mongo:27017/testdb'
// 'mongo' is the service name we will use in docker-compose
const dbUri = process.env.DB_URI || 'mongodb://localhost:27017/testdb';
mongoose.connect(dbUri)
.then(() => console.log('MongoDB connected successfully!'))
.catch(err => console.error('MongoDB connection error:', err));
app.get('/', (req, res) => {
res.send('Hello from your Node.js app! Connected to MongoDB.');
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
π My DevOps Learning Path
20 Projects Challenge:
β Node+MongoDB Containers
Flask+Redis+PostgreSQL (Coming Soon)
π¬ Let's Build Together!
I need your input on:
*Which project should I tackle next?
*How would you improve this setup?
*DevOps interview tips for 2-3 yr experience roles
Hire Me:
π +91-8384860549 | π§ akv280501@gmail.com
Open to relocation | Immediate joiner
Top comments (0)