Whether youβre a backend developer, DevOps enthusiast, or just getting into microservices, Docker is one of the most essential tools in the modern development toolbox.
This guide will walk you through:
- What Docker & Docker Compose are
- Why Docker became so popular
- How to install Docker
- How to use Docker & Docker Compose
- Real-world project: Node.js + MongoDB
Letβs dive in π
π§ What is Docker?
Docker is a platform for packaging applications into containers β lightweight, portable units that bundle your app with its dependencies.
Imagine this:
"It works on my machine" β Docker makes that your reality everywhere.
Each container includes:
- Code
- Runtime (e.g., Node.js, Java)
- Libraries
- Environment/configuration
So your app runs the same on any system, cloud, or CI/CD pipeline.
π Why Docker Became So Popular
Docker took over the world for several key reasons:
β
Lightning Fast β Containers start in milliseconds vs full VMs
β
Environment Consistency β Same behavior in dev, staging, and prod
β
DevOps & CI/CD Friendly β Works seamlessly with GitHub Actions, Jenkins, GitLab CI
β
Perfect for Microservices β Each service in its own container
β
Portable β Works on Windows, Linux, Mac, cloud platforms
β
Vibrant Ecosystem β Docker Hub provides 100,000+ ready-to-use images
βοΈ Docker vs Traditional Deployment
Feature | Traditional Deployment | Docker-Based Deployment |
---|---|---|
Setup time | Manual setup, often inconsistent | One-line setup (docker run ) |
Environment conflicts | Very common | Fully isolated per container |
Portability | OS/Env dependent | Runs anywhere Docker is installed |
Scaling services | Hard & manual | Easy with docker-compose or K8s |
Dev β Staging β Prod | Error-prone | Consistent containers everywhere |
π§° How to Install Docker & Docker Compose
πΉ 1. Install Docker
- Go to: https://www.docker.com/products/docker-desktop
- Download for your OS (Windows, macOS, Linux)
- Follow installer instructions
- After install, run:
docker --version
You should see something like:
Docker version 24.x.x, build abcdef
πΉ 2. Docker Compose (usually pre-installed)
Check with:
docker compose version
If it shows the version, you're good to go!
π¦ Real Example: Node.js + MongoDB with Docker Compose
Letβs build a simple project that runs a Node.js API connected to MongoDB β all inside Docker.
π§± Project Structure
docker-node-mongo-app/
βββ app/
β βββ server.js
β βββ package.json
βββ Dockerfile
βββ docker-compose.yml
βοΈ Step 1: Node.js Code (app/server.js
)
const express = require('express');
const mongoose = require('mongoose');
const app = express();
mongoose.connect('mongodb://mongo:27017/mydb', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
app.get('/', (req, res) => {
res.send('Hello from Dockerized Node.js + MongoDB!');
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
βοΈ Step 2: app/package.json
{
"name": "docker-node-mongo",
"version": "1.0.0",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.18.2",
"mongoose": "^7.2.2"
}
}
π³ Step 3: Dockerfile
FROM node:18
WORKDIR /app
COPY app/package*.json ./
RUN npm install
COPY app .
EXPOSE 3000
CMD ["npm", "start"]
π Step 4: docker-compose.yml
version: '3.8'
services:
app:
build: .
ports:
- "3000:3000"
depends_on:
- mongo
mongo:
image: mongo
ports:
- "27017:27017"
volumes:
- mongo-data:/data/db
volumes:
mongo-data:
βΆοΈ How to Run the Project
- Open terminal in your project root.
- Run the following:
docker-compose up --build
- Visit:
π http://localhost:3000
Youβll see:
Hello from Dockerized Node.js + MongoDB!
π§Ή How to Stop & Clean Up
To stop the app:
docker-compose down
To remove the volume data as well:
docker-compose down -v
π― Recap
By now, you've learned:
β
What Docker and Docker Compose are
β
Why Docker became a standard in modern development
β
How to install and check Docker setup
β
How to containerize an app
β
How to run multi-container apps with Compose
π¬ What's Next?
You can now dockerize:
- Python Flask/Django apps
- Spring Boot + PostgreSQL apps
- React/Next.js with NGINX
Top comments (0)