How to use the command 'docker save' (with examples)
The docker save
command is used to export one or more Docker images to an archive. This command allows you to save the images as a tar file that can be transported and imported into another Docker environment. It is especially useful when you want to share Docker images with others or backup your images on a different machine.
Use case 1: Save an image by redirecting stdout
to a tar archive
Code:
docker save image:tag > path/to/file.tar
Motivation: This use case is helpful when you want to save a specific image and tag combination to a tar archive. By redirecting the output to a file, you can easily name and store the tar file for later use or transfer it to another machine.
Explanation:
docker save
is the command used to save Docker images.image:tag
is the combination of the image name and the desired tag that you want to save.>
is the output redirection symbol that tells the command to save the output to a file instead of displaying it on the console.path/to/file.tar
is the destination path and filename where the tar file will be saved.
Example output:
$ docker save myimage:v1.0 > /home/user/myimage.tar
In this example, the image with the name myimage
and tag v1.0
will be saved to the file /home/user/myimage.tar
.
Use case 2: Save an image to a tar archive
Code:
docker save --output path/to/file.tar image:tag
Motivation: This use case is useful when you want to save a specific image and tag combination to a tar archive. By specifying the output file using the --output
option, you can directly save the image to the desired location without using output redirection.
Explanation:
--output
is an option that allows you to specify the output file where the Docker image will be saved.path/to/file.tar
is the destination path and filename where the tar file will be saved.image:tag
is the name and tag of the image that you want to save.
Example output:
$ docker save --output /home/user/myimage.tar myimage:v1.0
In this example, the image with the name myimage
and tag v1.0
will be saved to the file /home/user/myimage.tar
.
Use case 3: Save all tags of the image
Code:
docker save --output path/to/file.tar image_name
Motivation: This use case is beneficial when you want to save all the tags associated with a specific Docker image. By specifying just the image name without the tag, all the tags associated with that image will be saved to the tar archive.
Explanation:
image_name
is the name of the Docker image that you want to save.path/to/file.tar
is the destination path and filename where the tar file will be saved.
Example output:
$ docker save --output /home/user/myimage.tar myimage
In this example, all the tags associated with the image named myimage
will be saved to the file /home/user/myimage.tar
.
Use case 4: Cherry-pick particular tags of an image to save
Code:
docker save --output path/to/file.tar image_name:tag1 image_name:tag2 ...
Motivation: This use case is useful when you want to save specific tags of a Docker image instead of all the tags. By specifying the image name followed by the desired tags, you can cherry-pick only the tags you require for saving to a tar archive.
Explanation:
image_name:tag1 image_name:tag2 ...
are the combinations of the image name and the tags that you want to save.path/to/file.tar
is the destination path and filename where the tar file will be saved.
Example output:
$ docker save --output /home/user/myimage.tar myimage:v1.0 myimage:v2.0
In this example, only the tags v1.0
and v2.0
associated with the image named myimage
will be saved to the file /home/user/myimage.tar
.
Conclusion:
The docker save
command is a powerful tool for exporting Docker images to an archive for sharing or backup purposes. By using various options and arguments, you can save specific image and tag combinations, all tags of an image, or cherry-pick certain tags to be saved. This command adds flexibility to Docker image management and facilitates image transfer between different environments.