網(wǎng)站域名備案在哪里職業(yè)培訓(xùn)機構(gòu)需要什么資質(zhì)
目錄
注意:
腳本內(nèi)容
執(zhí)行效果
注意:
- 以下腳本為shell腳本通過docker/nerdctl進行鏡像獨立打包鏡像的相關(guān)操作
- 腳本內(nèi)倉庫信息和鏡像存取路徑需自行更改
- 需自行創(chuàng)建images.txt并填寫值,并且與腳本位于同級目錄下
[root@master01 sulibao]# ll -rw-r--r-- 1 root root 3621 Feb 17 15:32 images_pull_save_load_tag_push.sh -rw-r--r-- 1 root root 906 Feb 17 15:11 images.txt
- 該腳本讀入單個選項和多個選項,多個選項以英文模式","相隔
- 該腳本不關(guān)系定時任務(wù),使用交互顯示輸出信息,需要保留日志可自行添加定向
腳本內(nèi)容
#!/bin/bash#定義原鏡像倉庫地址、login用戶、login密碼
registry_host="xxx"
registry_username="xxx"
registry_password="xxx"#定義鏡像列表文件,此文件應(yīng)和腳本位于同級目錄下
IMAGE_LIST_FILE="images.txt"#定義鏡像存取目錄
SAVE_DIR=/data/sulibao/images_$(date +"%Y-%m-%d")#定義push的鏡像倉庫地址、login用戶、login密碼
des_registry_host="192.168.2.190:5000"
des_registry_username="xxx"
des_registry_password="xxx"#環(huán)境準備,登錄原鏡像倉庫,確認鏡像列表文件存在
prepare_environment() {mkdir -p "$SAVE_DIR"local login_output=$(docker login "$registry_host" -u "$registry_username" -p "$registry_password" 2>&1)if [ $? != 0 ]; thenecho "Login error: $login_output"echo "exit!!!"return 1fiif [ ! -f "$IMAGE_LIST_FILE" ]; thenecho "指定的鏡像列表文件 $IMAGE_LIST_FILE 不存在,請檢查文件和權(quán)限!"exit 1elif [ ! -s "$IMAGE_LIST_FILE" ]; thenecho "指定的鏡像列表文件 $IMAGE_LIST_FILE 為空,請確保文件包含有效的鏡像列表信息!"exit 1fireturn 0
}#拉取鏡像
pull_images() {echo "開始拉取鏡像!"while IFS= read -r image; doif [[ ! -z "$image" ]]; thenecho "Pulling image: $image"docker pull "$image"if [ $? != 0 ]; thenecho "拉取鏡像 $image 失敗!"fifidone < "$IMAGE_LIST_FILE"echo "鏡像拉取完成!"
}#保存鏡像
save_images() {echo "開始保存鏡像!"while IFS= read -r image; doif [[ ! -z "$image" ]]; thenlocal image_file=$(echo "$image" | tr '/:' '_')echo "Saving image: $image to $SAVE_DIR/$image_file.tar"docker save -o "$SAVE_DIR/$image_file.tar" "$image"if [ $? != 0 ]; thenecho "保存鏡像 $image 失敗!"fi#docker rmi "$image" #此處涉及有一個鏡像遍歷刪除,在保存鏡像包后刪除鏡像,如需要請自行開啟fidone < "$IMAGE_LIST_FILE"echo "鏡像保存完成!"
}#導(dǎo)入鏡像
load_images() {if [ -z "$(find "$SAVE_DIR" -mindepth 1 -print -quit)" ]; thenecho "鏡像存取目錄 $SAVE_DIR 中未發(fā)現(xiàn)鏡像文件,請檢查路徑!"exit 1filocal des_login_output=$(docker login $des_registry_host -u $des_registry_username -p $des_registry_password 2>&1)if [ $? != 0 ]; thenecho "Login error: $des_login_output"echo "exit!!!"exit 1fiecho "開始導(dǎo)入鏡像!"for image_file in "$SAVE_DIR"/*.tar; doif [ -f "$image_file" ]; thenecho "正在導(dǎo)入鏡像 $image_file"docker load -i "$image_file"if [ $? -ne 0 ]; thenecho "導(dǎo)入鏡像 $image_file 失敗!"fifidoneecho "所有 .tar 鏡像導(dǎo)入完成!"
}#替換tag并推送到目標鏡像倉庫
tag_and_push_images() {echo "開始批量修改鏡像標簽并推送!"while IFS= read -r image; doif [ -n "$image" ]; thenimage_suffix="${image#*/}"new_image="${des_registry_host}/${image_suffix}"echo "正在為 $image 添加新標簽 $new_image"docker tag "$image" "$new_image"if [ $? -ne 0 ]; thenecho "為 $image 添加新標簽 $new_image 失敗!"continuefiecho "正在推送新鏡像 $new_image"docker push $new_imageif [ $? -ne 0 ]; thenecho "推送新鏡像 $new_image 失敗!"fidocker rmi $new_imagefidone < "$IMAGE_LIST_FILE"
}#操作選項
show_menu() {echo "Select the action you want to perform:"echo "1.Pull images."echo "2.Save images to $SAVE_DIR"echo "3.Load images from $SAVE_DIR"echo "4.Replace tag for images and push to target registry"echo "5.Exit."read -p "Please enter the operation number:" choiceIFS=',' read -ra choices <<< "$choice"for choice in "${choices[@]}"; docase $choice in1)pull_images;;2)save_images ;;3)load_images;;4)tag_and_push_images;;5) echo "You have exited the program."exit 0;; *)echo "Invalid input, please enter option number."show_menu;;esacdone
}prepare_environment
show_menu
執(zhí)行效果
- 完整執(zhí)行過程
[root@master01 sulibao]# bash images_pull_save_load_tag_push.sh
Select the action you want to perform:
1.Pull images.
2.Save images to /data/sulibao/images_2025-02-17
3.Load images from /data/sulibao/images_2025-02-17
4.Replace tag for images and push to target registry
5.Exit.
Please enter the operation number:1,2,3,4開始拉取鏡像!
Pulling image: xxx/nginx:1.26.3
1.26.3: Pulling from xxx/nginx
Digest: sha256:52892d0d4f284526aa1d72ac42fd194c3e887580f60080ded2686d49037c6941
Status: Image is up to date for xxx/nginx:1.26.3
xxx/nginx:1.26.3
......
鏡像拉取完成!開始保存鏡像!
Saving image: xxx/nginx:1.26.3 to /data/sulibao/images_2025-02-17/xxx_nginx_1.26.3.tar
......
鏡像保存完成!開始導(dǎo)入鏡像!
正在導(dǎo)入鏡像 /data/sulibao/images_2025-02-17/xxx_nginx_1.26.3.tar
Loaded image: xxx/nginx:1.26.3
......
所有 .tar 鏡像導(dǎo)入完成!開始批量修改鏡像標簽并推送!
正在為 xxx/nginx:1.26.3 添加新標簽 192.168.2.190:5000/su03/nginx:1.26.3
正在推送新鏡像 192.168.2.190:5000/su03/nginx:1.26.3
The push refers to repository [192.168.2.190:5000/su03/nginx]
d9c572c95560: Layer already exists
8190a7b44dae: Layer already exists
a676f50129b7: Layer already exists
867f455c499d: Layer already exists
043f2436492e: Layer already exists
263be8d15880: Layer already exists
7914c8f600f5: Layer already exists
1.26.3: digest: sha256:ed94607a3100cd7274aa915678f8e6d1effba2919198044654000383d77035ac size: 1778
Untagged: 192.168.2.190:5000/su03/nginx:1.26.3
Untagged: 192.168.2.190:5000/su03/nginx@sha256:ed94607a3100cd7274aa915678f8e6d1effba2919198044654000383d77035ac[root@master01 sulibao]# ll images_2025-02-17/
total 1290584
-rw------- 1 root root 781347328 Feb 17 16:35 xxx_mysql_8.0.41.tar
-rw------- 1 root root 196080128 Feb 17 16:35 xxx_nginx_1.26.3.tar
-rw------- 1 root root 344122368 Feb 17 16:35 xxx_postgresql_13.16.0.tar
- 若鏡像列表文件不存在或為空
[root@master01 sulibao]# bash images_pull_save_load_tag_push.sh
指定的鏡像列表文件 images.txt 不存在,請檢查文件和權(quán)限![root@master01 sulibao]# bash images_pull_save_load_tag_push.sh
指定的鏡像列表文件 images.txt 為空,請確保文件包含有效的鏡像列表信息!
- 僅僅需要執(zhí)行l(wèi)oad_image時,請確保鏡像存取目錄中存在有你需要的鏡像包。無論是你手動存放的包還是通過腳本pull_and_save的包,需要以".tar"為后綴,若后綴不同,請自行修改腳本。
[root@master01 sulibao]# bash images_pull_save_load_tag_push.sh
Select the action you want to perform:
1.Pull images.
2.Save images to /data/sulibao/images_2025-02-17
3.Load images from /data/sulibao/images_2025-02-17
4.Replace tag for images and push to target registry
5.Exit.
Please enter the operation number:3
鏡像存取目錄 /data/sulibao/images_2025-02-17 中未發(fā)現(xiàn)鏡像文件,請檢查路徑![root@master01 sulibao]# ll #此時僅有prepare_environment函數(shù)創(chuàng)建的鏡像存取目錄,實際上無數(shù)據(jù),執(zhí)行失敗的
-rw-r--r-- 1 root root 4695 Feb 17 16:45 images_pull_save_load_tag_push.sh
-rw-r--r-- 1 root root 159 Feb 17 16:53 images.txt
drwxr-xr-x 2 root root 6 Feb 17 16:54 images_2025-02-17