Docker saves the image locally and loads the local image file

Table of contents

1. Applicable situations

2. Save the image to your local machine 

1. View existing image files

2. Save the image as a local file

Save command one

Save command two

Test saving the image based on the image ID

3. Load local image

Loading command one

Loading command two

Load local image saved by image ID

4. Batch save and load image scripts

Batch save images to local script

Batch load image tar script


1. Applicable situations

Generally, when we download Docker images, we will configure domestic image sources to speed up the download, but sometimes there are the following requirements:

  • The device cannot be connected to the Internet, and the image cannot be downloaded directly. You need to download the image from other devices and copy it to the device that is not currently connected to the Internet.
  • The image on the device is in a private warehouse and cannot be downloaded. It needs to be copied to the current device.

When encountering the above situations, you can refer to the following operations ( attached are image batch saving and batch import scripts )

2. Save the image to your local machine 

1. View existing image files

docker images

 As shown in the figure below, all images in the current device

2. Save the image as a local file

   important point:

  1. The saved image must be a tar type file
  2. If the image ID is used in the save command, REPOSTITORY and TAG are none when importing the tar file, and the image tag needs to be reset using the image ID; if the image name is used, this problem will not occur.
  • Save command one

#指令一
docker save -o /data/docker/nginx_latest.tar nginx

#Instruction one

docker save -o

     /data/docker/nginx_latest.tar Save file name, must be tar type, save path can be added

     nginx The image to be saved (either name or ID, you can add TAG after the name to specify the version)

  • Save command two

#指令二
docker save nginx > nginx_latest01.tar

#Instruction 2

docker save

        nginx image to be saved (either name or ID)

        >

        nginx_latest01.tar The image to be saved (either name or ID, you can add TAG after the name to specify the version)

 Execute command one and command two respectively

After success, save to local files named nginx_latest.tar and nginx_latest01.tar

  • Test saving the image based on the image ID

In order to test " if the image ID is used in the save command, REPOSTITORY and TAG are none when importing the tar file, and the image tag needs to be reset using the id of the image", the local image saved by IMAGE ID will be tested separately when loading the image.

docker save -o nginx_latest_byID.tar 89da1fb6dcb9

3. Load local image

We have a local image file. When needed, we can copy the saved local image to the required device, and use docker load to import the locally saved image into docker again.

  • Loading command one

#指令一
docker load < nginx_latest.tar

 The following demonstration is to restore the same image after deletion

  • Loading command two

#指令二
docker load --input nginx_latest.tar

The basic operation of command 2 is the same as command 1.

  • Load local image saved by image ID

The local image file saved by IMAGE ID is nginx_latest_byID.tar. After loading, REPOSTITORY and TAG are none. The following is the load test.

4. Batch save and load image scripts

Batch save and load image script download icon-default.png?t=N6B9https://download.csdn.net/download/DreamEhome/88146817

  • Batch save images to local script

Create a new save_images.sh and write the following content:

#!/bin/bash
  
  
# 获取到 "image:tag" 格式的镜像名
IMG_NAME=`docker images | grep -v TAG | awk '{print $1":"$2}'`
# 如果原本镜像名中存在 "/" 是需要去掉的
echo $IMG_NAME | awk '{gsub(/ /,"\n",$0)} {print $0}'

# 定义镜像存放目录
DIR="/data/docker/image_save"
if [ ! -d "$DIR" ]; then
  echo -e "\033[34m${DIR}\033[0m 不存在"
  mkdir -p "$DIR"
  echo -e "\033[34m${DIR}\033[0m 已创建"
else
  echo -e "\033[34m${DIR}\033[0m 已存在"
fi
echo ""
for IMAGE in $IMG_NAME
do
  echo -e "正在保存 \033[33m${IMAGE}\033[0m"
  SAVE_NAME=`echo $IMAGE | awk -F: '{print $1"_"$2}' | sed 's/\//_/g'`
  docker save $IMAGE -o ${DIR}/${SAVE_NAME}.tar
  echo -e "已保存到 \033[34m${DIR}/\033[31m${SAVE_NAME}.tar\033[0m"
  echo ""
done

The newly added script does not have execution permissions. The current test directly sets permissions to 777.

#修改文件权限为简单粗暴的所有者、所属组和其他人都有读写执行权限
chmod 777 save_images.sh

 Execution effect demonstration:

  • Batch load image tar script

Create a new load_images.sh, and other operations are the same as save_image.sh.

#!/bin/bash
  
# 在此处填写镜像文件的保存目录
IMAGE_DIR="/data/docker/image_save"
  
for IMAGE in `ls $IMAGE_DIR`
do
  echo -e "正在导入镜像 \033[33m$IMAGE\033[0m"
  docker load -i ${IMAGE_DIR}/${IMAGE}
  echo -e "已成功导入镜像 \033[33m$IMAGE\033[0m"
  echo ""
done

Execution effect demonstration:

 

Guess you like

Origin blog.csdn.net/DreamEhome/article/details/132054143