第6章 Docker镜像基础操作

6.1.1 基本搜索

bash 复制代码
# 搜索镜像
docker search nginx

# 输出示例:
# NAME                               DESCRIPTION                     STARS   OFFICIAL   AUTOMATED
# nginx                              Official build of Nginx         19000   [OK]       
# linuxserver/nginx                  Nginx web server                200                [OK]
# bitnami/nginx                      Bitnami nginx Docker Image      150                [OK]

输出字段说明

  • NAME: 镜像名称
  • DESCRIPTION: 镜像描述
  • STARS: 星标数(受欢迎程度)
  • OFFICIAL: 是否为官方镜像
  • AUTOMATED: 是否为自动构建

6.1.2 过滤搜索结果

bash 复制代码
# 只显示官方镜像
docker search --filter "is-official=true" nginx

# 只显示自动构建的镜像
docker search --filter "is-automated=true" nginx

# 显示星标>=100的镜像
docker search --filter "stars=100" nginx

# 限制显示数量
docker search --limit 5 nginx

# 不截断描述
docker search --no-trunc nginx

# 格式化输出
docker search --format "table {{.Name}}\t{{.StarCount}}\t{{.IsOfficial}}" nginx

6.1.3 搜索技巧

选择镜像的原则

  1. 优先选择官方镜像

    bash 复制代码
    docker search --filter "is-official=true" python
  2. 查看星标数

    • 星标数反映社区认可度
    • 一般选择星标>100的镜像
  3. 检查更新频率

  4. 查看镜像文档

    • 阅读镜像的README
    • 了解支持的标签和版本

6.1.4 实战示例

bash 复制代码
# 场景1:寻找Python运行环境
docker search python --filter "is-official=true"

# 场景2:寻找Redis数据库
docker search redis --filter "stars=100" --limit 10

# 场景3:搜索特定版本
# 注意:search命令不支持标签搜索,需要到Docker Hub查看
# https://hub.docker.com/_/nginx?tab=tags

6.2 拉取镜像:docker pull

6.2.1 基本拉取

bash 复制代码
# 拉取最新版本(latest标签)
docker pull nginx
# 等同于
docker pull nginx:latest

# 拉取指定版本
docker pull nginx:1.25
docker pull nginx:1.25.3
docker pull nginx:alpine

# 拉取指定摘要
docker pull nginx@sha256:abc123...

6.2.2 从不同仓库拉取

bash 复制代码
# 从Docker Hub拉取(默认)
docker pull nginx

# 完整格式
docker pull docker.io/library/nginx:latest

# 从私有仓库拉取
docker pull registry.company.com:5000/myapp:1.0

# 从其他公共仓库拉取
docker pull quay.io/prometheus/prometheus
docker pull gcr.io/google-containers/nginx

6.2.3 多架构镜像

bash 复制代码
# 拉取特定平台的镜像
docker pull --platform linux/amd64 nginx
docker pull --platform linux/arm64 nginx
docker pull --platform linux/arm/v7 nginx

# 查看镜像支持的平台
docker manifest inspect nginx | grep -A 5 "platform"

# 在Apple Silicon Mac上拉取x86镜像
docker pull --platform linux/amd64 mysql:8.0

6.2.4 批量拉取

bash 复制代码
# 拉取多个版本
for tag in 1.23 1.24 1.25; do
  docker pull nginx:$tag
done

# 从文件读取并拉取
# images.txt内容:
# nginx:1.25
# redis:7.0
# postgres:13
cat images.txt | while read image; do
  docker pull $image
done

6.2.5 拉取优化

bash 复制代码
# 使用镜像加速器(已在daemon.json配置)
# 查看配置
docker info | grep -A 5 "Registry Mirrors"

# 限制并发下载
# 在daemon.json中配置
{
  "max-concurrent-downloads": 3
}

# 查看拉取进度
docker pull nginx:latest
# 输出:
# latest: Pulling from library/nginx
# a2abf6c4d29d: Pull complete 
# a9edb18cadd1: Pull complete 
# 589b7251471a: Pull complete

