Loading...
Loading...
Optimize Docker images and containers for size, build speed, and runtime performance
npx skill4agent add pluginagentmarketplace/custom-plugin-docker docker-optimization| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
| target | enum | No | all | size/build/runtime/all |
| base_image | string | No | - | Current base image |
| current_size | string | No | - | Current image size |
| Base | Size | Use Case |
|---|---|---|
| scratch | 0 | Static Go/Rust binaries |
| distroless | 2-20MB | Production containers |
| alpine | 5MB | General purpose |
| slim | 80-150MB | When apt packages needed |
| full | 500MB+ | Development only |
# Before: 1.2GB
FROM node:20
# After: 150MB (88% smaller)
FROM node:20-alpine# 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/*# 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/# Dependencies first (rarely change)
COPY package*.json ./
RUN npm ci
# Source code last (frequently changes)
COPY . .
RUN npm run build# syntax=docker/dockerfile:1
FROM node:20-alpine
# Cache npm packages
RUN \
npm ci
# Cache pip packages
RUN \
pip install -r requirements.txt# 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 lintservices:
app:
deploy:
resources:
limits:
cpus: '1'
memory: 512M
reservations:
cpus: '0.5'
memory: 256M# 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# 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 .| Issue | Cause | Solution |
|---|---|---|
| Large image | No multi-stage | Implement multi-stage |
| Slow builds | Poor layer order | Dependencies before code |
| Cache not working | Context changes | Use .dockerignore |
| OOM at runtime | No limits | Set memory limits |
DOCKER_BUILDKIT=1diveSkill("docker-optimization")