第04章:Docker 镜像管理 第04章Docker 镜像管理本章目标全面掌握 Docker 镜像的搜索、拉取、查看、构建、推送等操作理解镜像标签和分层的管理策略。4.1 镜像的命名规范4.1.1 镜像名称的组成[registry-host[:port]/][namespace/]repository[:tag|digest] 完整格式示例 docker.io/library/nginx:latest registry.cn-hangzhou.aliyuncs.com/myproject/myapp:v1.0 ghcr.io/owner/repo:sha-abc123组成部分说明默认值registry-host镜像仓库地址docker.ioDocker Hubnamespace命名空间/组织library官方镜像repository镜像名称必填tag标签版本号latestdigest内容哈希—4.1.2 官方镜像 vs 第三方镜像# 官方镜像省略 namespace 和 registrydockerpull nginx# 等价于 docker.io/library/nginx:latest# 第三方镜像需要指定 namespacedockerpull bitnami/nginx# docker.io/bitnami/nginx:latest# 私有仓库镜像需要指定 registry 地址dockerpull registry.example.com:5000/myproject/myapp:v1.04.2 镜像的基本操作4.2.1 搜索镜像# 从 Docker Hub 搜索dockersearch nginx# 指定仓库搜索dockersearch bitnami/nginx# 按星标数过滤dockersearch--filterstars100nginx# 限制输出数量dockersearch--limit5nginx4.2.2 拉取镜像# 拉取最新版本默认dockerpull nginx# 拉取指定版本标签dockerpull nginx:1.25dockerpull nginx:1.25-alpine# 拉取指定平台的镜像dockerpull--platformlinux/amd64 nginx:latest# 通过 digest 拉取精确版本dockerpull nginxsha256:abc123def456...# 从指定仓库拉取dockerpull registry.cn-hangzhou.aliyuncs.com/library/nginx:latest4.2.3 查看本地镜像# 列出所有本地镜像dockerimagesdockerimagels# 以表格形式显示更多信息dockerimages--formattable {{.Repository}}\t{{.Tag}}\t{{.Size}}\t{{.CreatedSince}}# 输出示例# REPOSITORY TAG SIZE CREATEDSINCE# nginx 1.25 187MB 2 weeks ago# nginx 1.25-alpine 41MB 2 weeks ago# python 3.11 912MB 3 weeks ago# ubuntu 22.04 77.8MB 1 month ago# 过滤镜像dockerimages--filterdanglingtrue# 悬空镜像无标签dockerimages--filterreferencenginx:*# 指定仓库的镜像dockerimages--filterbeforeubuntu:22.04# 在某个镜像之前创建的dockerimages--filtersinceubuntu:22.04# 在某个镜像之后创建的# 显示镜像 IDdockerimages-q4.2.4 查看镜像详情# 查看镜像完整元数据JSON 格式dockerimage inspect nginx:latest# 查看镜像的层信息dockerhistorynginx:latest# 查看镜像大小dockerimage inspect nginx:latest--format{{.Size}}# 输出: 187425608 (字节)# 转换为可读格式dockersystemdf-v# 查看所有镜像的详细大小4.2.5 删除镜像# 删除指定镜像dockerrmi nginx:latestdockerimagermnginx:latest# 批量删除dockerrmi$(dockerimages-q)# 删除所有镜像dockerrmi$(dockerimages--filterdanglingtrue-q)# 删除悬空镜像dockerimages-q--filterreferencenginx*|xargsdockerrmi# 删除 nginx 相关镜像# 强制删除即使有容器在使用dockerrmi-fnginx:latest4.3 镜像标签管理4.3.1 什么是镜像标签标签Tag是给镜像起的别名用于标识不同版本nginx:1.25 ← 精确版本 nginx:1.25-alpine ← 精确版本 变体 nginx:latest ← 最新版本不推荐生产使用 nginx ← 等价于 nginx:latest4.3.2 为镜像打标签# 给已有镜像打标签相当于创建了一个新引用dockertag nginx:latest myregistry.com/nginx:v1.0dockertag nginx:latest myregistry.com/nginx:production# 标签不影响镜像内容只是额外的引用dockerimages myregistry.com/nginx# REPOSITORY TAG SIZE# myregistry.com/nginx v1.0 187MB# myregistry.com/nginx production 187MB# nginx latest 187MB ← 原始标签仍在4.3.3 标签策略最佳实践策略示例适用场景语义化版本v1.2.3正式发布Git SHAcommit-a1b2c3d开发版本日期标签2024.06.15定期构建环境标签staging、production环境区分多标签同时打v1.2.3和latest灵活引用# 推荐为同一镜像打多个标签dockertag myapp:build-123 myregistry.com/myapp:v1.2.3dockertag myapp:build-123 myregistry.com/myapp:latest4.4 镜像的导入与导出4.4.1 导出为 tar 文件# 将镜像导出为 tar 文件dockersave-onginx-backup.tar nginx:latestdockersave nginx:latestnginx-backup.tar# 导出多个镜像dockersave-oimages-backup.tar nginx:latest python:3.11 ubuntu:22.04# 导出所有镜像dockersave-oall-images.tar$(dockerimages-q)4.4.2 从 tar 文件导入# 从 tar 文件导入镜像dockerload-inginx-backup.tardockerloadnginx-backup.tar# 导入后查看dockerimages|grepnginx4.4.3 容器导出为镜像# 将运行中的容器状态导出为新镜像# Step 1: 启动容器并做一些修改dockerrun-itubuntu:22.04 /bin/bash# 在容器内安装一些软件...apt-getupdateapt-getinstall-yvimcurlexit# Step 2: 导出容器为 tardockerexportcontainer-idmy-ubuntu.tar# Step 3: 将 tar 导入为新镜像dockerimportmy-ubuntu.tar my-ubuntu:v1.0# 注意export 会丢失镜像的元数据如 CMD、ENV 等# 建议优先使用 docker commit4.4.4 save vs export 对比特性docker savedocker export输入镜像image容器container输出保留所有层和元数据合并为单层丢失元数据导入命令docker loaddocker import适用场景备份、迁移镜像创建简单的文件系统快照4.5 镜像构建commit 方式4.5.1 docker commit 原理# 基于容器的修改创建新镜像dockercommit[OPTIONS]CONTAINER[REPOSITORY[:TAG]]# 示例# Step 1: 启动一个容器dockerrun-itubuntu:22.04 /bin/bash# Step 2: 在容器中安装软件apt-getupdateapt-getinstall-ypython3 python3--version# Step 3: 退出容器exit# Step 4: 基于容器创建镜像dockercommitcontainer-idmy-ubuntu-python:v1.0# Step 5: 验证dockerrun-itmy-ubuntu-python:v1.0 python3--version# Python 3.10.124.5.2 commit 的局限性问题说明不可追溯无法知道镜像经历了哪些变更不可重复手动操作难以精确复现体积膨胀无法利用缓存机制优化不符合最佳实践应该使用 Dockerfile 构建⚠️企业级规范生产环境应使用 Dockerfile 构建镜像docker commit 仅用于临时调试。4.6 清理无用镜像4.6.1 悬空镜像Dangling Images# 悬空镜像没有标签指向的镜像层# 通常在重新构建镜像后产生# 查看悬空镜像dockerimages-fdanglingtrue# 删除所有悬空镜像dockerimage prune# 同时删除未被容器使用的镜像dockerimage prune-a4.6.2 一键清理# 清理所有未使用的资源镜像、容器、网络、构建缓存dockersystem prune# 更激进的清理包括所有未使用的卷dockersystem prune-a--volumes# 查看 Docker 磁盘使用情况dockersystemdfdockersystemdf-v# 详细信息4.6.3 磁盘使用分析# 查看各类型资源占用dockersystemdf# TYPE TOTAL ACTIVE SIZE RECLAIMABLE# Images 15 5 2.8GB 1.9GB (67%)# Containers 8 3 150MB 120MB (80%)# Local Volumes 10 4 2.1GB 1.5GB (71%)# Build Cache 50 0 3.2GB 3.2GB (100%)# 按大小排序查看镜像dockerimages--format{{.Repository}}:{{.Tag}}\t{{.Size}}|sort-t$\t-k2-h-r4.7 镜像安全扫描4.7.1 使用 Docker Scout# Docker Scout 是 Docker 官方的镜像安全扫描工具dockerscout cves nginx:latestdockerscout recommendations nginx:latest4.7.2 使用 Trivy开源工具# 安装 Trivy# macOSbrewinstalltrivy# Linuxsudoapt-getinstalltrivy# 扫描镜像漏洞trivy image nginx:latest trivy image--severityHIGH,CRITICAL nginx:latest# 输出 JSON 格式报告trivy image-fjson-oreport.json nginx:latest4.8 动手实验实验 4.1镜像的基本操作# 1. 搜索并拉取镜像dockersearch pythondockerpull python:3.11-slim# 2. 查看本地镜像dockerimages python# 3. 查看镜像历史dockerhistorypython:3.11-slim# 4. 导出镜像dockersave python:3.11-slimpython-backup.tarls-lhpython-backup.tar# 5. 删除镜像dockerrmi python:3.11-slimdockerimages|greppython# 确认已删除# 6. 从备份恢复dockerloadpython-backup.tardockerimages python实验 4.2使用 commit 构建自定义镜像# 1. 启动基础容器dockerrun-it--namemy-custom ubuntu:22.04 /bin/bash# 2. 在容器内安装软件apt-getupdateapt-getinstall-ycurlvimgitmkdir-p/appechoHello Custom Image/app/hello.txt# 3. 退出容器exit# 4. 基于容器创建镜像dockercommit-mAdded curl vim gitmy-custom my-ubuntu-custom:v1.0# 5. 验证新镜像dockerrun--rmmy-ubuntu-custom:v1.0cat/app/hello.txt# Hello Custom Imagedockerrun--rmmy-ubuntu-custom:v1.0curl--version# 6. 清理dockerrmmy-custom实验 4.3磁盘清理# 1. 查看磁盘使用dockersystemdf# 2. 创建一些悬空镜像foriin{1..5};dodockerbuild-ttest-$i-EOF FROM alpine:latest RUN echo test$i EOFdone# 3. 查看悬空镜像dockerimages-fdanglingtrue# 4. 清理悬空镜像dockerimage prune-f# 5. 最终清理dockersystem prune-f4.9 本章小结操作命令说明搜索docker search在仓库中搜索镜像拉取docker pull下载镜像到本地列表docker images查看本地镜像详情docker inspect查看镜像完整元数据历史docker history查看镜像的构建历史标签docker tag为镜像添加标签导出docker save导出为 tar 文件导入docker load从 tar 文件导入提交docker commit基于容器创建镜像删除docker rmi删除镜像清理docker image prune清理无用镜像4.10 课后练习基础题拉取 3 个不同镜像nginx、python、redis查看它们的层结构和大小。操作题使用 docker save/load 完成镜像的备份和恢复流程。进阶题使用 Trivy 扫描 nginx:latest 镜像分析其安全漏洞情况。 下一章Dockerfile 深度解析 —— 掌握企业级 Dockerfile 编写技能