Resources

100 Docker & Containers resources for developers

This resource guide provides actionable patterns and tools for optimizing Docker workflows, focusing on building slim, secure images and managing complex local development environments with Docker Compose.

Dockerfile Optimization and Build Patterns

  1. 1

    Implement Multi-Stage Builds

    beginnerhigh

    Separate build-time dependencies from the final runtime image using 'FROM ... AS build' and 'COPY --from=build' to reduce image size by up to 90%.

  2. 2

    BuildKit Cache Mounts

    intermediatehigh

    Use 'RUN --mount=type=cache,target=/root/.npm' to persist package manager caches between builds, significantly reducing CI/CD build times.

  3. 3

    Distroless Runtime Images

    advancedhigh

    Use GoogleContainerTools/distroless as the final stage base to remove shells and package managers, minimizing the attack surface.

  4. 4

    Layer Ordering for Caching

    beginnerstandard

    Copy dependency files (package.json, go.mod) and run install commands before copying the rest of the source code to maximize layer reuse.

  5. 5

    Hadolint for Linting

    beginnermedium

    Integrate Hadolint into your CI pipeline to enforce Dockerfile best practices and catch inefficient instructions like 'RUN apt-get upgrade'.

  6. 6

    Non-Root User Configuration

    intermediatehigh

    Explicitly create a system user and use the 'USER' instruction to avoid running container processes as root, preventing container breakout exploits.

  7. 7

    Tini Init Process

    intermediatestandard

    Use 'tini' as your entrypoint to correctly handle signal forwarding and reap zombie processes in containers running complex applications.

  8. 8

    .dockerignore Optimization

    beginnermedium

    Exclude .git, node_modules, and local build artifacts to prevent sending unnecessary context to the Docker daemon and bloating layers.

  9. 9

    Specific Tag Versioning

    beginnerhigh

    Avoid the ':latest' tag; use specific semantic versions or SHA256 hashes for base images to ensure build reproducibility across environments.

  10. 10

    Dive Layer Inspection

    intermediatemedium

    Use the 'dive' CLI tool to analyze image layers and identify wasted space or files that should have been excluded from the final image.

Docker Compose and Local Development

  1. 1

    Healthcheck-based Dependencies

    intermediatehigh

    Use 'depends_on' with 'condition: service_healthy' to ensure the application only starts after the database is ready to accept connections.

  2. 2

    Docker Compose Profiles

    intermediatemedium

    Define 'profiles' in your YAML to selectively start services, such as 'debug' or 'testing' tools, without cluttering the default stack.

  3. 3

    Bind Mounts for Hot Reload

    beginnerhigh

    Map local source directories to container paths using volumes to enable live-reloading frameworks (like Vite or Nodemon) without rebuilding.

  4. 4

    Environment Variable Files

    beginnerstandard

    Utilize '.env' files alongside 'env_file' directives to manage secrets and configurations locally without hardcoding values in the YAML.

  5. 5

    External Network Isolation

    intermediatemedium

    Define external networks to allow communication between separate Compose projects while keeping database traffic isolated from the public bridge.

  6. 6

    Resource Constraints

    intermediatestandard

    Apply 'deploy.resources.limits' in Compose files to simulate production constraints (CPU/Memory) and prevent local resource exhaustion.

  7. 7

    Traefik Reverse Proxy Integration

    advancedhigh

    Run Traefik in a container to provide automatic SSL and local domain routing (e.g., app.localhost) for multiple Compose services.

  8. 8

    Docker Compose Overrides

    beginnermedium

    Use 'docker-compose.override.yml' for local-only settings like debug ports, keeping the base YAML clean for production-like environments.

  9. 9

    Named Volumes for Persistence

    beginnerhigh

    Use named volumes instead of host paths for database data to improve I/O performance on macOS and Windows via VirtioFS.

  10. 10

    Lazydocker TUI

    beginnermedium

    Manage containers, logs, and resource usage from the terminal using 'lazydocker' for faster troubleshooting than the standard CLI.

Container Security and Operations

  1. 1

    Trivy Vulnerability Scanning

    intermediatehigh

    Run 'trivy image <name>' in your CI pipeline to detect CVEs in OS packages and application dependencies before deployment.

  2. 2

    Read-Only Root Filesystem

    advancedhigh

    Set 'read_only: true' in Compose or '--read-only' in CLI to prevent attackers from writing malicious scripts to the container's disk.

  3. 3

    Log Rotation Configuration

    beginnerstandard

    Configure the 'json-file' logging driver with 'max-size' and 'max-file' limits to prevent container logs from consuming all host disk space.

  4. 4

    Seccomp Profile Hardening

    advancedmedium

    Apply custom Seccomp profiles to restrict the system calls a container can make, mitigating kernel-level exploits.

  5. 5

    Docker Scout SBOM

    intermediatemedium

    Generate a Software Bill of Materials (SBOM) using Docker Scout to track exactly which software versions are running in production.

  6. 6

    Automated Image Pruning

    beginnerstandard

    Schedule 'docker system prune -af --volumes' via cron to remove dangling images and unused networks that accumulate on build nodes.

  7. 7

    GHCR Authenticated Pulls

    intermediatehigh

    Configure GitHub Actions to use 'docker/login-action' with temporary GITHUB_TOKEN for secure image pushes to GitHub Container Registry.

  8. 8

    Portainer Environment Management

    beginnermedium

    Deploy Portainer as a lightweight management UI to visualize container health and manage remote Docker engines via an agent.

  9. 9

    Watchtower Auto-Updates

    intermediatestandard

    Use the Watchtower container to automatically pull the latest images and restart containers when a new version is pushed to the registry.

  10. 10

    Container Resource Monitoring

    advancedmedium

    Deploy cAdvisor to collect real-time resource usage and performance characteristics of running containers for Prometheus scraping.