6.2.6 验证镜像

bash 复制代码
# 拉取后验证
docker pull nginx:1.25

# 查看镜像详情
docker image inspect nginx:1.25

# 验证镜像签名(Docker Content Trust)
export DOCKER_CONTENT_TRUST=1
docker pull nginx:1.25
# 会验证镜像签名

6.3 查看本地镜像:docker images

6.3.1 基本列表

bash 复制代码
# 列出所有镜像
docker images

# 新格式
docker image ls

# 输出示例:
# REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
# nginx         1.25      605c77e624dd   2 weeks ago    141MB
# nginx         1.24      ad17f2e2b55e   1 month ago    140MB
# ubuntu        22.04     27941809078c   2 weeks ago    77.8MB
# redis         7.0       7614ae9453d1   3 weeks ago    117MB

6.3.2 过滤和筛选

bash 复制代码
# 只显示特定仓库的镜像
docker images nginx
docker images ubuntu

# 显示完整的IMAGE ID
docker images --no-trunc

# 只显示镜像ID
docker images -q
docker images nginx -q

# 显示悬空镜像(dangling)
docker images --filter "dangling=true"

# 过滤特定标签
docker images --filter "reference=nginx:1.*"

# 显示指定时间之前/之后创建的镜像
docker images --filter "before=nginx:1.25"
docker images --filter "since=nginx:1.24"

6.3.3 格式化输出

bash 复制代码
# 自定义输出格式
docker images --format "{{.Repository}}:{{.Tag}}"

# 表格格式
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"

# JSON格式
docker images --format "{{json .}}"

# 使用jq处理JSON
docker images --format "{{json .}}" | jq -r '.Repository + ":" + .Tag'

# 自定义输出示例
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.ID}}\t{{.Size}}"
# REPOSITORY    TAG       IMAGE ID      SIZE
# nginx         1.25      605c77e624    141MB
# ubuntu        22.04     27941809078   77.8MB

6.3.4 查看镜像详情

bash 复制代码
# 查看镜像详细信息
docker image inspect nginx:1.25

# 提取特定信息
docker image inspect nginx:1.25 --format='{{.Architecture}}'
docker image inspect nginx:1.25 --format='{{.Os}}'
docker image inspect nginx:1.25 --format='{{.Size}}'

# 查看镜像环境变量
docker image inspect nginx:1.25 --format='{{.Config.Env}}'

# 查看镜像暴露的端口
docker image inspect nginx:1.25 --format='{{.Config.ExposedPorts}}'

# 查看镜像层
docker image inspect nginx:1.25 --format='{{.RootFS.Layers}}'

6.3.5 查看镜像历史

bash 复制代码
# 查看镜像构建历史
docker history nginx:1.25

# 输出示例:
# IMAGE          CREATED       CREATED BY                                      SIZE
# 605c77e624dd   2 weeks ago   /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon...   0B
# <missing>      2 weeks ago   /bin/sh -c #(nop)  STOPSIGNAL SIGQUIT           0B
# <missing>      2 weeks ago   /bin/sh -c #(nop)  EXPOSE 80                    0B
# <missing>      2 weeks ago   /bin/sh -c apt-get update && apt-get instal...   65MB

# 不截断显示
docker history --no-trunc nginx:1.25

# 显示完整命令
docker history --no-trunc --format "{{.CreatedBy}}" nginx:1.25

# 查看每层的大小
docker history --format "table {{.ID}}\t{{.Size}}" nginx:1.25

6.4 删除镜像:docker rmi

6.4.1 基本删除

bash 复制代码
# 删除单个镜像
docker rmi nginx:1.24
docker image rm nginx:1.24

# 使用镜像ID删除
docker rmi 605c77e624dd

# 删除多个镜像
docker rmi nginx:1.24 nginx:1.23 ubuntu:20.04

# 强制删除(即使有容器在使用)
docker rmi -f nginx:1.24

# 注意:强制删除可能导致容器无法启动

6.4.2 批量删除

