Appearance
镜像管理
镜像是 Docker 的核心概念,本节介绍镜像的查看、拉取、构建、删除等完整操作。
查看本地镜像
bash
# 列出所有本地镜像
docker images
# 简洁格式(只显示 ID)
docker images -q
# 列出所有镜像(包括中间层镜像)
docker images -a
# 格式化输出
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"输出说明:
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest a72860cb95fd 2 days ago 187MB
ubuntu 22.04 174c8c134b2a 3 weeks ago 77.8MB| 字段 | 说明 |
|---|---|
| REPOSITORY | 镜像名称(仓库名) |
| TAG | 标签,通常是版本号 |
| IMAGE ID | 镜像的唯一标识(前12位) |
| CREATED | 创建时间 |
| SIZE | 镜像大小 |
搜索镜像
bash
# 搜索 Docker Hub 上的镜像
docker search nginx
# 只显示官方镜像
docker search --filter is-official=true nginx
# 限制结果数量
docker search --limit 5 nginx拉取镜像
bash
# 拉取最新版本(latest 标签)
docker pull nginx
# 拉取指定版本
docker pull nginx:1.25
# 拉取指定架构的镜像
docker pull --platform linux/amd64 nginx
# 拉取到本地后查看
docker images nginx常用镜像标签规范
| 标签示例 | 说明 |
|---|---|
latest | 最新稳定版(默认) |
1.25 | 指定主次版本 |
1.25.3 | 指定完整版本 |
alpine | 基于 Alpine Linux,体积最小 |
slim | 精简版,去除非必要组件 |
lts | 长期支持版 |
20-alpine | Node.js 20 + Alpine |
查看镜像详情
bash
# 查看镜像详细信息(JSON 格式)
docker inspect nginx
# 查看镜像的分层历史
docker history nginx
# 查看镜像历史(完整命令)
docker history --no-trunc nginxdocker history 输出示例:
IMAGE CREATED CREATED BY SIZE
a72860cb95fd 2 days ago CMD ["nginx" "-g" "daemon off;"] 0B
<missing> 2 days ago STOPSIGNAL SIGQUIT 0B
<missing> 2 days ago EXPOSE map[80/tcp:{}] 0B
...镜像标签管理
bash
# 为镜像添加新标签(相当于创建别名)
docker tag nginx:latest myregistry.com/myapp/nginx:1.0
# 标签后推送
docker push myregistry.com/myapp/nginx:1.0删除镜像
bash
# 删除指定镜像
docker rmi nginx
# 按镜像 ID 删除
docker rmi a72860cb95fd
# 强制删除(即使有容器使用该镜像)
docker rmi -f nginx
# 删除所有本地镜像(慎用!)
docker rmi $(docker images -q)
# 删除未使用的镜像
docker image prune
# 删除所有未被容器使用的镜像
docker image prune -a导出和导入镜像
当无法访问网络时,可以通过文件传输镜像:
bash
# 导出镜像为 tar 文件
docker save -o nginx.tar nginx:latest
# 也可以保存多个镜像
docker save -o images.tar nginx:latest mysql:8.0
# 从 tar 文件导入镜像
docker load -i nginx.tar
# 查看导入结果
docker images从容器提交镜像
将容器的当前状态保存为新镜像:
bash
# 启动容器并做修改
docker run -it ubuntu bash
# 在容器内安装软件...
apt-get install -y curl
exit
# 将容器提交为新镜像
docker commit <容器ID或名称> my-ubuntu:with-curl
# 查看新镜像
docker images my-ubuntu提示:实际工作中应优先使用 Dockerfile 而非
docker commit,Dockerfile 更透明、可维护、可重复构建。
清理本地镜像
bash
# 删除所有悬空(dangling)镜像(没有标签的中间层镜像)
docker image prune
# 删除所有未被容器使用的镜像
docker image prune -a
# 查看 Docker 占用的磁盘空间
docker system df
# 一键清理:容器、镜像、网络、构建缓存
docker system prune -a镜像加速配置
如果拉取镜像速度较慢,参考镜像加速配置章节。
常用官方镜像推荐
| 镜像 | 推荐版本 | 说明 |
|---|---|---|
nginx | nginx:alpine | Web 服务器,alpine 版更小 |
node | node:20-alpine | Node.js 运行环境 |
python | python:3.12-slim | Python 运行环境 |
mysql | mysql:8.0 | MySQL 数据库 |
redis | redis:alpine | Redis 缓存 |
postgres | postgres:16-alpine | PostgreSQL 数据库 |
mongo | mongo:7.0 | MongoDB 数据库 |
ubuntu | ubuntu:22.04 | Ubuntu 系统 |
alpine | alpine:3.19 | 最小化 Linux,约 5MB |
总结
镜像管理的核心命令:
| 操作 | 命令 |
|---|---|
| 查看镜像 | docker images |
| 搜索镜像 | docker search |
| 拉取镜像 | docker pull |
| 查看详情 | docker inspect / docker history |
| 打标签 | docker tag |
| 删除镜像 | docker rmi |
| 导出镜像 | docker save |
| 导入镜像 | docker load |
| 清理镜像 | docker image prune |
下一节将介绍容器的完整管理操作。