How to use docker image save command to export images

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker save command to export Docker images into tar archive files. This is a crucial skill for backing up images, transferring them between systems without a registry, or bundling multiple images for distribution.

You will explore saving a single image, saving multiple images into one archive, saving a specific platform variant of an image, and compressing the saved image archive using gzip. By the end of this lab, you will be proficient in using docker save for various image management tasks.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") docker/ImageOperationsGroup -.-> docker/images("List Images") docker/ImageOperationsGroup -.-> docker/save("Save Image") subgraph Lab Skills docker/pull -.-> lab-555162{{"How to use docker image save command to export images"}} docker/images -.-> lab-555162{{"How to use docker image save command to export images"}} docker/save -.-> lab-555162{{"How to use docker image save command to export images"}} end

Save a single image to a tar archive

In this step, you will learn how to save a single Docker image to a tar archive file. This is useful for backing up images or transferring them to another system without a direct Docker registry connection.

First, let's pull a simple Docker image that we will use for this example. We will use the hello-world image, which is small and suitable for this demonstration.

docker pull hello-world

You should see output indicating that the image is being pulled and downloaded.

Using default tag: latest
latest: Pulling from library/hello-world
...
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest

Now that we have the hello-world image locally, we can save it to a tar archive using the docker save command. The basic syntax is docker save -o <output_file.tar> <image_name>.

We will save the hello-world image to a file named hello-world.tar in your current directory (~/project).

docker save -o hello-world.tar hello-world

After running this command, a file named hello-world.tar will be created in your ~/project directory. This file contains the hello-world Docker image.

You can verify that the file was created and check its size using the ls -lh command.

ls -lh hello-world.tar

You should see output similar to this, showing the file size:

-rw-r--r-- 1 labex labex ... hello-world.tar

This tar file can now be transferred to another machine and loaded using the docker load command.

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.

Save a specific platform variant of an image

In this step, you will learn how to save a specific platform variant of a Docker image. Docker images can be built for different architectures (like amd64, arm64, etc.). Sometimes, you might need to save an image specifically built for a particular platform.

By default, docker pull and docker save operate on the image variant that matches the architecture of your current system. However, you can specify a different platform using the --platform flag.

Let's pull an image that has variants for different platforms. The alpine image is a good example. We will explicitly pull the arm64 variant of the alpine image, even though your LabEx VM is likely amd64.

docker pull --platform arm64 alpine

You should see output indicating that the arm64 variant of the alpine image is being pulled.

arm64: Pulling from library/alpine
...
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest

Now, let's save this specific arm64 variant of the alpine image to a tar archive. We will use the --platform flag again with the docker save command.

We will save the arm64 variant of alpine to a file named alpine_arm64.tar in your ~/project directory.

docker save --platform arm64 -o alpine_arm64.tar alpine

After running this command, a file named alpine_arm64.tar will be created in your ~/project directory. This file contains the arm64 variant of the alpine Docker image.

You can verify that the file was created using the ls -lh command.

ls -lh alpine_arm64.tar

You should see output similar to this:

-rw-r--r-- 1 labex labex ... alpine_arm64.tar

To confirm that the saved image is indeed the arm64 variant, you can inspect the contents of the tar file. While directly verifying the architecture from the tar contents is complex, the fact that you used --platform arm64 during the save operation ensures that the saved image is for that platform.

This demonstrates how to explicitly save an image for a platform different from your host's architecture.

Save an image and compress it using gzip

In this final step, you will learn how to save a Docker image to a tar archive and compress it using gzip simultaneously. This is a common practice to reduce the file size of the saved image, making it easier to store and transfer.

The docker save command itself does not have a built-in compression flag. However, you can achieve compression by piping the output of docker save to a compression utility like gzip.

We will save the alpine image (which you should have pulled in a previous step) and compress it using gzip. The output will be redirected to a file with a .tar.gz extension.

The syntax involves piping the output: docker save <image_name> | gzip > <output_file.tar.gz>.

Let's save the alpine image to a gzipped tar file named alpine.tar.gz in your ~/project directory.

docker save alpine | gzip > alpine.tar.gz

This command saves the alpine image to standard output, pipes that output to the gzip command for compression, and then redirects the compressed output to the file alpine.tar.gz.

After running this command, a file named alpine.tar.gz will be created in your ~/project directory.

You can verify that the file was created and check its size using the ls -lh command. Compare its size to the uncompressed alpine image size (which is part of images.tar or would be if you saved it individually) to see the effect of compression.

ls -lh alpine.tar.gz

You should see output similar to this:

-rw-r--r-- 1 labex labex ... alpine.tar.gz

To confirm that the file is a gzipped tar archive, you can use the file command.

file alpine.tar.gz

The output should indicate that it is a gzip compressed data file.

alpine.tar.gz: gzip compressed data, ...

You can also list the contents of the compressed archive using tar -tfz. The z flag tells tar to decompress the file using gzip before listing the contents.

tar -tfz alpine.tar.gz

You should see the contents of the alpine image within the archive, similar to when you listed the contents of images.tar.

...
alpine/
alpine/manifest.json
...

This method is efficient for saving and compressing images in a single command.

Summary

In this lab, you learned how to use the docker save command to export Docker images to tar archives. You successfully saved a single image (hello-world) to a tar file, demonstrating the basic usage of the command.

Furthermore, you explored saving multiple images into a single archive, which is useful for bundling images. You also learned how to save a specific platform variant of an image and how to compress the output tar file using gzip for reduced size. These steps covered the fundamental capabilities of docker save for image backup and transfer.