docker-optimization

Optimize Python application Docker images with multi-stage builds and Trivy scanning.

Updated Sep 8, 2025
One-click install
npx skills add https://github.com/randalmurphal/claude-config --skill docker-optimization
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: docker-optimization
Source: https://github.com/randalmurphal/claude-config/tree/main/skills/docker-optimization
Command: npx skills add https://github.com/randalmurphal/claude-config --skill docker-optimization

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Optimize Docker images for Python applications including multi-stage builds (70%+ size reduction), security scanning with Trivy, layer caching, and distroless base images. Use when creating Dockerfiles, reducing image size, improving build performance, or scanning for vulnerabilities.

Core Features & Use Cases

  • Multi-stage Builds: Separate build and runtime to minimize image size.
  • Distroless Runtime: Highest security with no shell.
  • Layer Caching & .dockerignore: Faster builds and smaller contexts.

Quick Start

Start with a multi-stage Dockerfile; use distroless for production builds and run security scans.

Frequently Asked Questions about docker-optimization

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I reduce Python Docker image size with multi-stage builds?

Multi-stage builds separate compilation from runtime, removing build dependencies from the final image. Define a builder stage to compile dependencies, then copy only artifacts into a slim or distroless runtime stage, achieving 70%+ size reduction.

What's the difference between Alpine, slim, and distroless base images for Docker?

Alpine and slim reduce bloat but retain shells and package managers. Distroless removes all non-essential binaries, offering the smallest footprint and highest security—ideal for production Python applications with no shell access needed.

How do I scan Docker images for security vulnerabilities?

Trivy scans Docker images for CVEs and misconfigurations. Integrate it into your CI/CD pipeline post-build to detect vulnerabilities in dependencies and base layers before deployment.

Can I speed up Docker builds with layer caching and .dockerignore?

Layer caching reuses unchanged steps; order Dockerfile commands from least to most frequently changing. Use .dockerignore to exclude unnecessary files from the build context, reducing transfer size and build time.

Why should I use pip no-cache-dir in Python Dockerfiles?

The pip cache persists in layers, increasing image size unnecessarily. Adding --no-cache-dir prevents caching pip artifacts, and combining apt-get clean in the same RUN command removes package manager caches in a single layer.

Does Docker optimization work for development and production workflows?

Yes. Apply multi-stage builds, layer caching, and .dockerignore optimization to all environments. Use distroless and security scanning in production; development images can retain shells for debugging while maintaining the same build efficiency.