SoFunction
Updated on 2025-03-09

The specific use of docker save and docker load

Let me introduce it in detaildocker saveanddocker loadUse of commands:

docker save command:

  • Function: Save one or more Docker images as tar archives
  • Common formats:
# Basic syntaxdocker save [options] image [image...] > 

# Save a single imagedocker save nginx:latest > 

# Save multiple imagesdocker save nginx:latest redis:latest > 

# Use -o Parametersdocker save -o  mysql:5.7

# Actual project exampledocker save \
  mysql:5.7 \
  redis:latest \
  nginx:alpine \
  > project_images.tar

docker load command:

  • Function: Load the image from the tar archive file
  • Common formats:
# Basic syntaxdocker load < 

# Use -i parameterdocker load -i 

# Check the loading progressdocker load --input  --quiet=false

# Actual project examplecat project_images.tar | docker load
  • Common combination examples:
# 1. Export all imagesdocker save $(docker images -q) > all_images.tar

# 2. Export the specified image and its related dependenciesdocker save $(docker history -q nginx:latest | tac) > nginx_with_deps.tar

# 3. Export the image in docker-composedocker save $(docker compose images -q) > compose_images.tar
  • Error handling and verification:
# Verify when saving the imagedocker save nginx:latest | gzip > 
if [ $? -eq 0 ]; then
    echo "Save successful"
else
    echo "Save failed"
fi

# Verify when loading the imagedocker load < 
if [ $? -eq 0 ]; then
    echo "Load successful"
else
    echo "Load failed"
fi
  • Advanced usage scenarios:
#!/bin/bash
# save_images.sh

# Set variablesOUTPUT_DIR="docker_images"
mkdir -p $OUTPUT_DIR

# Get all imagesimages=$(docker images --format "{{.Repository}}:{{.Tag}}" | grep -v "<none>")

# Save the mirror one by onefor img in $images; do
    # Replace / to create a valid file name    filename=$(echo $img | tr '/' '_')
    echo "Saving $img to $OUTPUT_DIR/$"
    docker save $img > "$OUTPUT_DIR/$"
done
  • FAQs and Solutions:
# 1. File too large: Use gzip to compressdocker save nginx:latest | gzip > 
gunzip -c  | docker load

# 2. Verify mirror integrity# Calculate the checksum during savingdocker save nginx:latest | tee  | md5sum > nginx.md5

# Verification on loadingmd5sum -c nginx.md5 && docker load < 

# 3. Batch processing with progress displaydocker save nginx:latest | pv > 
  • Example of practical scripts:
#!/bin/bash
# docker_image_backup.sh

#ConfigurationBACKUP_DIR="/backup/docker/images"
DATE=$(date +%Y%m%d)
BACKUP_FILE="$BACKUP_DIR/docker_images_$"

# Create a backup directorymkdir -p $BACKUP_DIR

# Get all non-empty imagesIMAGES=$(docker images --format "{{.Repository}}:{{.Tag}}" | grep -v "<none>")

# Save the imageecho "Starting backup..."
docker save $IMAGES > $BACKUP_FILE

# Compressiongzip $BACKUP_FILE

echo "Backup completed: ${BACKUP_FILE}.gz"

Notes:

  • Space considerations:
# Check the mirror sizedocker images --format "{{.Repository}}:{{.Tag}} - {{.Size}}"

# Check tar file sizedu -sh 
  • Performance considerations:
# Use compression to reduce file sizedocker save myimage | gzip > 

# Use pv to display progressdocker save myimage | pv | gzip > 
  • Safety Considerations:
# Add file permission controlchmod 600 

# Add a checksumsha256sum  > .sha256

Recommended usage:

  • Clean up unnecessary images before saving them
  • Use meaningful file naming
  • Keep the version record
  • Clean old backup files regularly
  • Consider using compression to save space

This is the introduction to this article about the specific use of docker save and docker load. For more related docker save and docker load content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!