Docker save allows users to export a Docker image as a tarball (compressed archive file). This is particularly useful for storing, sharing, and transferring images across different environments without requiring an internet connection or access to a remote Docker registry.
What is docker save
?
The docker save
command is used to create a compressed archive of one or more Docker images. This allows users to back up images, transfer them between machines, or store them for future use without relying on an online repository like Docker Hub.
It's one of the most important Docker commands you can learn, whether you're taking a Docker course or self-teaching.
General Syntax of docker save
docker save -o output.tar image_name:tag
Or, if saving multiple images:
docker save -o output.tar image1:tag1 image2:tag2
To send the output to stdin, use:
docker save image_name | gzip > output.tar.gz
This allows compression while saving and ensures the archive is minimized in size.
Saving a Docker Image
To save a Docker image called my-app
with the tag latest
as a tar file:
docker save -o my-app.tar my-app:latest
This will create a file named my-app.tar
in the current directory, containing the Docker image.
Loading a Saved Image
A saved image can be loaded onto another system using the docker load
command:
docker load -i my-app.tar
This command restores the Docker image from the tar archive and makes it available for use.
Why Use docker save
?
- Offline Sharing: Transfer images to air-gapped systems or devices without internet access.
- Backup and Restore: Store important images safely in an offline backup.
- Faster Deployment: Distribute images without pulling from a Docker registry.
- Version Control: Save and track different image versions for development or rollback purposes.
- Snapshot of a Container: By saving an image before modifications, you can maintain snapshots for future reference.
Differences Between docker save
and docker export
While docker save
exports an entire Docker image, docker export
saves the filesystem of a running container. Key differences:
Feature | docker save |
docker export |
---|---|---|
Saves an image | Yes | No |
Includes layers and history | Yes | No |
Includes container state | No | Yes |
Can be loaded back as an image | Yes | No |
Common Questions About Docker Save
What is docker save
command?
The docker save
command is used to export one or more Docker images as a tarball. This allows for easy storage and transfer of images between systems without requiring a Docker registry.
Where is docker save
?
The docker save
command is part of the Docker CLI (bin/docker
) and is available in any Docker installation. You can check its availability by running:
docker save --help
It is executed from the terminal session and works in the working directory where you want to save the image archive.
What is the difference between docker export
and docker save
?
docker save
exports an entire Docker image, including all layers and metadata, whereas docker export
extracts only the filesystem of a running container, omitting image history and layers. If you need to restore the image with all layers and history, docker save
is the preferred approach.
What is the difference between docker commit
and docker save
?
docker commit
creates a new Docker image from a modified container but does not save it as a tar archive.docker save
exports an entire image, preserving its layers and metadata, allowing for future use withdocker load
.
Best Practices When Using docker save
- Use meaningful filenames (e.g.,
my-app-v1.tar
) to track versions. - Compress large images to reduce file size (
gzip my-app.tar
using the gzip command). - Store backups in a secure location.
- Verify image integrity after loading using
docker images
. - Ensure that the correct repository tag image ID is used when saving images.
- Check for available images using:
docker ps -a
- Use entrypoint scripts to modify saved images efficiently.
Using docker save
with Flags
The docker save
command supports multiple flags for better control:
-o
: Specifies the output file for the tar archive.--output
: Alternative to-o
to define the tar file destination.--help
: Displays available flags and options.
Working with docker save
on Different Hosts
When transferring a saved image between Docker hosts, you can use scp to copy the tarball:
scp my-app.tar user@remote_host:/usr/local/images/
Then, on the remote host, load the image:
docker load -i /usr/local/images/my-app.tar
This method is useful when migrating images between Ubuntu, CentOS, or other Linux-based environments.
Using docker save
with a Specific Image ID
To save an image using its image ID, first retrieve the ID:
docker images
Then use:
docker save -o my-app.tar IMAGE_ID
Automating docker save
with Scripts
You can automate saving images with a script:
#!/bin/bash
IMAGES=$(docker images --format "{{.Repository}}:{{.Tag}}")
for IMG in $IMAGES; do
FILE_NAME=$(echo $IMG | tr ':' '_').tar
docker save -o $FILE_NAME $IMG
done
This script saves all available images in the Docker repository with a proper naming convention.
Key Takeaways
docker save
creates a tarball of one or more Docker images.- Images saved with
docker save
can be reloaded usingdocker load
. - It is useful for offline image transfers, backups, and faster deployments.
- Unlike
docker export
,docker save
retains all image layers and metadata. - Compression can be applied using the gzip command to reduce the archive size.
- Supports saving by image name, tag, or image ID.
- Helps create a snapshot of a container before modifications.
Wrapping Up
Docker save is a powerful tool for managing Docker images efficiently. Whether you're backing up images, sharing them offline, or maintaining version control, it provides a reliable way to store and transfer container images.
Understanding when and how to use docker save
ensures seamless image management in various Docker environments, especially when working with repository images, entrypoints, and configurations.