Docker images are an important component of Docker. They are the template that contains instructions for creating/building Docker containers. While working on Docker, developers may want to save Docker images as an archive file for different reasons, such as keeping a backup of images, offline development, etc. In this situation, Docker provides a “docker save” command to save one or multiple images to an archive file.
This study will explain how the “save” function works in Docker.
How Does the Save Function or “save” Command Work in Docker?
The “docker save” is a command in Docker utilized to save single or multiple Docker images to an archive file. The archive file can then be used for the backup of Docker images and transfer images to another PC.
Look at the following examples to see how the “docker save” command works.
Example 1: How to Save and Load a Docker Image After Deleting the Existing Image?
In this example, the following operations will be performed:
- List all the Docker images and select the desired image.
- Save the selected image to an archive file using the “docker save <image-name> -o <output-file-name>” or “docker save <image-name> > <output-file-name>” command.
- Delete the existing image from the local repository.
- Load the image from the archive file via the “docker load -i <input-file-name>” command.
- List the Docker images to verify whether the desired image is loaded from the archive file and available again or not.
Step 1: Display Docker Images
First, run the following command to list all the available Docker images:
The below output shows all the Docker images. Now, choose the desired Docker image to save it as a file. For instance, we have selected the “alpine” image:
Step 2: Save Docker Image as a File
Then, save the selected Docker image as a file with the help of provided command:
Here:
- “docker save” is a Docker command used to save the Docker image to the tar archive file.
- “alpine” is the selected Docker image that needs to be saved as a file.
- “-o” option is utilized to specify the output file.
- “my-alpine.tar” is the output file name:
Alternatively, the below-stated command can also be used to save the Docker image as a file:
Here, “my-alpine2.tar” is the output file name:
Step 3: Verify Changes
Use the “ls” command with the output file name to see whether the selected Docker image has been saved as a file or not:
It can be seen that the desired Docker image has been saved as a tar archive file:
Step 4: Delete Existing Docker Image
Next, delete the existing Docker image from the repository:
Here, the “rm” option is used to delete the “alpine” Docker image:
Step 5: Verify Deleted Image
Execute the below-listed command to verify whether the desired image has been deleted or not:
In the below output, the “alpine” Docker image cannot be seen, which means it has been deleted from the repository:
Step 6: Load Docker Image From Archive File
Now, load the desired Docker image from the tar archive file using the provided command:
Here:
- “docker load” command is utilized to load the Docker image from an archive file.
- “-i” option specifies the input file, i.e., “my-alpine.tar”.
This command will load the “alpine” image from the “my-alpine.tar” file:
Step 7: Verification
Lastly, ensure that the desired image has been loaded from the archive file or not:
In the below screenshot, the “alpine” Docker image can be seen, which means it has been loaded successfully from the archive file:
Example 2: How to Save Multiple Docker Images at Once?
In this example, we will select two Docker images and save them to the tar archive file using the “docker save -o <output-file-name> <image1> <image2>” command.
Step 1: Save Multiple Images
To save multiple images at once, utilize the “docker save” command with the “-o” option and specify the output file name and multiple images names:
Here:
- “-o” option is utilized to specify the output file.
- “my-images.tar” is the output file name.
- “alpine” and “nginx” are Docker images:
Step 2: Verification
Run the following command to verify whether multiple Docker images have been saved as a file or not:
The below image indicates that the multiple Docker images have been saved to a tar archive file i.e., “my-images.tar”:
We have efficiently explained how the “save” command works in Docker.
Conclusion
The “docker save” is a Docker command used to save single or multiple Docker images to a tar archive file. To save the single Docker image as a file, the “docker save <image-name> -o <output-file-name>” command is used. In order to save multiple Docker images to the tar archive file, utilize the “docker save -o <output-file-name> <image1> <image2>” command. This study explained how the “save” function works in Docker.