image-management

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Docker 镜像管理

Docker 镜像管理

概述

概述

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

镜像操作

镜像操作

查看镜像

查看镜像

bash
undefined
bash
undefined

列出镜像

列出镜像

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

镜像详情

镜像详情

docker inspect image_name docker history image_name # 构建历史
docker inspect image_name docker history image_name # 构建历史

搜索镜像

搜索镜像

docker search nginx
undefined
docker search nginx
undefined

拉取与推送

拉取与推送

bash
undefined
bash
undefined

拉取镜像

拉取镜像

docker pull nginx docker pull nginx:1.20 docker pull registry.example.com/myapp:latest
docker pull nginx docker pull nginx:1.20 docker pull registry.example.com/myapp:latest

推送镜像

推送镜像

docker push myrepo/myimage:tag
docker push myrepo/myimage:tag

登录仓库

登录仓库

docker login docker login registry.example.com docker logout
undefined
docker login docker login registry.example.com docker logout
undefined

镜像标签

镜像标签

bash
undefined
bash
undefined

添加标签

添加标签

docker tag source_image:tag target_image:tag docker tag myapp:latest myrepo/myapp:v1.0
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 # 删除未使用镜像
undefined
docker rmi image_name docker rmi -f image_name # 强制删除 docker image prune # 删除悬空镜像 docker image prune -a # 删除未使用镜像
undefined

导入导出

导入导出

bash
undefined
bash
undefined

导出镜像

导出镜像

docker save -o myimage.tar myimage:tag docker save myimage:tag | gzip > myimage.tar.gz
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
undefined
docker load -i myimage.tar docker load < myimage.tar.gz
undefined

镜像构建

镜像构建

基础构建

基础构建

bash
undefined
bash
undefined

构建镜像

构建镜像

docker build -t myimage:tag . docker build -t myimage:tag -f Dockerfile.prod .
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 --build-arg VERSION=1.0 -t myimage:tag .

不使用缓存

不使用缓存

docker build --no-cache -t myimage:tag .
docker build --no-cache -t myimage:tag .

指定目标阶段

指定目标阶段

docker build --target builder -t myimage:builder .
undefined
docker build --target builder -t myimage:builder .
undefined

Dockerfile 基础

Dockerfile 基础

dockerfile
undefined
dockerfile
undefined

基础镜像

基础镜像

FROM node:18-alpine
FROM node:18-alpine

元数据

元数据

LABEL maintainer="your@email.com" LABEL version="1.0"
LABEL maintainer="your@email.com" LABEL version="1.0"

设置工作目录

设置工作目录

WORKDIR /app
WORKDIR /app

复制文件

复制文件

COPY package*.json ./ COPY . .
COPY package*.json ./ COPY . .

运行命令

运行命令

RUN npm install
RUN npm install

环境变量

环境变量

ENV NODE_ENV=production ENV PORT=3000
ENV NODE_ENV=production ENV PORT=3000

暴露端口

暴露端口

EXPOSE 3000
EXPOSE 3000

启动命令

启动命令

CMD ["node", "app.js"]
undefined
CMD ["node", "app.js"]
undefined

多阶段构建

多阶段构建

dockerfile
undefined
dockerfile
undefined

构建阶段

构建阶段

FROM node:18-alpine AS builder WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build
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"]
undefined
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"]
undefined

Go 应用多阶段构建

Go 应用多阶段构建

dockerfile
undefined
dockerfile
undefined

构建阶段

构建阶段

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 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"]
undefined
FROM alpine:latest RUN apk --no-cache add ca-certificates WORKDIR /root/ COPY --from=builder /app/main . EXPOSE 8080 CMD ["./main"]
undefined

镜像优化

镜像优化

减小镜像体积

减小镜像体积

dockerfile
undefined
dockerfile
undefined

1. 使用精简基础镜像

1. 使用精简基础镜像

FROM alpine:latest FROM node:18-alpine FROM python:3.11-slim
FROM alpine:latest FROM node:18-alpine FROM python:3.11-slim

2. 合并 RUN 命令

2. 合并 RUN 命令

RUN apt-get update &&
apt-get install -y --no-install-recommends
package1
package2 &&
rm -rf /var/lib/apt/lists/*
RUN apt-get update &&
apt-get install -y --no-install-recommends
package1
package2 &&
rm -rf /var/lib/apt/lists/*

3. 使用 .dockerignore

3. 使用 .dockerignore

.dockerignore 文件

.dockerignore 文件

node_modules .git *.md Dockerfile .dockerignore
node_modules .git *.md Dockerfile .dockerignore

4. 多阶段构建只复制必要文件

4. 多阶段构建只复制必要文件

COPY --from=builder /app/dist ./dist
undefined
COPY --from=builder /app/dist ./dist
undefined

构建缓存优化

构建缓存优化

dockerfile
undefined
dockerfile
undefined

先复制依赖文件,利用缓存

先复制依赖文件,利用缓存

COPY package*.json ./ RUN npm ci
COPY package*.json ./ RUN npm ci

再复制源代码

再复制源代码

COPY . . RUN npm run build
undefined
COPY . . RUN npm run build
undefined

安全最佳实践

安全最佳实践

dockerfile
undefined
dockerfile
undefined

1. 使用非 root 用户

1. 使用非 root 用户

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

2. 固定版本

2. 固定版本

FROM node:18.17.0-alpine3.18
FROM node:18.17.0-alpine3.18

3. 扫描漏洞

3. 扫描漏洞

docker scan myimage:tag

docker scan myimage:tag

4. 最小权限

4. 最小权限

RUN chmod 500 /app/main
undefined
RUN chmod 500 /app/main
undefined

私有仓库

私有仓库

搭建私有仓库

搭建私有仓库

bash
undefined
bash
undefined

运行 Registry

运行 Registry

docker run -d -p 5000:5000 --name registry registry:2
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 tag myimage:tag localhost:5000/myimage:tag docker push localhost:5000/myimage:tag

拉取

拉取

docker pull localhost:5000/myimage:tag
undefined
docker pull localhost:5000/myimage:tag
undefined

配置认证

配置认证

bash
undefined
bash
undefined

创建密码文件

创建密码文件

htpasswd -Bc /auth/htpasswd admin
htpasswd -Bc /auth/htpasswd admin

运行带认证的 Registry

运行带认证的 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
undefined
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
undefined

常见场景

常见场景

场景 1:分析镜像层

场景 1:分析镜像层

bash
undefined
bash
undefined

查看镜像层

查看镜像层

docker history myimage:tag --no-trunc
docker history myimage:tag --no-trunc

使用 dive 分析

使用 dive 分析

docker run --rm -it
-v /var/run/docker.sock:/var/run/docker.sock
wagoodman/dive:latest myimage:tag
undefined
docker run --rm -it
-v /var/run/docker.sock:/var/run/docker.sock
wagoodman/dive:latest myimage:tag
undefined

场景 2:清理磁盘空间

场景 2:清理磁盘空间

bash
undefined
bash
undefined

查看磁盘使用

查看磁盘使用

docker system df docker system df -v
docker system df docker system df -v

清理未使用资源

清理未使用资源

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

场景 3:镜像漏洞扫描

场景 3:镜像漏洞扫描

bash
undefined
bash
undefined

Docker Scout

Docker Scout

docker scout cves myimage:tag
docker scout cves myimage:tag

Trivy

Trivy

docker run --rm
-v /var/run/docker.sock:/var/run/docker.sock
aquasec/trivy image myimage:tag
undefined
docker run --rm
-v /var/run/docker.sock:/var/run/docker.sock
aquasec/trivy image myimage:tag
undefined

故障排查

故障排查

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