optimize-docker-image-size

A guide on reducing Docker image size for PHP projects. Covers strategies such as using Alpine images, multi-stage builds, cleaning image layers, and minimizing dependencies to optimize image size.

Last repository push
GitHub Stars
109
Forks
26
Issues / PRs
1

Installation

Review the source and dependencies with a prompt. A package is available to download.

Install with an AI assistant

Copy the prompt so your assistant can review the pinned revision and dependencies first.

View promptHide prompt
Check whether SkillPI Skill Finder is installed for your current agent. If it is missing, install and enable it using the official instructions at https://github.com/Findskills/skillpi. Then use Skill Finder to locate and install "optimize-docker-image-size" from this exact SkillPI detail page: https://skillpi.com/skills/deepskill-73c063526c521e2a3ac6d502958cb41c505e36eef1a84db9dd911efa6d9a0cf3. Verify the upstream source and version against https://github.com/dykyi-roman/awesome-claude-code before installing. If they do not match, stop and report the mismatch. Report the installed path and source version.

Source and setup

github.com

Setup guidance pending

See the original source for supported tools, dependencies, and permissions.

Installation test pending

A guide on reducing Docker image size for PHP projects. Covers strategies such as using Alpine images, multi-stage builds, cleaning image layers, and minimizing dependencies to optimize image size.

0 Downloads

Introduction source: third_party · dykyi-roman · 0.0.1 Reference

SkillPi interpretation

Tasks
Optimizes Docker image size for PHP projects. Reduces image footprint through Alpine, multi-stage builds, layer cleanup, and dependency minimization.
Testing
Not tested

Browse files

Browse files at the source ↗

Original SKILL.md

Open original ↗
Frontmatter
name: optimize-docker-image-size
description: Optimizes Docker image size for PHP projects. Reduces image footprint through Alpine, multi-stage builds, layer cleanup, and dependency minimization.

Docker Image Size Optimization

Analyzes Docker images and provides actionable recommendations to reduce footprint for PHP projects.

Image Size Anatomy

┌─────────────────────────────────────────────────────────────────┐
│              TYPICAL PHP IMAGE SIZE BREAKDOWN                    │
├─────────────────────────────────────────────────────────────────┤
│  Base OS (Debian):   ████████████████████░░░░░  150MB (33%)    │
│  PHP runtime:        ████████░░░░░░░░░░░░░░░░░   50MB (11%)   │
│  PHP extensions:     ██████░░░░░░░░░░░░░░░░░░░   40MB  (9%)   │
│  Build dependencies: ████████████░░░░░░░░░░░░░   80MB (18%)   │
│  Composer vendor:    ████████████░░░░░░░░░░░░░   80MB (18%)   │
│  Application source: ████░░░░░░░░░░░░░░░░░░░░░   20MB  (4%)   │
│  Misc (logs, cache): ████░░░░░░░░░░░░░░░░░░░░░   30MB  (7%)   │
│  Total unoptimized:  █████████████████████████  ~450MB          │
│  Target optimized:   ████████████░░░░░░░░░░░░░  ~120MB (-73%) │
└─────────────────────────────────────────────────────────────────┘

Size Estimation by Component

Component Debian Alpine Savings
Base OS 150MB 8MB -142MB
PHP runtime 50MB 35MB -15MB
PHP extensions (5 typical) 40MB 25MB -15MB
Build dependencies 80MB 0MB (multi-stage) -80MB
Vendor (with dev) 80MB 30MB (--no-dev) -50MB
Application source 20MB 10MB (.dockerignore) -10MB
Total ~450MB ~108MB -342MB

Alpine vs Debian

# ❌ Debian-based: ~150MB base → Final: 400-500MB
FROM php:8.4-fpm

# ✅ Alpine-based: ~8MB base → Final: 80-150MB
FROM php:8.4-fpm-alpine
Concern Solution
musl vs glibc Most PHP extensions work fine with musl
Missing packages Use apk search to find Alpine equivalents
DNS resolution Add gnu-libc-compat if needed

Multi-Stage Builds: Production-Only Artifacts

# Stage 1: Build with all tools
FROM php:8.4-cli-alpine AS builder
RUN apk add --no-cache --virtual .build-deps $PHPIZE_DEPS libzip-dev icu-dev \
    && docker-php-ext-install pdo_mysql intl zip
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
COPY composer.json composer.lock ./
RUN composer install --no-dev --prefer-dist --optimize-autoloader
COPY . .

# Stage 2: Minimal production image
FROM php:8.4-fpm-alpine AS production
RUN apk add --no-cache libzip icu-libs
COPY --from=builder /usr/local/lib/php/extensions/ /usr/local/lib/php/extensions/
COPY --from=builder /usr/local/etc/php/conf.d/ /usr/local/etc/php/conf.d/
COPY --from=builder /app/vendor /app/vendor
COPY --from=builder /app/src /app/src
COPY --from=builder /app/public /app/public
COPY --from=builder /app/config /app/config

