Docker
DevOps
Backend
Tutorial
Beginner
Ask anything about this article
Hi! I've read this article.
What would you like to know?
@farhan
Docker changed how we deploy software forever. Instead of "it works on my machine," Docker ensures your application runs identically everywhere — your laptop, your colleague's Mac, the CI server, and production.
Docker is a platform that packages your application and all its dependencies into a standardized unit called a container. Think of it as a lightweight, portable virtual machine that contains everything your app needs to run.
Let's containerize a Node.js application:
dockerfile# Use official Node.js image as base
FROM node:20-alpine
# Set working directory inside the container
WORKDIR /app
# Copy package files first (for better caching)
COPY package*.json ./
# Install dependencies
RUN npm ci --only=production
# Copy application code
COPY . .
# Expose port
EXPOSE 3000
# Define the command to run
CMD ["node", "server.js"]
bash# Build the image
docker build -t my-app:1.0 .
# Run the container
docker run -d -p 3000:3000 --name my-app my-app:1.0
# Check running containers
docker ps
# View logs
docker logs my-app
# Stop the container
docker stop my-app
Most real applications need multiple services. Docker Compose orchestrates them:
yaml# docker-compose.yml
version: '3.8'
services:
app:
build: .
ports:
- "3000:3000"
environment:
- MONGODB_URI=mongodb://mongo:27017/mydb
- REDIS_URL=redis://redis:6379
depends_on:
- mongo
- redis
mongo:
image: mongo:7
volumes:
- mongo-data:/data/db
ports:
- "27017:27017"
redis:
image: redis:7-alpine
ports:
- "6379:6379"
volumes:
mongo-data:
bash# Start all services
docker-compose up -d
# Stop all services
docker-compose down
# View logs for a specific service
docker-compose logs -f app
latest in productionnode_modules
.git
.env
*.md
docker-compose*.yml
.dockerignore
Dockerfile
Docker is an essential skill for every modern developer. Start containerizing your projects today.