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...]
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 anddocker 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
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_20250207.tar
). - Compress large images with
gzip
orxz
to reduce file size. - Verify the loaded image with
docker images
to 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 save
and is not corrupted. - “Image not found after loading”
→ Check the output ofdocker load
to confirm the image and tag. - “Permission denied”
→ Run the command withsudo
or 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
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND