Batch export and import docker image offline package script under Linux - The Road to Dreaming

Implementation principle

1. Write an image manifest file to save an image address in each line.

2. Export and import images in batches by looping through the contents of the image manifest file

Prepare image manifest file

File name/home/image_list.txt

centos:7.9
ubuntu: 18.04
nginx:1.24.0-alpine
openjdk:8-alpine

Export images in batches as offline packages

Script file docker-save-images.sh

#!/bin/bash
# docker-save-images.sh
# 批量导出docker镜像,保存为tar.gz

# 指定包含镜像名称的文件  
file_list="/home/image_list.txt"  

# 遍历文件列表中的每个镜像名称  
while IFS= read -r image; do  
    # 检查镜像是否存在  
    if docker images -q "$image" >/dev/null 2>&1; then  
        # 导出镜像  
        docker save -o "$image.tar.gz" "$image"  

        # 打印导出成功的消息  
        echo "Successfully save image: $image"  
    fi  
done < "$file_list"

Import image offline packages in batches

Script file load-docker-images.sh

#!/bin/bash
# load-docker-images.sh
# 批量导入docker离线镜像包tar.gz

# 指定包含镜像文件路径的文本文件  
file_list="/home/file_list.txt"  

# 遍历文件列表中的每个文件  
while IFS= read -r file; do  
    # 检查文件是否存在且为.tar文件  
    if [[ -f "$file" && "${file##*.}" == "tar.gz" ]]; then  
        # 加载镜像文件  
        docker load -i "$file"  

        # 打印导入成功的消息  
        echo "Successfully load image: $(basename -- "$file" .tar.gz)"  
    fi  
done < "$file_list"

 

 

Guess you like

Origin blog.csdn.net/qq_34777982/article/details/134418578