Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: October 21, 2024
While working on Docker, we can save Docker images as a single tar file that can be easily shared, moved, or stored. This tar file contains the entire image which makes it portable to easily move the image to another system, share it with others, or store it for future use. Later, we can load these images from the tar file back into Docker to ensure smooth portability across various environments.
In this tutorial, we’ll learn how to save Docker images into tar files and load them back in.
First, we can display the available Docker images on the system using the docker images command:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 447a8665cc1d 7 weeks ago 273MB
ubuntu latest 8a37d68f4f73 2 months ago 117MB
redis latest 878983f8f504 2 months ago 174MB
mysql latest d8df06984890 2 months ago 802MB
hello-world latest 53cc4d415d83 17 months ago 24.4kB
Let’s now use the docker save command to save the desired Docker image into a tarball called my-nginx.tar:
$ docker save nginx -o my-nginx.tar
Alternatively, we can use file redirection to achieve the same:
$ docker save nginx > my-nginx.tar
For verification, let’s list the current directory’s content to check the my-nginx.tar file:
$ ls C:\Users\thinkpad
Directory: C:\Users\thinkpad
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 9/22/2024 6:51 PM .docker
d-r--- 9/21/2024 8:54 PM Desktop
-a---- 9/2/2024 12:35 AM 71040000 my-nginx.tar
Hence, we successfully created the my-nginx.tar file. We can now transfer it to another system or save it locally for future use and load it when needed.
Having exported the image to our filesystem, let’s now remove the nginx image from Docker:
$ docker rmi nginx
Untagged: nginx:latest
Deleted: sha256:447a8665cc1dab95b1ca778e162215839ccbb9189104c79d7ec3a81e14577add
If we list the docker images now, we’ll see that the nginx image has been removed:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 8a37d68f4f73 2 months ago 117MB
redis latest 878983f8f504 2 months ago 174MB
mysql latest d8df06984890 2 months ago 802MB
hello-world latest 53cc4d415d83 17 months ago 24.4kB
To load our Docker image tarball back into Docker, we use docker load command along with the -i option to specify the path to the file:
$ docker load -i my-nginx.tar
Loaded image: nginx:latest
Alternatively, we can also use file redirection to achieve the same:
$ docker load < my-nginx.tar
Loaded image: nginx:latest
At this point, we’ve imported the Docker image stored in the my-nginx.tar file back into the local Docker environment.
Once we load the image, we can verify it by listing the Docker images:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 447a8665cc1d 7 weeks ago 273MB
ubuntu latest 8a37d68f4f73 2 months ago 117MB
redis latest 878983f8f504 2 months ago 174MB
mysql latest d8df06984890 2 months ago 802MB
hello-world latest 53cc4d415d83 17 months ago 24.4kB
We can see from the output that the nginx image is again available. It’s perhaps of interest that all the properties remain the same, including the created time. This is because Docker preserved and now restored all the image’s metadata, including the image creation date, image size, and so on.
Now that we have the image loaded, it’s safe to delete my-nginx.tar from our filesystem.
In this article, we explained the method to load a Docker image from a tar file. We first saved an image into a tar file using the docker save command. Then, we used the docker load command to load the Docker image from the tar file.
Ultimately, we can use this method to save any Docker image into a tar file for offline distribution, backups, or secure transfers between environments, and load it from the tar file when needed.