bash 复制代码
# 删除所有悬空镜像(推荐)
docker image prune

# 删除所有未使用的镜像
docker image prune -a

# 删除所有镜像(危险操作)
docker rmi $(docker images -q)

# 删除特定仓库的所有镜像
docker rmi $(docker images nginx -q)

# 删除特定标签模式的镜像
docker rmi $(docker images --filter "reference=myapp:dev-*" -q)

# 删除30天前创建的镜像
docker image prune -a --filter "until=720h"

6.4.3 清理策略

bash 复制代码
# 查看磁盘使用情况
docker system df

# 输出示例:
# TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
# Images          10        5         2.5GB     1.2GB (48%)
# Containers      5         3         150MB     50MB (33%)
# Local Volumes   3         2         500MB     200MB (40%)
# Build Cache     0         0         0B        0B

# 详细信息
docker system df -v

# 安全清理(只删除悬空镜像)
docker image prune

# 激进清理(删除所有未使用资源)
docker system prune -a --volumes

# 交互式清理
docker system prune
# 提示:Are you sure you want to continue? [y/N]

6.4.4 防止误删

bash 复制代码
# 检查哪些容器在使用该镜像
docker ps -a --filter "ancestor=nginx:1.24"

# 查看镜像被哪些容器引用
docker image inspect nginx:1.24 --format='{{.RepoDigests}}'

# 先停止并删除相关容器
docker stop $(docker ps -a --filter "ancestor=nginx:1.24" -q)
docker rm $(docker ps -a --filter "ancestor=nginx:1.24" -q)

# 然后删除镜像
docker rmi nginx:1.24

6.5 镜像标签管理:docker tag

6.5.1 基本标签操作

bash 复制代码
# 为镜像添加新标签
docker tag nginx:1.25 mynginx:latest
docker tag nginx:1.25 mynginx:prod
docker tag nginx:1.25 mynginx:v1.25.3

# 查看结果
docker images mynginx
# REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
# mynginx      latest    605c77e624dd   2 weeks ago  141MB
# mynginx      prod      605c77e624dd   2 weeks ago  141MB
# mynginx      v1.25.3   605c77e624dd   2 weeks ago  141MB

# 注意:这些标签都指向同一个镜像(IMAGE ID相同)

6.5.2 标签命名规范

bash 复制代码
# 语义化版本
docker tag myapp:latest myapp:1.0.0
docker tag myapp:latest myapp:1.0
docker tag myapp:latest myapp:1

# Git提交哈希
GIT_COMMIT=$(git rev-parse --short HEAD)
docker tag myapp:latest myapp:$GIT_COMMIT

# 时间戳
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
docker tag myapp:latest myapp:$TIMESTAMP

# 环境标签
docker tag myapp:latest myapp:dev
docker tag myapp:latest myapp:staging
docker tag myapp:latest myapp:prod

# 组合标签
docker tag myapp:latest myapp:1.0.0-prod
docker tag myapp:latest myapp:1.0.0-$GIT_COMMIT

6.5.3 推送到私有仓库

bash 复制代码
# 为私有仓库添加标签
docker tag nginx:1.25 registry.company.com:5000/nginx:1.25
docker tag myapp:latest registry.company.com:5000/myapp:prod

# 推送到私有仓库
docker push registry.company.com:5000/nginx:1.25
docker push registry.company.com:5000/myapp:prod

# 推送所有标签
docker push registry.company.com:5000/myapp --all-tags

6.5.4 标签管理策略

开发环境标签策略

bash 复制代码
# 开发分支
docker tag myapp:latest myapp:dev-feature-x
docker tag myapp:latest myapp:dev-$(git rev-parse --short HEAD)

# 测试环境
docker tag myapp:latest myapp:test
docker tag myapp:latest myapp:test-$(date +%Y%m%d)

# 预发布环境
docker tag myapp:latest myapp:staging
docker tag myapp:latest myapp:rc-1.0.0

# 生产环境
docker tag myapp:latest myapp:prod
docker tag myapp:latest myapp:1.0.0
docker tag myapp:latest myapp:stable

