镜像基础 #

什么是Docker镜像? #

Docker镜像是一个只读模板,包含创建Docker容器所需的文件系统和运行应用所需的所有内容:代码、运行时、库、环境变量和配置文件。

镜像特点 #

特点 说明
只读性 镜像构建后不可修改
分层存储 每层独立,便于复用和传输
版本管理 通过标签管理不同版本
轻量级 共享基础层,节省存储空间

获取镜像 #

从仓库拉取镜像 #

bash
# 拉取最新版本镜像
docker pull nginx

# 拉取指定版本镜像
docker pull nginx:1.23

# 拉取指定平台镜像
docker pull --platform linux/arm64 nginx:alpine

# 拉取所有标签
docker pull -a nginx

拉取过程解析 #

text
┌─────────────────────────────────────────────────────┐
│                   镜像拉取流程                       │
├─────────────────────────────────────────────────────┤
│                                                     │
│  1. 检查本地是否存在镜像                            │
│           │                                         │
│           ↓                                         │
│  2. 从Registry获取镜像manifest                      │
│           │                                         │
│           ↓                                         │
│  3. 检查哪些层需要下载                              │
│           │                                         │
│           ↓                                         │
│  4. 下载缺失的层                                    │
│           │                                         │
│           ↓                                         │
│  5. 组装镜像                                        │
│                                                     │
└─────────────────────────────────────────────────────┘

从Dockerfile构建 #

bash
# 构建镜像
docker build -t myapp:v1.0 .

# 指定Dockerfile路径
docker build -t myapp:v1.0 -f Dockerfile.prod .

# 构建时传递参数
docker build --build-arg VERSION=1.0 -t myapp:v1.0 .

查看镜像 #

列出本地镜像 #

bash
# 列出所有镜像
docker images

# 列出所有镜像(包括中间层)
docker images -a

# 只显示镜像ID
docker images -q

# 显示镜像摘要
docker images --digests

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

# 过滤镜像
docker images --filter "dangling=true"
docker images --filter "reference=nginx"

输出示例 #

text
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
nginx         latest    605c77e624dd   2 weeks ago    141MB
nginx         alpine    c31fe73098a4   2 weeks ago    23.5MB
redis         latest    7614ae9453d1   3 weeks ago    113MB
mysql         8.0       3218b38490ce   4 weeks ago    516MB

查看镜像详情 #

bash
# 查看镜像详细信息
docker inspect nginx:latest

# 查看特定信息
docker inspect --format='{{.Architecture}}' nginx:latest
docker inspect --format='{{.Size}}' nginx:latest
docker inspect --format='{{.Config.Env}}' nginx:latest

查看镜像历史 #

bash
# 查看镜像构建历史
docker history nginx:latest

# 不截断输出
docker history --no-trunc nginx:latest

# 格式化输出
docker history --format "{{.CreatedBy}}" nginx:latest

镜像标签管理 #

添加标签 #

bash
# 为镜像添加标签
docker tag nginx:latest myregistry.com/nginx:latest

# 添加多个标签
docker tag nginx:latest nginx:stable
docker tag nginx:latest nginx:1.23

标签命名规范 #

text
[registry/][username/]repository[:tag]

示例:
nginx                    # 官方镜像,默认标签latest
nginx:alpine             # 官方镜像,指定标签
library/nginx:latest     # 完整写法
username/nginx:latest    # 用户镜像
myregistry.com/nginx     # 私有仓库镜像

删除镜像 #

删除本地镜像 #

bash
# 通过镜像名删除
docker rmi nginx:latest

# 通过镜像ID删除
docker rmi 605c77e624dd

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

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

# 删除所有镜像
docker rmi $(docker images -q)

# 删除悬空镜像(dangling images)
docker image prune -f

悬空镜像 #

悬空镜像是指没有标签指向的镜像层:

bash
# 查看悬空镜像
docker images -f "dangling=true"

# 删除悬空镜像
docker image prune

镜像导入导出 #

导出镜像 #

bash
# 导出单个镜像
docker save -o nginx.tar nginx:latest

# 导出多个镜像
docker save -o images.tar nginx:latest redis:latest

# 导出并压缩
docker save nginx:latest | gzip > nginx.tar.gz

导入镜像 #

bash
# 从文件导入
docker load -i nginx.tar

# 从压缩文件导入
docker load < nginx.tar.gz

# 从URL导入
docker load < <(curl -s http://example.com/nginx.tar)

镜像搜索 #

搜索Docker Hub镜像 #

bash
# 搜索镜像
docker search nginx

# 限制结果数量
docker search --limit 5 nginx

# 只显示官方镜像
docker search --filter is-official=true nginx

# 显示星标数大于指定值的镜像
docker search --filter stars=100 nginx

输出示例 #

text
NAME                             DESCRIPTION                                     STARS     OFFICIAL
nginx                            Official build of Nginx.                         17000     [OK]
jwilder/nginx-proxy              Automated Nginx reverse proxy...                 2100
richarvey/nginx-php-fpm          Container running Nginx + PHP-FPM...             800

镜像存储分析 #

查看磁盘使用 #

bash
# 查看Docker磁盘使用情况
docker system df

# 详细信息
docker system df -v

输出示例 #

text
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          15        5         5.2GB     3.1GB (59%)
Containers      5         3         128MB     64MB (50%)
Local Volumes   3         2         256MB     128MB (50%)
Build Cache     10        0         512MB     512MB (100%)

镜像清理 #

清理策略 #

bash
# 清理悬空镜像
docker image prune

# 清理所有未使用的镜像
docker image prune -a

# 设置过期时间
docker image prune -a --filter "until=24h"

# 清理所有未使用资源
docker system prune

# 清理所有资源(包括镜像)
docker system prune -a

镜像最佳实践 #

1. 选择合适的基础镜像 #

bash
# 使用Alpine版本(最小)
FROM alpine:3.18

# 使用slim版本(较小)
FROM node:18-slim

# 使用官方镜像
FROM nginx:alpine

2. 使用特定版本标签 #

bash
# 不推荐
FROM nginx:latest

# 推荐
FROM nginx:1.23.4-alpine

3. 定期清理镜像 #

bash
# 定期执行清理
docker image prune -a --filter "until=168h"

4. 使用.dockerignore #

text
# .dockerignore
node_modules
npm-debug.log
Dockerfile
.dockerignore
.git
.gitignore
README.md
.env

镜像命名规范 #

官方镜像 #

text
[镜像名]:[标签]

示例:
nginx:latest
nginx:alpine
nginx:1.23.4

用户镜像 #

text
[用户名]/[镜像名]:[标签]

示例:
username/myapp:v1.0
username/webapp:latest

私有仓库镜像 #

text
[仓库地址]/[用户名]/[镜像名]:[标签]

示例:
registry.example.com/team/myapp:v1.0
myregistry.com/nginx:latest

小结 #

本节学习了Docker镜像的基本操作:

  • 从仓库拉取镜像
  • 查看和管理本地镜像
  • 镜像标签管理
  • 镜像导入导出
  • 镜像清理和最佳实践

下一步 #

接下来,让我们学习 Dockerfile编写,了解如何编写Dockerfile构建自定义镜像。