Updated April 12, 2023
Introduction to Docker Save
The ‘docker save’ is used to save one or more than one image to a tar archive. It includes all parent layers, and all tags or versions. It is by default streamed to STDOUT, however, we can write to a file, instead of STDOUT by specifying a flag. This command is very useful when we have to use Docker images on different machines without using a registry or repository. We can also compress or save the image to a tar.gz file using gzip.
Syntax:
$docker save [OPTIONS] IMAGE [IMAGE…]
Options:
–output, -o: It is used to redirect the output to a file
–help, -h: It is used to get help about the command.
How save function works in Docker?
As we know, it is used to save Docker images to an archive. When we run this command from the command line, the Docker daemon saves the mentioned Docker image as an archive. We can then share that archive with different teams or to different computers and so on. Now, we need to load that archive file, in order to use that image. When we load that archive file again, it creates a Docker image.
Examples
Let’s understand the whole process with an example.
Scenario: We are going to save a Docker image and then will load it after deleting the existing image.
Step 1. Let’s check available Docker images locally using the below command:
docker image ls
Explanation: – In the above snapshot, we can see that there are two images available, let’s take the ‘ubuntu’ Docker image with the ‘latest’ tag. If the image is not available locally, pull it from the public registry that is hub.docker.com using the below command:
docker pull ubuntu
Note: We can use any Docker images; it should be available locally.
Step 2. Now, we have a Docker image available locally let’s save it to an archive file as shown below:
docker save ubuntu -o my-ubuntu.tar
ls my-ubuntu2.tar
Explanation: In the above snapshot, we can see that the ‘-o’ option has been used to save it to an archive and the name of the archive is ‘my-ubuntu.tar’. We can also use redirection instead of the ‘-o’ option to redirect the output to an archive file as shown below:
docker save ubuntu > my-ubuntu2.tar
ls my-ubuntu2.tar
Step 3. Let’s verify by deleting exiting image and loading it from archive using below command:
docker image rm ubuntu
docker image ls
docker load –i my-ubuntu.tar
docker image ls
Explanation: In the above snapshot, we have deleted the ‘ubuntu’ image that we have verified using the second command and then we have loaded the image using the archive file in which we have saved the image earlier. We have to use the ‘-i’ option to instruct the Docker daemon to load the image from an archive file instead of STDOUT. Finally, we have checked the locally available images to verify and we can see that the image is available again.
Scenario: Save multiple images at once.
Step 1. Let’s save two Docker images Ubuntu and nginx to an archive file as shown below:
docker save -o my-images.tar ubuntu nginx:alpine
Step 2. Let’s verify it as well using the same way we did earlier:
docker image rm ubuntu nginx:alpine
docker image ls
docker load -o my-images.tar
docker image ls
Explanation: As per the above example, we can understand that we can save multiple images in a single archive, so we load it again, it will load all the images that were saved using the ‘docker save’ command.
Scenario: Save Docker images to a tar.gz file.
Solution: We can do it by using the gzip command. It compresses the tar and makes it smaller in size. We can run the below command to save the images to a tar.gz file:
docker save IMAGE [IMAGE] | gzip > <archive_name.gz>
docker save ubuntu nginx:alpine | gzip > my-images2.tar.gz
ll my-images.tar my-images2.tar.gz
Explanation: In the above snapshot, we can see that the ‘my-images2.tar.gz’ file is much smaller than the ‘my-images.tar’ file.
Advantages
- The main advantage is we can share Docker images with our teams or to test on different computers without using the Docker registry.
- We can archive the Docker images if not in use, for example, old builds are not required after a new build is released, however, we have to keep it for some time before removing it so better we can archive those images.
- We can save a lot of disk space if we also compressing the archive using gzip.
Conclusion
The ‘docker save’ is a handy command to save the images only, it does not save changes made to the image by any running container using that image. We have one more command to save the images and that is ‘docker image save’. This command works similarly to the ‘docker save’ command.
Recommended Articles
This is a guide to Docker Save. Here we also discuss the introduction and How save function work in docker? along with different examples and its code implementation. You may also have a look at the following articles to learn more –