6.5.5 删除标签

bash 复制代码
# 删除本地标签(实际是删除镜像引用)
docker rmi mynginx:prod
docker rmi mynginx:latest

# 注意:如果同一个IMAGE ID有多个标签,删除一个标签不会删除镜像
docker images
# mynginx      prod      605c77e624dd   2 weeks ago  141MB
# mynginx      latest    605c77e624dd   2 weeks ago  141MB

docker rmi mynginx:prod
# 只删除prod标签,latest标签和镜像本身仍然存在

docker rmi mynginx:latest
# 删除latest标签,但镜像仍可能通过IMAGE ID访问

# 完全删除镜像(所有标签)
docker rmi 605c77e624dd
# 或强制删除
docker rmi -f 605c77e624dd

6.6 镜像的导入导出

6.6.1 导出镜像:docker save

bash 复制代码
# 导出单个镜像
docker save nginx:1.25 > nginx-1.25.tar
docker save nginx:1.25 -o nginx-1.25.tar

# 导出多个镜像到同一个文件
docker save nginx:1.25 nginx:1.24 > nginx-multiple.tar

# 导出所有标签
docker save nginx > nginx-all.tar

# 压缩导出
docker save nginx:1.25 | gzip > nginx-1.25.tar.gz

# 查看tar文件大小
ls -lh nginx-1.25.tar
# -rw-r--r-- 1 user user 141M Feb 10 10:00 nginx-1.25.tar

6.6.2 导入镜像:docker load

bash 复制代码
# 导入镜像
docker load < nginx-1.25.tar
docker load -i nginx-1.25.tar

# 导入压缩的镜像
gunzip -c nginx-1.25.tar.gz | docker load

# 查看导入结果
docker images nginx

6.6.3 导出容器:docker export

bash 复制代码
# 运行一个容器
docker run -d --name web nginx

# 导出容器(导出容器的文件系统)
docker export web > web-container.tar
docker export web -o web-container.tar

# 注意:export导出的是容器的文件系统,不包含历史和元数据

6.6.4 导入容器:docker import

bash 复制代码
# 从tar文件导入为镜像
docker import web-container.tar mynginx:exported

# 从URL导入
docker import http://example.com/container.tar mynginx:imported

# 指定提交信息
docker import -c "CMD nginx -g 'daemon off;'" web-container.tar mynginx:v1

# 查看导入的镜像
docker images mynginx

6.6.5 save vs export 对比

特性 docker save docker export
操作对象 镜像 容器
保留内容 完整的镜像层、历史、元数据 容器的文件系统快照
大小 较大(包含所有层) 较小(单层)
恢复命令 docker load docker import
使用场景 迁移镜像、备份 创建新的基础镜像

实例对比

bash 复制代码
# save保留完整历史
docker save nginx:1.25 > nginx-save.tar
docker load < nginx-save.tar
docker history nginx:1.25  # 显示完整构建历史

# export只保留文件系统
docker run -d --name temp nginx:1.25
docker export temp > nginx-export.tar
docker import nginx-export.tar mynginx:imported
docker history mynginx:imported  # 只有一层

6.6.6 实用场景

场景1:离线环境部署

bash 复制代码
# 在联网机器上
docker pull nginx:1.25
docker pull redis:7.0
docker pull postgres:13

# 导出所有镜像
docker save nginx:1.25 redis:7.0 postgres:13 > offline-images.tar

# 复制到离线机器
scp offline-images.tar user@offline-server:/tmp/

# 在离线机器上
docker load < /tmp/offline-images.tar

场景2:快速分发自定义镜像

bash 复制代码
# 构建自定义镜像
docker build -t myapp:1.0 .

# 导出并压缩
docker save myapp:1.0 | gzip > myapp-1.0.tar.gz

# 分发给其他开发者
# 其他开发者导入
gunzip -c myapp-1.0.tar.gz | docker load

场景3:镜像备份

