Save multiple images to a tar archive
In the previous step, you learned how to save a single Docker image. In this step, you will learn how to save multiple Docker images into a single tar archive file. This is convenient when you need to bundle several images together for distribution or backup.
First, let's pull another Docker image to have more than one image to save. We will pull the alpine
image, which is a lightweight Linux distribution.
docker pull alpine
You should see output indicating the alpine
image is being pulled and downloaded.
Using default tag: latest
latest: Pulling from library/alpine
...
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest
Now you have both hello-world
and alpine
images locally. You can verify this by listing your local images:
docker images
The output should show both hello-world
and alpine
in the list.
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest ... ... ...
alpine latest ... ... ...
To save multiple images to a single tar archive, you simply list the image names after the output file name in the docker save
command. The syntax is docker save -o <output_file.tar> <image1_name> <image2_name> ...
.
We will save both hello-world
and alpine
images to a file named images.tar
in your ~/project
directory.
docker save -o images.tar hello-world alpine
After running this command, a file named images.tar
will be created in your ~/project
directory. This file contains both the hello-world
and alpine
Docker images.
You can verify that the file was created and check its size using the ls -lh
command. The size will be larger than the hello-world.tar
file from the previous step because it contains two images.
ls -lh images.tar
You should see output similar to this:
-rw-r--r-- 1 labex labex ... images.tar
To confirm that both images are included in the archive, you can list the contents of the tar file using the tar -tf
command.
tar -tf images.tar
The output should show files related to both images, including their manifests and layer data. You should see entries like hello-world/
and alpine/
.
...
hello-world/
hello-world/manifest.json
...
alpine/
alpine/manifest.json
...
This single tar file can now be used to load both images onto another system using docker load
.