Docker Cheat Sheet

Dockerfile and docker-compose.yml examples for common setups.

📄 Dockerfile Examples

Node.js App

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

Python Flask

FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python", "app.py"]

Multi-stage Build

# Build stage
FROM node:18-alpine AS builder
WORKDIR /app
COPY . .
RUN npm run build

# Production stage
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html

🐳 docker-compose.yml Examples

Web + Database

version: '3.8'
services:
  web:
    build: .
    ports:
      - "3000:3000"
    depends_on:
      - db
  db:
    image: postgres:14
    environment:
      POSTGRES_PASSWORD: secret
    volumes:
      - db-data:/var/lib/postgresql/data
volumes:
  db-data:

Docker Configuration Examples

Dockerfiles and docker-compose.yml files are essential for containerizing applications. This cheat sheet provides ready-to-use examples for common scenarios including Node.js, Python, and multi-stage builds.

Dockerfile Best Practices

Use specific base image tags (node:18-alpine not node:latest). Copy package files first to leverage layer caching. Use multi-stage builds to reduce image size. Minimize layer count by combining RUN commands.

Docker Compose

Docker Compose defines multi-container applications. Use depends_on for service dependencies. Define volumes for data persistence. Use environment variables for configuration.