Communitygithub.com

Docker 最佳化工具

審查 Dockerfile 以找出最佳實務、安全性問題以及映像檔大小最佳化,包括多階段建置與層級快取。適合處理 Docker、容器或部署相關工作時使用。

Docker 最佳化工具 是什麼?

Docker Optimizer 是一個專為開發者與 AI 代理設計的專業技能,旨在自動分析並最佳化 Dockerfile,涵蓋效能、安全性及最佳實務。它支援 Claude Code、Cursor、Codex 等平台,能自動掃描專案中所有 Dockerfile,套用多階段建置、最小化層級、使用特定基礎映像標籤、非 root 使用者、.dockerignore、健康檢查等準則,同時識別硬編碼密碼等安全漏洞。該技能提供逐步檢查清單與最佳化後的 Dockerfile 範本,並可生成 BuildKit 等高效建置命令,讓開發者輕鬆產出更小、更安全、更快的容器映像。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/ComeOnOliver/skillshub/tree/HEAD/skills/aiskillstore/marketplace/crazydubya/docker-optimizer

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

Docker 最佳化工具 是做什麼的?

分析並最佳化 Dockerfile,以提升效能、安全性與最佳實務。

使用時機

  • 使用者正在處理 Docker 或容器
  • 需要最佳化 Dockerfile
  • 容器映像檔過大
  • 使用者提及「Docker」、「容器」、「映像檔大小」或「部署」

操作說明

1. 尋找 Dockerfile

搜尋:Dockerfile、Dockerfile.*、*.dockerfile

2. 檢查最佳實務

使用特定的基礎映像版本:

# 錯誤
FROM node:latest

# 正確
FROM node:18-alpine

最小化層級:

# 錯誤
RUN apt-get update
RUN apt-get install -y curl
RUN apt-get install -y git

