【原创实践】Docker 镜像批量导出镜像与导入镜像

1. 批量保存镜像到 .tar 文件

bash 复制代码
#!/bin/bash

# 保存目录
SAVE_DIR=/root/docker_images_backup
mkdir -p $SAVE_DIR

# 获取本地所有镜像列表 (Repository:Tag + IMAGE ID)
docker images --format "{{.Repository}}:{{.Tag}} {{.ID}}" | while read img id; do

    # 如果 TAG 是 <none>,使用 IMAGE ID 代替
    if [[ $img == *"<none>"* ]]; then
        img=$id
    fi

    # 生成文件名,将 / 和 : 替换为 _
    filename=$(echo $img | sed 's/[\/:]/_/g').tar

    echo "Saving $img -> $SAVE_DIR/$filename"

    # 执行 docker save 导出镜像
    docker save -o $SAVE_DIR/$filename $img

done

✅ 功能说明:

  • 自动处理 <none> 标签的镜像。

  • 每个镜像导出为独立 tar 文件,命名格式 REPOSITORY_TAG.tar

  • 方便迁移和备份。


2. 批量加载镜像

bash 复制代码
#!/bin/bash

# 镜像 tar 文件目录
LOAD_DIR=/root/docker_images_backup

# 遍历目录下所有 tar 文件
for tarfile in $LOAD_DIR/*.tar; do
    echo "Loading $tarfile ..."
    docker load -i $tarfile
done

✅ 功能说明:

  • 自动加载目录下所有 .tar 文件到本地 Docker。

  • 可以用于迁移到新服务器或重建环境。


3. 使用示例

  1. 保存镜像:
bash 复制代码
bash save_all_images.sh
  1. 在新服务器加载镜像:
bash 复制代码
bash load_all_images.sh
相关推荐
Elastic 中国社区官方博客7 小时前
教程:使用 ES|QL 进行威胁狩猎
大数据·运维·数据库·elasticsearch·搜索引擎·全文检索·安全威胁分析
bwz999@88.com7 小时前
Ubuntu Server 24.04 设置中文
linux·运维·ubuntu
Cx330❀8 小时前
【Linux网络】网络层协议 IP :从网络层原理到 Linux 内核源码
linux·运维·服务器·网络·tcp/ip·ai·ai编程
志栋智能8 小时前
运维超自动化的变更管理实践
运维·自动化
全栈攻略8 小时前
Docker 中 ROS2 工作流 Topic 验证与常用命令指南
运维·docker·容器
Gl�ria9 小时前
Linux 清理日志常用命令
linux·运维·服务器
j7~11 小时前
【Linux网络加餐(二)】《进程组,会话,控制终端,作业控制和守护进程》---详解
linux·运维·服务器·守护进程·会话·进程组·控制终端
名字还没想好☜11 小时前
Docker 数据卷实战:volume、bind mount、tmpfs 到底怎么选,数据持久化与权限坑
运维·docker·容器·kubernetes
天道jimmy11 小时前
VulnHub 系列:Prime,1
linux·运维·服务器