mcpField
D
docker-optimization

docker-optimization

github.com/pluginagentmarketplaceCloud & DevOps273 installsUpdated 43w ago
Repository

About docker-optimization

A skill for developers optimizing Docker images and containers for size, build speed, and runtime performance. It provides strategies, checklists, and analysis commands for base images, caching, multi-stage builds, and resource limits.

Key features

  1. 01Base image selection
  2. 02Layer and cache optimization
  3. 03Multi-stage build guidance
  4. 04Runtime resource limits
  5. 05Image and layer analysis commands

Use cases

  1. 01Reduce container image size
  2. 02Improve Docker build times
  3. 03Set container CPU and memory limits
  4. 04Investigate image layers and cache issues

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/pluginagentmarketplace/custom-plugin-docker --skill docker-optimization

After installing, type/docker-optimizationin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

assets/
config.yaml
optimization-tips.yaml
schema.json
references/
GUIDE.md
PATTERNS.md
scripts/
validate.py

Details

From the project README

Docker Optimization Skill

Comprehensive optimization techniques for Docker images and containers covering size reduction, build caching, and runtime performance.

Purpose

Reduce image size, improve build times, and optimize container performance using 2024-2025 industry best practices.

Parameters

ParameterTypeRequiredDefaultDescription
targetenumNoallsize/build/runtime/all
base_imagestringNo-Current base image
current_sizestringNo-Current image size

Optimization Strategies

1. Image Size Reduction
Base Image Selection
BaseSizeUse Case
scratch0Static Go/Rust binaries
distroless2-20MBProduction containers
alpine5MBGeneral purpose
slim80-150MBWhen apt packages needed
full500MB+Development only
# Before: 1.2GB
FROM node:20

# After: 150MB (88% smaller)
FROM node:20-alpine
Layer Optimization
# Bad: 3 layers, 150MB
RUN apt-get update
RUN apt-get install -y curl
RUN rm -rf /var/lib/apt/lists/*

# Good: 1 layer, 50MB
RUN apt-get update && \
    apt-get install -y --no-install-recommends curl && \
    rm -rf /var/lib/apt/lists/*
Remove Unnecessary Files
# Clean package manager cache
RUN npm cache clean --force
RUN pip cache purge
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Use .dockerignore
# node_modules, .git, *.md, tests/, docs/
2. Build Speed Optimization
Layer Caching Strategy
# Dependencies first (rarely change)
COPY package*.json ./
RUN npm ci

# Source code last (frequently changes)
COPY . .
RUN npm run build
BuildKit Cache Mounts
# syntax=docker/dockerfile:1
FROM node:20-alpine

# Cache npm packages
RUN --mount=type=cache,target=/root/.npm \
    npm ci

# Cache pip packages
RUN --mount=type=cache,target=/root/.cache/pip \
    pip install -r requirements.txt
Parallel Multi-Stage
# Parallel stages (BuildKit)
FROM node:20-alpine AS deps
COPY package*.json ./
RUN npm ci

FROM deps AS builder
COPY . .
RUN npm run build

FROM deps AS linter
COPY . .
RUN npm run lint
3. Runtime Performance
Resource Limits
services:
  app:
    deploy:
      resources:
        limits:
          cpus: '1'
          memory: 512M
        reservations:
          cpus: '0.5'
          memory: 256M
Container Tuning
# Set memory limits
docker run --memory=512m --memory-swap=512m app

# CPU limits
docker run --cpus=1.5 app

# I/O limits
docker run --device-read-bps=/dev/sda:1mb app

Optimization Checklist

Size Checklist
  • Using smallest viable base image?
  • Multi-stage build implemented?
  • Package manager cache cleaned?
  • Dev dependencies excluded?
  • .dockerignore configured?
Build Speed Checklist
  • Dependencies copied before code?
  • BuildKit enabled?
  • Cache mounts used?
  • Parallel stages where possible?
Runtime Checklist
  • Resource limits set?
  • Health checks configured?
  • Non-root user?
  • Read-only filesystem where possible?

Analysis Tools

# Image size analysis
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"

# Layer analysis
docker history <image> --format "{{.Size}}\t{{.CreatedBy}}"

# Deep dive analysis
dive <image>

# Build time analysis
DOCKER_BUILDKIT=1 docker build --progress=plain .

Error Handling

Common Issues
IssueCauseSolution
Large imageNo multi-stageImplement multi-stage
Slow buildsPoor layer orderDependencies before code
Cache not workingContext changesUse .dockerignore
OOM at runtimeNo limitsSet memory limits

Troubleshooting

Debug Checklist
  • BuildKit enabled? (DOCKER_BUILDKIT=1)
  • Cache being used? (Check build output)
  • .dockerignore working? (Check context size)
  • Layers optimized? (Run dive)

Usage

Skill("docker-optimization")

Related Skills

  • docker-multi-stage
  • dockerfile-basics
  • docker-production

Comments

Markdown supported