Containerization with Docker: A Practical Guide

“It works on my machine” is the oldest joke in software engineering. Docker makes it not funny anymore — because it stops being true.

What is Docker?

Docker packages your application and its dependencies into a single, portable container. That container runs identically on your laptop, your CI server, and your production host.

Writing Your First Dockerfile

FROM node:20-alpine
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
CMD ["node", "index.js"]

Docker Compose

For multi-container setups, Docker Compose is the tool. Define your app, database, and cache in a single docker-compose.yml:

services:
  web:
    build: .
    ports:
      - "3000:3000"
  db:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: secret

Best Practices

  1. Keep images small — use Alpine bases.
  2. Layer your Dockerfile cache-friendly: copy package.json before source code.
  3. Use multi-stage builds to strip build tools from the final image.
  4. Never run as root inside a container.

Conclusion

Docker is not magic, but it feels like it. Once you containerize your workflow, you wonder how you ever lived without it.