随笔 - 61,  文章 - 1,  评论 - 0,  阅读 - 58489

docker 命令:

导出镜像:

runoob@runoob:~$ docker save -o my_ubuntu_v3.tar runoob/ubuntu:v3
runoob@runoob:~$ ll my_ubuntu_v3.tar
-rw------- 1 runoob runoob 142102016 Jul 11 01:37 my_ubuntu_v3.ta

导入镜像:

docker load  -i flannel_flannel_v0.25.1.tar

批量导出

#!/bin/bash  
  
# 设置导出镜像的目录  
EXPORT_DIR="/root/images"  
  
# 确保导出目录存在  
mkdir -p "$EXPORT_DIR"  
  
# 获取所有的镜像并导出  
IMAGES=$(docker images --format '{{.Repository}}:{{.Tag}}')  
  
# 遍历所有的镜像  
for IMAGE in $IMAGES; do  
    # 分割镜像仓库名和标签  
    IFS=":" read -r REPOSITORY TAG <<< "$IMAGE"  
      
    # 将仓库名中的斜杠替换为下划线  
    SAFE_REPOSITORY=${REPOSITORY//\//_}  
      
    # 构造导出文件的名称  
    EXPORT_FILE="${SAFE_REPOSITORY}_${TAG}.tar"  
    EXPORT_PATH="$EXPORT_DIR/$EXPORT_FILE"  
      
    # 检查镜像是否存在,如果存在则导出  
    if docker image inspect "${REPOSITORY}:${TAG}" >/dev/null 2>&1; then  
        docker save -o "$EXPORT_PATH" "${REPOSITORY}:${TAG}"  
        echo "Exported ${REPOSITORY}:${TAG} to $EXPORT_PATH"  
    else  
        echo "Error: ${REPOSITORY}:${TAG} does not exist"  
    fi  
done

echo "Export process completed."

批量导入

#!/bin/bash  
  
# 设置包含要导入镜像的 .tar 文件的目录  
IMPORT_DIR="/root/images"  
  
# 检查目录是否存在  
if [ ! -d "$IMPORT_DIR" ]; then  
    echo "Error: Directory $IMPORT_DIR does not exist."  
    exit 1  
fi  
  
# 遍历目录下的所有 .tar 文件并导入  
for TAR_FILE in "$IMPORT_DIR"/*.tar; do  
    if [ -f "$TAR_FILE" ]; then  
        echo "Importing $TAR_FILE..."  
        docker load -i "$TAR_FILE"  
        if [ $? -eq 0 ]; then  
            echo "Successfully imported $TAR_FILE"  
        else  
            echo "Failed to import $TAR_FILE"  
        fi  
    fi  
done  
  
echo "Import process completed."
 
posted on   天外来客I  阅读(309)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示