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
- Keep images small — use Alpine bases.
- Layer your Dockerfile cache-friendly: copy
package.jsonbefore source code. - Use multi-stage builds to strip build tools from the final image.
- 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.