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 withdocker 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...]
Code language: CSS (css)
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
Code language: CSS (css)
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
Code language: CSS (css)
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
Code language: CSS (css)
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
Code language: JavaScript (javascript)
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"
Code language: JavaScript (javascript)
This script saves my_app:latest with a timestamped filename.
7. Check the Content of a Saved Tarball
tar -tvf my_image.tar
Code language: CSS (css)
This lists the contents of the tar file, showing layers and metadata.
docker load Command Syntax
docker load [OPTIONS]
Code language: CSS (css)
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
Code language: CSS (css)
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
Code language: CSS (css)
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
Code language: CSS (css)
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
Code language: JavaScript (javascript)
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 saveto cache images anddocker loadto reuse them in CI/CD.
4. Air-Gapped Deployments
- Use
docker saveto 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 saveto migrate images to a new host. - Example: Move legacy Docker images to a new infrastructure.
List of Common docker save and docker load Commands
| Command | Description |
|---|---|
docker save -o my_image.tar my_image:latest | Save an image to a tar file |
| `docker save my_image:latest | gzip > my_image.tar.gz` |
docker save -o images_backup.tar nginx alpine | Save multiple images to a single tarball |
docker load -i my_image.tar | Load an image from a tar file |
| `gunzip < my_image.tar.gz | docker load` |
docker load < my_image.tar | Load an image using standard input |
docker save -o backup.tar my_app:1.0 | Save a specific image tag |
Best Practices for Using docker save and docker load:
- Use meaningful filenames when saving images (e.g.,
my_app_backup_20260207.tar). - Compress large images with
gziporxzto reduce file size. - Verify the loaded image with
docker imagesto ensure it imported successfully. - Automate image backups in scripts for regular snapshots.
- Check tarball integrity before loading (
tar -tvf my_image.tar).
Common Errors and Solutions
- “No such file or directory”
โ Ensure the tar file exists and the path is correct. - “Invalid tar file”
โ Verify the tarball was created withdocker saveand is not corrupted. - “Image not found after loading”
โ Check the output ofdocker loadto confirm the image and tag. - “Permission denied”
โ Run the command withsudoor check your user permissions.
Combining docker save and docker load in Workflows
Backup and Restore Workflow
- Save the image:
docker save -o my_image.tar my_image:latest - 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
Code language: PHP (php)
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals