How to Backup and Restore All Docker Images with Gzip Compression
TL;DR:
To back up all your Docker images safely, use docker save to export them and gzip to compress them.
Then, when you need to restore, use docker load to re-import everything.
Below you’ll find production-ready Bash scripts for automated backup and restore — complete with compression and error handling.
📦 Why You Need This
Whether you’re upgrading your system, cleaning your Docker environment, or migrating to another host, exporting your local images is crucial. Docker’s built-in commands make this possible, but using them manually for dozens of images can be tedious and space-inefficient.
This article provides automated scripts that will:
- Backup every Docker image individually,
- Compress each file with
gzipfor storage efficiency, - Restore all images automatically with a single command.
🧱 Backup Script (backup_docker_images.sh)
The script below exports all Docker images, one by one, into compressed .tar.gz files.
Each image gets its own archive, named after its repository and tag.
#!/bin/bash
# --------------------------------------------
# Backup all Docker images into compressed .tar.gz files
# --------------------------------------------
BACKUP_DIR=~/docker-backup
mkdir -p "$BACKUP_DIR"
cd "$BACKUP_DIR" || exit 1
echo "📦 Starting Docker image backup..."
echo "Backup directory: $BACKUP_DIR"
echo
for image in $(docker image ls --format "{{.Repository}}:{{.Tag}}"); do
# sanitize file name
safe_name=$(echo "$image" | tr '/:' '__')
outfile="${safe_name}.tar"
gzfile="${outfile}.gz"
echo "🟢 Saving $image → $gzfile"
# Save and compress directly (no uncompressed tar left behind)
docker save "$image" | gzip -c > "$gzfile"
if [ $? -eq 0 ]; then
echo "✅ Successfully saved $image"
else
echo "❌ Error saving $image"
fi
echo
done
echo "🎉 Backup complete!"
ls -lh "$BACKUP_DIR"/*.gz
💡 What This Script Does
- Creates a
~/docker-backupdirectory automatically. - Iterates over every local Docker image.
- Uses
docker savepiped togzipfor direct compression. - Prints friendly success and error messages.
Result: You’ll get a set of compressed files like:
jonathan-tomcat__latest.tar.gz
jonathan-mysql__latest.tar.gz
jonathan-grafana__latest.tar.gz
...
🔁 Restore Script (restore_docker_images.sh)
This companion script automatically restores every compressed image. It detects both .tar.gz and .tar files in the backup directory, decompresses them, and loads them back into Docker.
#!/bin/bash
# --------------------------------------------
# Restore all Docker images from .tar.gz or .tar files
# --------------------------------------------
BACKUP_DIR=~/docker-backup
cd "$BACKUP_DIR" || { echo "❌ Backup directory not found: $BACKUP_DIR"; exit 1; }
echo "🚀 Starting Docker image restore from $BACKUP_DIR"
echo
for file in *.tar.gz *.tar; do
[ -e "$file" ] || { echo "No backup files found."; exit 0; }
echo "🟡 Loading $file..."
if [[ "$file" == *.gz ]]; then
gunzip -c "$file" | docker load
else
docker load -i "$file"
fi
if [ $? -eq 0 ]; then
echo "✅ Successfully loaded $file"
else
echo "❌ Error loading $file"
fi
echo
done
echo "🎉 Restore complete!"
docker image ls
💡 How It Works
- Automatically detects
.tar.gzor.tarbackups. - Decompresses each one and loads it into Docker.
- Prints progress updates as it restores each image.
After running it, your local Docker environment will look exactly like before — same repositories, tags, and image IDs.
⚙️ How to Use
1️⃣ Backup All Docker Images
chmod +x backup_docker_images.sh
./backup_docker_images.sh
You’ll see a live summary of each image as it’s saved and compressed.
2️⃣ Restore Later (After a Prune or Reinstall)
chmod +x restore_docker_images.sh
./restore_docker_images.sh
Docker will reload each image automatically, maintaining all original metadata.
💾 Bonus: Cleaning and Rebuilding Your Docker Environment
If you want to clear all Docker data before restoring your images, run:
docker system prune -a --volumes
⚠️ Warning: This deletes all containers, images, networks, and volumes.
Afterward, simply run the restore script to bring your images back.
🧠 Why Use Gzip?
Docker image archives are often large — several gigabytes each. Compressing them with gzip:
- Saves 30–70% of disk space,
- Speeds up transfers (especially over SSH),
- Keeps the backups cleaner and easier to manage.
You can still restore them directly with gunzip -c file.tar.gz | docker load — no decompression step required.
✅ Summary Table
| Task | Command | Description |
|---|---|---|
| Backup all images (compressed) | ./backup_docker_images.sh |
Creates one .tar.gz per image |
| Restore all images | ./restore_docker_images.sh |
Loads back each saved archive |
| Prune all Docker data | docker system prune -a --volumes |
Clears everything before restore |
🚀 Conclusion
Backing up your Docker images is a crucial part of any development or CI/CD workflow. With these two scripts, you can protect your local Docker environment from accidental loss, disk cleanup, or reinstallation.
By combining docker save and gzip, you ensure both efficiency and recoverability — making your Docker workstation fully portable and disaster-proof.
Keep calm and backup your containers 🐳💾