Skip to content

Docker镜像相关命令

1. 镜像基础操作

1.1 拉取镜像

bash
# 从仓库拉取镜像(默认最新标签)
docker pull nginx

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

# 从私有仓库拉取
docker pull registry.example.com/myapp:v2

1.2 查看镜像

bash
# 列出所有镜像
docker images

# 显示镜像详细信息
docker image inspect nginx:latest

# 显示镜像构建历史
docker history nginx

# 搜索公共镜像
docker search ubuntu

2. 镜像构建

2.1 基础构建

bash
# 构建镜像(使用当前目录 Dockerfile)
docker build -t myapp:1.0 .

# 指定不同 Dockerfile
docker build -f Dockerfile.prod -t myapp:prod .

# 构建时不使用缓存
docker build --no-cache -t fresh-build .

2.2 高级构建

bash
# 多阶段构建
docker build --target builder-stage -t builder .

# 构建参数传递
docker build --build-arg NODE_ENV=production -t myapp:prod .

# 设置构建平台
docker build --platform linux/arm64 -t arm-image .

3. 镜像管理

3.1 镜像标记

bash
# 添加新标签
docker tag myapp:1.0 registry.example.com/myapp:latest

# 修改镜像名称
docker tag old-name:1.0 new-name:2.0

3.2 删除镜像

bash
# 删除单个镜像
docker rmi nginx:1.23

# 强制删除(被容器使用的镜像)
docker rmi -f myapp:test

# 删除所有悬空镜像(未被标记且无容器使用)
docker image prune

# 删除指定名称模式的镜像
docker images | grep "none" | awk '{print $3}' | xargs docker rmi

4. 镜像导入导出

4.1 保存/加载

bash
# 保存镜像为 tar 文件(保留完整层结构)
docker save -o nginx.tar nginx:latest

# 加载镜像
docker load -i nginx.tar

# 批量导出所有镜像
docker save $(docker images -q) -o all-images.tar

4.2 导出/导入容器快照

bash
# 导出容器文件系统(不包含元数据/层)
docker export my_container > container.tar

# 从容器快照创建镜像
cat container.tar | docker import - my_image:snapshot

5. 镜像分发

5.1 推送镜像

bash
# 登录镜像仓库
docker login registry.example.com

# 推送镜像到仓库
docker push registry.example.com/myapp:1.0

# 批量推送多个标签
docker push registry.example.com/myapp:{1.0,latest}

5.2 镜像迁移

bash
# 直接跨服务器传输
docker save myapp:1.0 | ssh user@server 'docker load'

6. 镜像分析

bash
# 查看镜像层大小
docker history --no-trunc myapp:1.0

# 分析镜像内容
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock wagoodman/dive myapp:1.0

# 检查镜像元数据
docker image inspect --format '{{.Config.Entrypoint}}' myapp:1.0

7. 镜像优化

bash
# 压缩镜像体积
docker export my_container | docker import --change 'CMD ["/app"]' - myapp:minimal

# 合并镜像层(需要 Docker 20.10+)
docker build --squash -t flattened-image .

8. 镜像安全

bash
# 扫描镜像漏洞
docker scan myapp:1.0

# 查看镜像签名验证
docker trust inspect --pretty registry.example.com/myapp

# 查看镜像摘要
docker images --digests

9. 镜像清理

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

# 删除早于指定时间的镜像
docker image prune -a --filter "until=24h"

# 按标签过滤删除
docker rmi $(docker images --filter "reference=*:test" -q)

10. 特殊镜像操作

bash
# 从运行容器创建新镜像
docker commit -m "Added config" my_container myapp:config-update

# 构建时跳过平台验证(危险操作)
docker build --platform=linux/amd64 --allow platform.mismatch -t cross-image .

# 查看镜像构建缓存
docker builder prune --filter type=build-cache

注意事项

  1. docker savedocker export 区别:

    • save 保存完整镜像(包含层结构)
    • export 导出容器文件系统快照
  2. 镜像命名规范:

    • 私有仓库格式:<registry>/<project>/<name>:<tag>
    • 官方镜像格式:<name>:<tag>
  3. 生产环境建议:

    • 始终使用明确版本标签(避免使用 latest)
    • 定期扫描镜像漏洞
    • 设置镜像保留策略
/src/technology/docker/%E9%95%9C%E5%83%8F%E7%9B%B8%E5%85%B3%E5%91%BD%E4%BB%A4.html