Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Docker commands Guide – docker save with examples

Here’s a complete tutorial on docker save and docker load, covering what they do, examples, and use cases.


What are docker save and docker load?

  • docker save: This command exports Docker images (with all layers, tags, and metadata) as a tarball file. You can then transfer or archive the image.
  • docker load: This command restores images from a tarball created with docker save.

Key Features:

  • docker save: Backup and transfer images without relying on a Docker registry.
  • docker load: Import images from tarballs on any machine.
  • Supports compressed tarballs (.tar.gz).

docker save Command Syntax

docker save [OPTIONS] IMAGE [IMAGE...]

Options:

  • -o, --output: Write the tarball to a file instead of standard output.

Examples of docker save

1. Save a Single Image to a Tarball

docker save -o my_image.tar my_image:latest

This saves the my_image:latest to a tar file named my_image.tar.


2. Save Multiple Images to a Single Tarball

docker save -o images_backup.tar my_image:latest alpine:3.14 nginx:latest

This saves multiple images (my_image, alpine, and nginx) to a single tar file.


3. Save an Image and Compress It

docker save my_image:latest | gzip > my_image.tar.gz

This saves the image and compresses it into my_image.tar.gz.


4. Use docker save with Standard Output

docker save my_image:latest > my_image.tar

This saves the image and writes the output to a tar file.


5. Save and Transfer an Image to Another System

docker save -o my_image.tar my_image:latest
scp my_image.tar user@remote:/path/to/destination

This saves the image to my_image.tar and transfers it to a remote machine using scp.


6. Automate Image Backup with a Script

#!/bin/bash
docker save -o /backups/my_app_$(date +%Y%m%d).tar my_app:latest
echo "Image backed up as my_app_$(date +%Y%m%d).tar"

This script saves my_app:latest with a timestamped filename.


7. Check the Content of a Saved Tarball

tar -tvf my_image.tar

This lists the contents of the tar file, showing layers and metadata.


docker load Command Syntax

docker load [OPTIONS]

Options:

  • -i, --input: Load from a file instead of standard input.
  • --quiet: Suppress verbose output during the load process.

Examples of docker load

1. Load an Image from a Tarball

docker load -i my_image.tar

This loads the image from my_image.tar into the local Docker repository.


2. Load a Compressed Tarball

gunzip < my_image.tar.gz | docker load

This decompresses my_image.tar.gz and loads the image.


3. Load an Image from Standard Input

cat my_image.tar | docker load

This pipes the tar file into docker load.


4. Load Multiple Images from a Single Tarball

docker load -i images_backup.tar

If images_backup.tar contains multiple images, they will all be loaded into Docker.


5. Quietly Load an Image

docker load -i my_image.tar --quiet

Suppresses verbose output while loading the image.


6. Automate Image Loading with a Script

#!/bin/bash
for tarfile in /backup/images/*.tar; do
  docker load -i $tarfile
  echo "Loaded image from $tarfile"
done

Use Cases for docker save and docker load

1. Backup and Restore Images

  • Backup images to tar files for disaster recovery.
  • Restore images after a system upgrade or failure.
  • Example: Save a production image and reload it on a fresh Docker host.

2. Transfer Images Between Systems

  • Move images between development, staging, and production environments without pushing them to Docker Hub.
  • Example: Transfer images to an air-gapped environment.

3. CI/CD Pipelines

  • Preload common images on build agents for faster builds.
  • Example: Use docker save to cache images and docker load to reuse them in CI/CD.

4. Air-Gapped Deployments

  • Use docker save to transfer images into restricted environments without internet access.
  • Example: Load images into an offline server.

5. Sharing Custom Images

  • Share custom-built Docker images with clients or collaborators as tar files.
  • Example: Send a custom application image as my_app.tar.gz.

6. Disaster Recovery and Migration

  • Use docker save to migrate images to a new host.
  • Example: Move legacy Docker images to a new infrastructure.

List of Common docker save and docker load Commands

CommandDescription
docker save -o my_image.tar my_image:latestSave an image to a tar file
`docker save my_image:latestgzip > my_image.tar.gz`
docker save -o images_backup.tar nginx alpineSave multiple images to a single tarball
docker load -i my_image.tarLoad an image from a tar file
`gunzip < my_image.tar.gzdocker load`
docker load < my_image.tarLoad an image using standard input
docker save -o backup.tar my_app:1.0Save a specific image tag

Best Practices for Using docker save and docker load:

  1. Use meaningful filenames when saving images (e.g., my_app_backup_20250207.tar).
  2. Compress large images with gzip or xz to reduce file size.
  3. Verify the loaded image with docker images to ensure it imported successfully.
  4. Automate image backups in scripts for regular snapshots.
  5. Check tarball integrity before loading (tar -tvf my_image.tar).

Common Errors and Solutions

  1. “No such file or directory”
    → Ensure the tar file exists and the path is correct.
  2. “Invalid tar file”
    → Verify the tarball was created with docker save and is not corrupted.
  3. “Image not found after loading”
    → Check the output of docker load to confirm the image and tag.
  4. “Permission denied”
    → Run the command with sudo or check your user permissions.

Combining docker save and docker load in Workflows

Backup and Restore Workflow

  1. Save the image: docker save -o my_image.tar my_image:latest
  2. Transfer and load the image: docker load -i my_image.tar

Automate Backup and Restore

#!/bin/bash
# Backup
docker save -o /backups/my_app_backup.tar my_app:latest

# Restore
docker load -i /backups/my_app_backup.tar

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x