# 正確
RUN apt-get update && \
    apt-get install -y curl git && \
    rm -rf /var/lib/apt/lists/*

按變更頻率排序指令:

# 相依項目變更次數少於程式碼
COPY package*.json ./
RUN npm install
COPY . .

使用 .dockerignore:

node_modules
.git
.env
*.md

3. 多階段建置

減少最終映像檔大小:

# 建置階段
FROM node:18 AS build
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

# 執行階段
FROM node:18-alpine
WORKDIR /app
COPY --from=build /app/dist ./dist
COPY --from=build /app/node_modules ./node_modules
CMD ["node", "dist/index.js"]

4. 安全性問題

不要以 root 執行:

RUN addgroup -S appgroup && adduser -S appuser -G appgroup
USER appuser

映像檔中不包含機密:

# 錯誤:硬編碼機密
ENV API_KEY=secret123

# 正確:使用建置引數或執行時期環境變數
ARG BUILD_ENV
ENV NODE_ENV=${BUILD_ENV}

掃描漏洞:

docker scan image:tag
trivy image image:tag

5. 大小最佳化

使用 Alpine 映像:

  • node:18-alpine vs node:18(900MB → 170MB)
  • python:3.11-alpine vs python:3.11(900MB → 50MB)

移除不必要的檔案:

RUN npm install --production && \
    npm cache clean --force

使用特定的 COPY:

# 錯誤:複製所有內容
COPY . .

# 正確:只複製需要的內容
COPY package*.json ./
COPY src ./src

6. 快取策略

層級快取最佳化:

# 先安裝相依項目(如果 package.json 未變更則快取)
COPY package*.json ./
RUN npm install

# 複製原始碼(變更頻率較高)
COPY . .
RUN npm run build

7. 健康檢查

HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD node healthcheck.js

8. 生成最佳化的 Dockerfile

提供改良版本,包含:

  • 多階段建置
  • 適當的基礎映像
  • 安全性改善
  • 層級最佳化
  • 建置快取
  • .dockerignore 檔案

9. 建置命令

高效建置:

# 使用 BuildKit
DOCKER_BUILDKIT=1 docker build -t app:latest .

# 從登錄檔快取建置
docker build --cache-from myregistry/app:latest -t app:latest .

10. Dockerfile 檢查清單

  • 特定的基礎映像標籤(非 latest)
  • 是否適用多階段建置
  • 非 root 使用者
  • 最少層級(合併 RUN 命令)
  • .dockerignore 存在
  • 映像檔中無機密
  • 正確的層級排序以利快取
  • 使用 Alpine 或 slim 變體
  • 在相同 RUN 層級中清理
  • 已定義 HEALTHCHECK

安全性最佳實務

  • 定期掃描映像
  • 使用官方基礎映像
  • 保持基礎映像更新
  • 最小化攻擊面(較少的套件)
  • 以非 root 使用者執行
  • 盡可能使用唯讀檔案系統

支援檔案

  • templates/Dockerfile.optimized:最佳化的多階段 Dockerfile 範例
  • templates/.dockerignore:常見的 .dockerignore 模式

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

ComeOnOliver/skillshub

Create stunning, animation-rich HTML presentations from scratch or by converting PowerPoint files. Use when the user wants to build a presentation, convert a PPT/PPTX to web, or create slides for a talk/pitch. Helps non-designers discover their aesthetic through visual exploration rather than abstract choices.

ComeOnOliver/skillshub

Next.js 16+ 和 Turbopack — 增量打包、文件系统缓存、开发速度,以及何时使用 Turbopack 与 webpack。

ComeOnOliver/skillshub

SwiftUI architecture patterns, state management with @Observable, view composition, navigation, performance optimization, and modern iOS/macOS UI best practices.

ComeOnOliver/skillshub

See, Understand, Act on video and audio. See- ingest from local files, URLs, RTSP/live feeds, or live record desktop; return realtime context and playable stream links. Understand- extract frames, build visual/semantic/temporal indexes, and search moments with timestamps and auto-clips. Act- transcode and normalize (codec, fps, resolution, aspect ratio), perform timeline edits (subtitles, text/image overlays, branding, audio overlays, dubbing, translation), generate media assets (image, audio, video), and create real time alerts for events from live streams or desktop capture.

ComeOnOliver/skillshub

AI-assisted video editing workflows for cutting, structuring, and augmenting real footage. Covers the full pipeline from raw capture through FFmpeg, Remotion, ElevenLabs, fal.ai, and final polish in Descript or CapCut. Use when the user wants to edit video, cut footage, create vlogs, or build video content.

ComeOnOliver/skillshub

Next.js 15 애플리케이션을 위한 프론트엔드 개발 가이드라인. React 19, TypeScript, Shadcn/ui, Tailwind CSS를 사용한 모던 패턴. Server Components, Client Components, App Router, 파일 구조, Shadcn/ui 컴포넌트, 성능 최적화, TypeScript 모범 사례 포함. 컴포넌트, 페이지, 기능 생성, 데이터 페칭, 스타일링, 라우팅, 프론트엔드 코드 작업 시 사용.

ComeOnOliver/skillshub

Provides Tamagui patterns for config v4, compiler optimization, styled context, and cross-platform styling. Must use when working with Tamagui projects (tamagui.config.ts, @tamagui imports).

ComeOnOliver/skillshub

Create distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.

ComeOnOliver/skillshub

Create high-converting, visually distinctive landing pages. Use when building marketing pages, product launches, SaaS homepages, or any single-page conversion-focused website. Guides section-by-section composition with anti-AI-slop principles.

ComeOnOliver/skillshub

Auto-loaded context for Portfolio Buddy 2 development. Use for ANY task involving: React 19 development, TypeScript, portfolio analysis features, metrics calculations, trading strategy comparison, or working with the Portfolio Buddy 2 codebase. Contains tech stack, known issues, and architectural constraints.

ComeOnOliver/skillshub

Next.js development tooling via MCP. Inspect routes, components, build info, and debug Next.js apps. Use when working on Next.js applications, debugging routing, or inspecting app structure. NOT for general React or non-Next.js projects.

ComeOnOliver/skillshub

Implement UI using Shadcn MCP (atoms/theme) and 21st.dev MCP (complex sections). Use when adding buttons, layouts, or generating landing pages.

ComeOnOliver/skillshub

A conceptual skill for building an API client in Next.js that handles JWT tokens

ComeOnOliver/skillshub

Best practices and patterns for Next.js App Router, Server Actions, and Routing in this project.

ComeOnOliver/skillshub

The Design System, Theme, and UX rules for the Physical AI Hub.

ComeOnOliver/skillshub

Comprehensive frontend development skill for building modern, performant web applications using ReactJS, NextJS, TypeScript, Tailwind CSS. Includes component scaffolding, performance optimization, bundle analysis, and UI best practices. Use when developing frontend features, optimizing performance, implementing UI/UX designs, managing state, or reviewing frontend code.

ComeOnOliver/skillshub

This document captures learnings from fixing keyboard navigation issues when floating components (Select, DropdownMenu, Popover) have CSS open/close animations.

ComeOnOliver/skillshub

Build Next.js 16 applications with correct patterns and distinctive design. Use when creating pages, layouts, dynamic routes, upgrading from Next.js 15, or implementing proxy.ts. Covers breaking changes (async params/searchParams, Turbopack, cacheComponents) and frontend aesthetics. NOT when building non-React or backend-only applications.

ComeOnOliver/skillshub

Comprehensive Framer Motion animation library for React. Covers motion components, variants, gestures, page transitions, and scroll animations. Use when adding animations to React/Next.js applications.

ComeOnOliver/skillshub

Build interfaces that resonate, not just render. This skill enforces bold, intentional

相關技能