docker_optimization

Optimizes Docker images using multi-stage builds, layer caching, and security hardening patterns.

Updated Jan 14, 2026
One-click install
npx skills add https://github.com/jvsandhu/agentic-skills --skill docker-optimization-jvsandhu
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: docker_optimization
Source: https://github.com/jvsandhu/agentic-skills/tree/main/skills/docker_optimization
Command: npx skills add https://github.com/jvsandhu/agentic-skills --skill docker-optimization-jvsandhu

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Docker images often ship bloated with build tools, caches, and secrets, resulting in slow builds, large deployments, and security risks. This Skill provides proven patterns to reduce image sizes by 70-90% and build times by 50-80% while hardening containers for production. ## Core Features & Use Cases - Multi-Stage Build Patterns: Separate build and runtime stages to strip compilers, package caches, and dev dependencies from final images. - Layer Caching & BuildKit: Order Dockerfile instructions by change frequency, use .dockerignore, and leverage BuildKit cache mounts for faster rebuilds. - Security Hardening: Run as non-root users, pin exact image versions, exclude secrets, and scan images with Trivy or Docker Scout. - Use Case: A Python FastAPI app with a 1.2GB image and 5-minute builds is restructured into a 140MB multi-stage image with 2-minute builds, non-root execution, and preserved dependency caching. ## Quick Start Ask the agent to optimize your Dockerfile for size, build speed, and security using the docker optimization patterns.

Frequently Asked Questions about docker_optimization

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

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

Use a builder stage with full tooling to compile dependencies, then copy only the artifacts into a minimal runtime stage like python:3.11-slim or node:20-alpine. This typically reduces images by 70-90%, for example from 800MB to 120MB for a Python app.

How to speed up Docker builds with layer caching?

Order Dockerfile instructions from least to most frequently changing: copy dependency files like requirements.txt before application code so dependency layers stay cached. Enable BuildKit and use cache mounts such as --mount=type=cache for package manager downloads.

Should I use alpine or slim base images for Docker?

Slim images like python:3.11-slim are the default choice with good compatibility at around 130MB. Alpine images are smaller at roughly 50MB but can cause glibc-related issues with some compiled dependencies.

How do I run a Docker container as a non-root user?

Create a dedicated user with useradd or adduser in the Dockerfile, set ownership of app files, and switch with the USER instruction before CMD. This prevents processes inside the container from running with root privileges.

Why does my Docker build reinstall dependencies on every code change?

This happens when COPY . . appears before the dependency install step, invalidating the cache on any file change. Copy only dependency manifests first, run the install, then copy source code afterward.

How do I scan Docker images for vulnerabilities?

Run docker scout cves, trivy image, or snyk container test against your built image to list known CVEs. Fix critical findings by updating base images and pinned package versions before deployment.