Docker Containerization
What is Docker?
Docker is a platform for developing, shipping, and running applications in containers. Containers are lightweight, standalone, and executable packages that include everything needed to run a piece of software.
Why Containers?
Key Benefits
- Consistency — “It works on my machine” is no longer an excuse
- Isolation — Each container has its own filesystem and dependencies
- Portability — Run anywhere Docker is installed
- Scalability — Easy to spin up multiple instances
Basic Commands
# Pull an image
docker pull nginx:latest
# Run a container
docker run -d -p 8080:80 --name webserver nginx
# List running containers
docker ps
# View logs
docker logs webserver
# Stop and remove
docker stop webserver && docker rm webserverDockerfile Example
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]Security Note
Always use specific image tags (e.g.,
node:18-alpine) instead oflatestin production. This ensures reproducible builds. See Web Development Basics for more on production best practices.
Docker Compose
For multi-container applications, Docker Compose is essential:
version: '3.8'
services:
web:
build: .
ports:
- "3000:3000"
depends_on:
- db
- redis
db:
image: postgres:15
environment:
POSTGRES_PASSWORD: secret
redis:
image: redis:alpineContainer vs VM
| Aspect | Container | Virtual Machine |
|---|---|---|
| Size | MBs | GBs |
| Startup | Seconds | Minutes |
| Isolation | Process-level | Hardware-level |
| OS | Shared kernel | Full OS |
| Performance | Near-native | Overhead |
Best Practice
Use
.dockerignoreto exclude unnecessary files from your build context, similar to.gitignorefor Git Version Control.
Real-world Usage
My Setup
I use Docker for:
- Quartz Blog Setup — Local development environment
- Smart Home Automation — Running home services
- Machine Learning Intro — GPU-enabled ML containers
*Tags: docker devops containers deployment