Build Dependencies Cleanup

# ❌ BAD: Build deps remain in image (+80MB)
RUN apk add $PHPIZE_DEPS libzip-dev icu-dev \
    && docker-php-ext-install pdo_mysql intl zip

# ✅ GOOD: Virtual package for clean removal
RUN apk add --no-cache --virtual .build-deps \
        $PHPIZE_DEPS libzip-dev icu-dev libpng-dev \
    && docker-php-ext-install pdo_mysql intl zip gd \
    && apk del .build-deps \
    && apk add --no-cache libzip icu-libs libpng

Debian equivalent:

RUN apt-get update \
    && apt-get install -y --no-install-recommends libzip-dev libicu-dev \
    && docker-php-ext-install pdo_mysql intl zip \
    && apt-get purge -y --auto-remove libzip-dev libicu-dev \
    && rm -rf /var/lib/apt/lists/*

Composer Dependency Minimization

# ❌ BAD: Includes dev deps (+50MB: phpunit, phpstan, faker)
RUN composer install

# ✅ GOOD: Production only
RUN composer install --no-dev --prefer-dist --optimize-autoloader \
    --classmap-authoritative --no-scripts

Image Analysis Tools

# Dive: interactive layer analysis
dive app:latest

# CI mode: fail if wasted space > 10%
CI=true dive app:latest --highestWastedBytes 10MB

# Docker history: show layer sizes
docker history --human --no-trunc app:latest

Specific Savings Per Technique

Technique Typical Savings Effort
Switch to Alpine 120-200MB Low
Multi-stage build 50-150MB Medium
Remove build deps (.build-deps) 40-80MB Low
Composer --no-dev 30-60MB Low
.dockerignore 10-50MB Low
Remove unused system packages 10-30MB Low
Optimize COPY (specific paths) 5-20MB Low
Strip debug symbols 5-15MB Low

Before/After Comparison

Metric Unoptimized Optimized Improvement
Base image php:8.4-fpm (150MB) php:8.4-fpm-alpine (8MB) -142MB
Build deps Included (80MB) Multi-stage removed -80MB
Vendor With dev (80MB) --no-dev (30MB) -50MB
Source Full repo (20MB) .dockerignore (10MB) -10MB
Total ~450MB ~108MB -76%

Generation Instructions

  1. Analyze current image: Run docker history and dive to identify large layers
  2. Identify waste: Build deps, dev packages, unnecessary files
  3. Apply techniques: Alpine base, multi-stage, cleanup, --no-dev
  4. Measure result: Compare before/after with docker images
  5. Validate runtime: Ensure application works correctly with minimal image

Current listing version

0.0.1

Verify the source revision ↗

Quality assessment

Out of 100 · Assessment report from SkillPi LAB.

Scenario Value92/100

Highlights

  • 解决PHP项目Docker镜像优化的核心痛点,直接影响部署效率和云成本
  • 提供量化指标和具体技术方案,具有高度实用价值
  • 覆盖多种优化技术,从基础镜像选择到构建流程优化
Trigger Precision88/100

Highlights

  • 触发词精准指向PHP项目Docker镜像优化场景
  • description清晰明确,覆盖多种优化技术变体
  • 边界清晰,不易与其他Skill重叠
Structure and Standards95/100

Highlights

  • 结构完整,包含问题分析、技术方案、代码示例和验证方法
  • 提供端到端示例和量化指标,便于理解和实施
  • frontmatter完整,文档组织清晰
Execution Reliability90/100

Highlights

  • 提供多种优化技术的具体实现方案
  • 包含验证方法和工具推荐,确保优化效果可测量
  • 覆盖主要异常情况和最佳实践
Evidence Verifiability (mean of the first four)91/100

The arithmetic mean of the first four original scores, rounded to a whole number.

Static text checks

100 / 100 · skillpi-20261123-no-r3-v1

No current rules matched the text.

This score reflects rule matches in the text and is provided for reference before use. It excludes attachments and runtime behavior.

Permissions and checks before use

Automatically summarized from this main document. Matches may be examples or defensive advice; they are not actual permission declarations. Attachments, external dependencies and runtime behavior are not covered.

  • Delete files

    Confirm the absolute path and scope and keep a recoverable backup. Do not run example commands against important directories.

    Mentioned in main document · lines 101
  • Install or fetch external dependencies

    Use official sources and pin versions. Check install scripts in an isolated environment. This document check does not cover code downloaded later.

    Mentioned in main document · lines 98

These notices grant no permissions. Your host tool must still confirm file changes, secrets, external transfers or costs as needed.

Security rating and access scope

safe · Static evidence review; not a runtime safety guarantee.

Before use, confirm and authorize actual file access, external services and network destinations in your host. This review grants no runtime permissions.