基础 深入理解Docker镜像的分层结构,掌握搜索、拉取、构建、推送
Docker镜像是一个只读的模板,包含创建容器所需的文件系统层和配置信息。镜像是容器的"蓝图"——同一个镜像可以创建无数个容器实例。
Docker镜像名称由三部分组成:
[registry/]repository[:tag]
# 示例解析:
docker.io/library/nginx:1.25-alpine
│ │ │ │
│ │ │ └── Tag(版本标签,默认latest)
│ │ └────── Repository(仓库名)
│ └────────────── Namespace(命名空间/用户)
└──────────────────────── Registry(注册服务器)
# 常见写法:
nginx # = docker.io/library/nginx:latest
nginx:1.25 # 指定版本
nginx:alpine # Alpine版
myregistry.com/myapp:v1 # 私有仓库
username/myapp:latest # Docker Hub个人仓库
# 在Docker Hub搜索镜像 ✅
docker search nginx
NAME DESCRIPTION STARS OFFICIAL
nginx Official build of Nginx. 19000 [OK]
nginx/nginx-ingress NGINX Ingress Controller 350
# 过滤星级 ≥ 50 的镜像
docker search --filter stars=50 nginx
# 只显示官方镜像
docker search --filter is-official=true nginx
# 限制结果数量
docker search --limit 5 nginx
# 拉取最新版本 ✅
docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
# 拉取指定版本 ✅
docker pull nginx:1.25-alpine
# 拉取所有标签(不推荐)
docker pull --all-tags nginx
# 从指定平台拉取
docker pull --platform linux/arm64 nginx
# 使用镜像加速器拉取
docker pull mirror.ccs.tencentyun.com/library/nginx:latest
# 列出本地所有镜像 ✅
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 2 weeks ago 187MB
ubuntu 22.04 58db3edaf2be 3 weeks ago 77.8MB
redis 7-alpine 3c5b4a53de57 4 weeks ago 32.4MB
# 只显示镜像ID
docker images -q
# 过滤显示
docker images nginx
docker images --filter "dangling=true" # 悬空镜像(无标签)
docker images --filter "since=ubuntu" # 在某镜像之后构建的
docker images --format "{{.Repository}}:{{.Tag}}" # 自定义格式
# 查看镜像详情 ✅
docker inspect nginx
# 查看特定字段
docker inspect nginx --format='{{.Config.Env}}'
docker inspect nginx --format='{{.Config.ExposedPorts}}'
docker inspect nginx --format='{{.RootFS.Layers}}'
# 查看镜像构建历史 ✅
docker history nginx
IMAGE CREATED CREATED BY SIZE
605c77e624dd 2 weeks ago /bin/sh -c #(nop) CMD ["nginx" "-g"... 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 #(nop) COPY entrypoint.sh... 1.2kB
# 查看镜像层信息 ✅
docker image inspect nginx --format='{{json .RootFS.Layers}}'
# 为镜像打标签 ✅
docker tag nginx:latest myregistry.com/myapp/nginx:v1.0
# 同一镜像可以有多个标签
docker tag nginx:latest myapp/nginx:stable
docker tag nginx:latest myapp/nginx:1.25
# 验证:同一IMAGE ID,不同标签
docker images | grep nginx
myregistry.com/myapp/nginx v1.0 605c77e624dd 2 weeks ago 187MB
myapp/nginx stable 605c77e624dd 2 weeks ago 187MB
myapp/nginx 1.25 605c77e624dd 2 weeks ago 187MB
# 登录Docker Hub
docker login
# 登录私有仓库
docker login myregistry.com
# 推送镜像 ✅
docker push myregistry.com/myapp/nginx:v1.0
# 推送所有标签
docker push --all-tags myregistry.com/myapp/nginx
# 删除镜像(需无容器使用)
docker rmi nginx:1.25-alpine
# 强制删除
docker rmi -f nginx:1.25-alpine
# 删除所有悬空镜像 ✅
docker image prune
# 删除所有未使用镜像
docker image prune -a
# 删除所有镜像(危险!)
docker rmi $(docker images -q)
# 导出为tar文件 ✅
docker save -o nginx.tar nginx:latest
# 导出多个镜像
docker save -o images.tar nginx:latest redis:7-alpine ubuntu:22.04
# 压缩导出
docker save nginx:latest | gzip > nginx.tar.gz
# 从tar文件导入 ✅
docker load -i nginx.tar
# 从压缩文件导入
gunzip -c nginx.tar.gz | docker load
docker save:保存完整镜像(含所有层和元数据),可重新推送到Registrydocker export:导出容器的文件系统快照(扁平化,无分层信息),用docker import导入save/load,迁移容器文件系统用export/import| 基础镜像 | 大小 | 包管理器 | 适用场景 |
|---|---|---|---|
| alpine | ~7MB | apk | 微服务、CLI工具(首选) |
| distroless | ~2MB | 无 | 安全敏感场景(无shell) |
| slim (Debian) | ~80MB | apt | 需要glibc兼容性 |
| ubuntu | ~77MB | apt | 通用开发环境 |
| debian | ~124MB | apt | 稳定性优先 |
| centos/rhel | ~200MB | yum/dnf | 企业兼容性 |
| node:alpine | ~180MB | apk | Node.js运行时 |
| python:slim | ~150MB | apt | Python运行时 |
# Web服务器
nginx:alpine # 首选,轻量快速
httpd:alpine # Apache
caddy:alpine # 自动HTTPS
# 应用运行时
node:20-alpine # Node.js
python:3.12-slim # Python
openjdk:21-jdk-slim # Java
golang:1.22-alpine # Go
# 数据库
postgres:16-alpine # PostgreSQL
mysql:8.0 # MySQL(无alpine版)
redis:7-alpine # Redis
mongo:7 # MongoDB
❓ 什么是悬空镜像(dangling image)?悬空镜像是指没有标签的镜像(
docker image prune清理,安全无副作用。❓ pull镜像超时怎么办?①配置镜像加速器(见第01课)②重试:
docker pull支持断点续传 ③使用代理:HTTP_PROXY=http://proxy:port docker pull nginx ④下载离线镜像包用docker load导入。
❓ 如何查看镜像里有什么文件?方法一:
docker run --rm -it nginx ls / 方法二:docker export <container> | tar -tvf - 方法三:dive工具可以交互式浏览每层内容。
下一课:深入学习容器生命周期管理——从创建到销毁的完整旅程!