Loading introduction
Loading stats
Loading about
Loading posts
Loading article
Skip to content
Md Nayeem Hossain
Explorer
home.tsx
docker-containerization-for-developers.md
docker-containerization-for-developers.md
blog.json

// DevOps

Docker Containerization: From Development to Production

Master Docker for modern application deployment with practical examples and production-ready configurations.

Md Nayeem HossainDec 18, 202411 min read

Docker Containerization for Developers#

"It works on my machine" is a phrase every developer hates hearing. Docker solves this by packaging your application and all its dependencies into a standard unit called a container.

In this guide, I'll explain how to Dockerize a Node.js application properly, from a development setup with hot-reloading to an optimized production build.

The Dockerfile#

The Dockerfile is the blueprint for your container. A common mistake is including unnecessary files in the final image, making it huge. We can use Multi-stage Builds to fix this.

We compile and build the app in a "builder" stage, and then copy only the necessary artifacts to a final, slim "production" stage.

dockerfile
1# Stage 1: Builder2# We use a larger image with all build tools3FROM node:20-alpine AS builder4WORKDIR /app5COPY package*.json ./6RUN npm ci7COPY . .8RUN npm run build9 10# Stage 2: Production11# We use a clean, small image12FROM node:20-alpine AS production13WORKDIR /app14ENV NODE_ENV=production15 16# Security: Don't run as root!17USER node18 19# Copy only what we need from the builder stage20COPY --from=builder /app/dist ./dist21COPY --from=builder /app/node_modules ./node_modules22COPY --from=builder /app/package.json ./23 24EXPOSE 300025CMD ["node", "dist/index.js"]

Docker Compose for Development#

Running containers manually is tedious. Docker Compose allows you to define your entire stack (App, Database, Redis) in one file.

This setup below mounts your local code into the container, so when you save a file, the app updates instantly (Hot Reloading), just like running it locally!

yaml
1version: '3.8'2 3services:4  app:5    build:6      context: .7      dockerfile: Dockerfile.dev8    ports:9      - "3000:3000"10    # This volume mapping enables hot-reloading11    volumes:12      - .:/app13      - /app/node_modules14    environment:15      - DATABASE_URL=mongodb://mongo:27017/myapp16    depends_on:17      - mongo18 19  mongo:20    image: mongo:721    ports:22      - "27017:27017"23    volumes:24      - mongo_data:/data/db25 26volumes:27  mongo_data:

Production Best Practices#

  1. Use Specific Tags: Never use :latest in production. Use specific versions (e.g., node:20.9.0-alpine) to ensure your build is reproducible.
  2. Health Checks: Add a HEALTHCHECK instruction so Docker (or Kubernetes) knows if your app is stuck or crashing.
  3. Secrets: Never bake secrets (API keys, passwords) into the image. Pass them as environment variables at runtime.
dockerfile
1# Add this to your Dockerfile2HEALTHCHECK --interval=30s --timeout=3s \3  CMD wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1
#Docker#DevOps#Containers#Deployment
OlderPostgreSQL Query Optimization: Advanced TechniquesNewer MongoDB Performance Optimization: Complete Guide

Outline

  • Docker Containerization for Developers
  • The Dockerfile
  • Docker Compose for Development
  • Production Best Practices