Back to Home
    DockerDocker v24Intermediate

    Docker

    Container management, Dockerfile syntax, docker-compose, networking, and volume management.

    12 min read
    dockercontainersdevopsdeployment

    Container Basics

    1 topic

    Running Containers

    bash
    # Run a container
    docker run <image>
    docker run -d <image>           # Detached mode
    docker run -p 8080:80 <image>   # Port mapping
    docker run --name myapp <image> # Named container
    docker run -it <image> bash     # Interactive shell
    
    # Manage containers
    docker ps                  # List running
    docker ps -a               # List all
    docker stop <container>    # Stop
    docker start <container>   # Start
    docker rm <container>      # Remove
    docker logs <container>    # View logs

    💡 Use -d for background containers

    ⚡ --rm auto-removes container on exit

    Images

    2 topics

    Managing Images

    bash
    # Pull an image
    docker pull <image>:<tag>
    
    # Build from Dockerfile
    docker build -t myapp:latest .
    docker build -f Dockerfile.prod -t myapp:prod .
    
    # List and remove images
    docker images
    docker rmi <image>
    docker image prune     # Remove unused

    💡 Use specific tags, avoid :latest in production

    📌 Multi-stage builds reduce image size

    Dockerfile & Multi-stage Builds

    dockerfile
    # Multi-stage build — keeps final image small
    FROM node:20-alpine AS builder
    WORKDIR /app
    COPY package*.json ./
    RUN npm ci
    COPY . .
    RUN npm run build
    
    # Runtime stage — only what ships to production
    FROM node:20-alpine AS runtime
    WORKDIR /app
    COPY --from=builder /app/dist ./dist
    COPY --from=builder /app/node_modules ./node_modules
    EXPOSE 3000
    CMD ["node", "dist/server.js"]

    ⚡ Multi-stage builds cut image size by 80%+ — build tools never reach production

    💡 Copy package.json before source to cache the npm install layer across rebuilds

    Docker Compose

    1 topic

    Multi-Container Setup

    yaml
    services:
      web:
        build: .
        ports:
          - "3000:3000"
        environment:
          DATABASE_URL: postgres://db:5432/app
        depends_on:
          db:
            condition: service_healthy
    
      db:
        image: postgres:16-alpine
        volumes:
          - pgdata:/var/lib/postgresql/data
        environment:
          POSTGRES_DB: app
          POSTGRES_PASSWORD: secret
        healthcheck:
          test: ["CMD", "pg_isready", "-U", "postgres"]
          interval: 5s
          retries: 5
    
    volumes:
      pgdata:

    💡 condition: service_healthy makes the app wait for DB readiness, not just container start

    ⚡ Named volumes persist data; bind mounts (./src:/app/src) enable dev hot-reload

    Networking & Volumes

    1 topic

    Network Management

    bash
    # Networks
    docker network create mynet
    docker network ls
    docker run --network mynet <image>
    
    # Volumes
    docker volume create mydata
    docker volume ls
    docker run -v mydata:/app/data <image>
    docker run -v $(pwd):/app <image>  # Bind mount

    💡 Containers on same network can resolve by name using the service name

    ⚡ Bind mounts are great for dev; named volumes for persistent data

    Debug & Cleanup

    2 topics

    Inspect & Debug

    bash
    # Inspect container details
    docker inspect <id>
    docker inspect <id> --format '{{.State.Status}}'
    
    # Live resource stats
    docker stats
    
    # Exec into running container
    docker exec -it <id> sh
    docker exec <id> cat /app/config.json
    
    # Copy files to/from container
    docker cp <id>:/app/logs ./logs
    docker cp ./config.json <id>:/app/
    
    # View image layer sizes
    docker history myapp:latest

    💡 docker stats shows live CPU/memory per container — useful for right-sizing resources

    ⚡ docker history reveals which Dockerfile instructions are responsible for large layers

    System Cleanup

    bash
    # Check disk usage
    docker system df
    
    # Remove stopped containers + dangling images + unused networks
    docker system prune
    
    # Also remove all unused images (not just dangling)
    docker system prune -a
    
    # Targeted cleanup
    docker container prune   # Stopped containers
    docker image prune -a    # Unused images
    docker volume prune      # Unused volumes

    ⚠️ system prune --volumes removes data volumes — never run without checking df first

    ⚡ docker system df is the first command to run when a build server is low on disk

    Related Articles

    Background reading and deeper explanations for this sheet.

    Scaling AI APIs with FastAPI, Docker, and Load Balancers: A Practical Guide for Production

    Building an AI API is easy—keeping it fast and reliable under real traffic is the hard part. In this practical guide, you’ll learn how to scale FastAPI services with Docker and load balancers, from local setup to production architecture, with real-world examples, code, and deployment tips.

    keyword overlap

    Build an Agentic App with FastAPI and Azure AI Foundry: A Practical Beginner-to-Pro Guide

    Learn how to design, build, and deploy an agentic application using FastAPI and Azure AI Foundry with practical, real-world examples. This guide walks you from architecture and setup to tool-calling, memory, observability, and production deployment patterns.

    keyword overlap

    GenAI SaaS Architecture: A Practical Blueprint for Building, Scaling, and Securing AI Products

    Designing a SaaS product on top of GenAI is more than calling an LLM API—it requires thoughtful architecture across product, data, safety, and operations. This practical guide walks you through a beginner-friendly yet deep system blueprint, with real-world patterns, code snippets, and deployment strategies you can use immediately.

    keyword overlap

    How to Design a Production-Ready LLM System: A Practical Guide for Real-World Apps

    Building an LLM demo is easy. Running one reliably in production is not. This guide walks you through architecture, retrieval, evaluation, safety, and operations so you can ship LLM systems that are useful, secure, and maintainable at scale.

    keyword overlap