bash 复制代码
# 定期备份重要镜像
#!/bin/bash
BACKUP_DIR=/backup/docker-images
mkdir -p $BACKUP_DIR

for image in $(docker images --format "{{.Repository}}:{{.Tag}}" | grep mycompany); do
  filename=$(echo $image | tr '/:' '_')
  docker save $image | gzip > $BACKUP_DIR/$filename-$(date +%Y%m%d).tar.gz
done

6.7 镜像层的理解

6.7.1 查看镜像层

bash 复制代码
# 查看镜像层
docker image inspect nginx:1.25 --format='{{json .RootFS.Layers}}' | jq

# 输出示例:
# [
#   "sha256:abc123...",
#   "sha256:def456...",
#   "sha256:ghi789..."
# ]

# 查看每层的大小
docker history nginx:1.25 --no-trunc

6.7.2 层的共享

bash 复制代码
# 两个镜像共享基础层
docker pull nginx:1.25
docker pull nginx:1.24

# 查看实际占用空间
docker system df
# 注意:两个镜像的SIZE之和 > 实际占用空间

# 查看层的共享情况
docker image inspect nginx:1.25 nginx:1.24 --format='{{.RootFS.Layers}}' | sort | uniq -d

6.7.3 优化镜像大小

技巧1:使用精简基础镜像

bash 复制代码
# Alpine Linux版本更小
docker images nginx
# nginx     1.25      141MB
# nginx     alpine    24MB

技巧2:合并层

dockerfile 复制代码
# 不好的做法(多层)
RUN apt-get update
RUN apt-get install -y curl
RUN apt-get install -y vim

# 好的做法(单层)
RUN apt-get update && apt-get install -y \
    curl \
    vim \
    && rm -rf /var/lib/apt/lists/*

6.8 小结

通过本章学习,我们掌握了Docker镜像的基础操作:

搜索镜像

  • docker search命令
  • 过滤和筛选
  • 选择镜像的原则

拉取镜像

  • docker pull命令
  • 多架构支持
  • 批量拉取和优化

查看镜像

  • docker images命令
  • 过滤和格式化
  • 查看详情和历史

删除镜像

  • docker rmi命令
  • 批量删除
  • 清理策略

标签管理

  • docker tag命令
  • 标签命名规范
  • 版本管理策略

导入导出

  • save/load vs export/import
  • 离线部署
  • 备份策略

下一步

在第7章中,我们将学习构建自定义镜像:

  • Dockerfile语法详解
  • 常用指令(FROM、RUN、COPY等)
  • 构建上下文
  • 多阶段构建
  • 构建缓存优化

这是Docker实践中最重要的技能之一。


本章思考题

  1. docker save和docker export的区别是什么?各适用于什么场景?
  2. 为什么多个镜像可以共享存储空间?
  3. 如何设计一个合理的镜像标签策略?
  4. 在生产环境中,应该如何管理镜像的生命周期?

相关资源

相关推荐
柏木乃一2 小时前
Linux进程信号(2):信号产生part2
linux·运维·服务器·c++·信号处理·信号·异常
马丁的代码日记2 小时前
Docker 无法拉取镜像的解决方案
运维·docker·容器
是小王吖!2 小时前
容器技术 - docker
运维·docker·容器
FJW0208143 小时前
《Nginx 高级应用:变量、Rewrite、反向代理与 OpenResty 扩展》(3)
运维·nginx·openresty
feng68_3 小时前
LVS(linuxvirtualserver)
运维·服务器·lvs
Cyber4K3 小时前
【Kubernetes专项】Ingress、Ingress-Controller
云原生·容器·kubernetes
云道轩4 小时前
在Rocky Linux 上在线安装OpenClaw 2026.2.13
linux·运维·人工智能·智能体·openclaw
IT 行者5 小时前
OpenClaw 浏览器自动化测试的那些坑(一):Linux Snap 版本的 Chromium 无法使用托管模式
linux·运维·服务器·人工智能
t***44235 小时前
CORS:跨域访问、如何在Nginx中配置允许跨域访问
运维·nginx