备份几个常见的docker相关脚本

1. 脚本1-导出所有镜像的 shell:

#!/bin/bash

# 获取所有镜像的列表
images=$(docker images --format "{{.Repository}}:{{.Tag}}")

# 遍历每个镜像并导出
for image in $images; do
    # 提取镜像名和标签
    repo=$(echo $image | cut -d':' -f1)
    tag=$(echo $image | cut -d':' -f2)
    
    # 去掉标签中的 '.'
    clean_tag=$(echo $tag | tr -d '.')
    
    # 生成导出文件名
    output_file="${repo//\//-}-${clean_tag}.tar"
    
    # 导出镜像
    docker save -o $output_file $image
    
    echo "Exported $image to $output_file"
done

echo "镜像导出成功"

2. 脚本2-导入目录中的所有 tar 镜像的 shell:

#!/bin/bash

# 获取所有 .tar 文件
tar_files=$(ls *.tar)

# 遍历每个 .tar 文件并导入
for file in $tar_files; do
    # 导入镜像
    docker load -i $file
    echo "Loaded image from $file"
done

echo "镜像导入成功"

3. 强制停止并删除所有docker容器

docker stop $(docker ps -aq) && docker rm $(docker ps -aq)

4. 强制删除所有镜像

docker rmi -f $(docker images -q)

5. 更安全的强制删除操作

# 删除悬空镜像(无标签的临时镜像)
docker image prune -a -f

# 删除剩余镜像(需先确保无容器依赖)
docker rmi -f $(docker images -q)

6. 命令1-不同环境下,拉取 linux 平台下可运行的镜像:

docker pull --platform linux/amd64 镜像:TAG