Skip to content
image-management logo

Docker 镜像管理

image-management

Docker 镜像管理

SKILL.md

Full skill instructions

Docker 镜像管理

概述

镜像构建、多阶段构建、镜像优化等技能。

镜像操作

查看镜像

# 列出镜像
docker images
docker images -a                    # 包含中间层
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"

# 镜像详情
docker inspect image_name
docker history image_name           # 构建历史

# 搜索镜像
docker search nginx

拉取与推送

# 拉取镜像
docker pull nginx
docker pull nginx:1.20
docker pull registry.example.com/​myapp:latest

# 推送镜像
docker push myrepo/​myimage:tag

# 登录仓库
docker login
docker login registry.example.com
docker logout

镜像标签

# 添加标签
docker tag source_image:tag target_image:tag
docker tag myapp:latest myrepo/​myapp:v1.0

# 删除镜像
docker rmi image_name
docker rmi -f image_name            # 强制删除
docker image prune                  # 删除悬空镜像
docker image prune -a               # 删除未使用镜像

导入导出

# 导出镜像
docker save -o myimage.tar myimage:tag
docker save myimage:tag | gzip > myimage.tar.gz

# 导入镜像
docker load -i myimage.tar
docker load < myimage.tar.gz

镜像构建

基础构建

# 构建镜像
docker build -t myimage:tag .
docker build -t myimage:tag -f Dockerfile.prod .

# 指定构建参数
docker build --build-arg VERSION=1.0 -t myimage:tag .

# 不使用缓存
docker build --no-cache -t myimage:tag .

# 指定目标阶段
docker build --target builder -t myimage:builder .

Dockerfile 基础

# 基础镜像
FROM node:18-alpine

# 元数据
LABEL maintainer="[email protected]"
LABEL version="1.0"

# 设置工作目录
WORKDIR /​app

# 复制文件
COPY package*.json ./
COPY . .

# 运行命令
RUN npm install

# 环境变量
ENV NODE_ENV=production
ENV PORT=3000

# 暴露端口
EXPOSE 3000

# 启动命令
CMD ["node", "app.js"]

多阶段构建

# 构建阶段
FROM node:18-alpine AS builder
WORKDIR /​app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

# 生产阶段
FROM node:18-alpine AS production
WORKDIR /​app
COPY --from=builder /​app/​dist ./​dist
COPY --from=builder /​app/​node_modules ./​node_modules
EXPOSE 3000
CMD ["node", "dist/​main.js"]

Go 应用多阶段构建

# 构建阶段
FROM golang:1.21-alpine AS builder
WORKDIR /​app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o main .

# 生产阶段
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /​root/
COPY --from=builder /​app/​main .
EXPOSE 8080
CMD ["./​main"]

镜像优化

减小镜像体积

# 1. 使用精简基础镜像
FROM alpine:latest
FROM node:18-alpine
FROM python:3.11-slim

# 2. 合并 RUN 命令
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        package1 \
        package2 && \
    rm -rf /​var/​lib/​apt/​lists/​*

# 3. 使用 .dockerignore
# .dockerignore 文件
node_modules
.git
*.md
Dockerfile
.dockerignore

# 4. 多阶段构建只复制必要文件
COPY --from=builder /​app/​dist ./​dist

构建缓存优化

# 先复制依赖文件,利用缓存
COPY package*.json ./
RUN npm ci

# 再复制源代码
COPY . .
RUN npm run build

安全最佳实践

# 1. 使用非 root 用户
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
USER appuser

# 2. 固定版本
FROM node:18.17.0-alpine3.18

# 3. 扫描漏洞
# docker scan myimage:tag

# 4. 最小权限
RUN chmod 500 /​app/​main

私有仓库

搭建私有仓库

# 运行 Registry
docker run -d -p 5000:5000 --name registry registry:2

# 推送到私有仓库
docker tag myimage:tag localhost:5000/​myimage:tag
docker push localhost:5000/​myimage:tag

# 拉取
docker pull localhost:5000/​myimage:tag

配置认证

# 创建密码文件
htpasswd -Bc /​auth/​htpasswd admin

# 运行带认证的 Registry
docker run -d -p 5000:5000 \
  -v /​auth:/​auth \
  -e "REGISTRY_AUTH=htpasswd" \
  -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
  -e "REGISTRY_AUTH_HTPASSWD_PATH=/​auth/​htpasswd" \
  registry:2

常见场景

场景 1:分析镜像层

# 查看镜像层
docker history myimage:tag --no-trunc

# 使用 dive 分析
docker run --rm -it \
  -v /​var/​run/​docker.sock:/​var/​run/​docker.sock \
  wagoodman/​dive:latest myimage:tag

场景 2:清理磁盘空间

# 查看磁盘使用
docker system df
docker system df -v

# 清理未使用资源
docker system prune              # 清理悬空资源
docker system prune -a           # 清理所有未使用资源
docker system prune --volumes    # 包括卷

场景 3:镜像漏洞扫描

# Docker Scout
docker scout cves myimage:tag

# Trivy
docker run --rm \
  -v /​var/​run/​docker.sock:/​var/​run/​docker.sock \
  aquasec/​trivy image myimage:tag

故障排查

问题排查方法
构建失败检查 Dockerfile 语法、网络
镜像过大使用多阶段构建、精简基础镜像
拉取失败检查网络、认证、镜像名
缓存失效检查 COPY 顺序、文件变更

More skills from majiayu000

xiaohongshu logo
majiayu000/claude-arsenal

xiaohongshu

xiaohongshu

286 148
View
agent-task-conductor logo
majiayu000/claude-skill-registry

agent-task-conductor

Conduct multi-agent task orchestration and workflow coordination.

663 1
View
conductor-setup logo
majiayu000/claude-skill-registry

conductor-setup

Initialize project with Conductor artifacts (product definition,

663 1
View
animation-designer logo
majiayu000/claude-skill-registry

animation-designer

Expert in web animations, transitions, and motion design using Framer Motion and CSS

663 1
View
diagramming logo
majiayu000/claude-skill-registry

diagramming

Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...

663 1
View
h3-pg logo
majiayu000/claude-skill-registry-data

h3-pg

PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.

23 1
View
conductor-development logo
majiayu000/claude-skill-registry

conductor-development

Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...

663 1
View
conductor-status logo
majiayu000/claude-skill-registry

conductor-status

Display project status, active tracks, and next actions

663 1
View
dockerization logo
majiayu000/claude-skill-registry

dockerization

Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.

663 1
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View