How to use docker image load command to load images

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker load command to load Docker images from archives. This is a crucial skill for transferring images between machines without relying on a Docker registry. You will begin by preparing a Docker image archive using docker save.

Following the preparation, you will explore different methods of loading the image. This includes loading an image from standard input (STDIN), loading from a file using the --input flag, and specifically loading a platform variant using the --platform flag. By completing these steps, you will gain practical experience in managing Docker images offline.


Skills Graph

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

Prepare a Docker image archive

In this step, we will learn how to prepare a Docker image archive. This is useful for transferring Docker images between different machines without relying on a Docker registry. We will use the docker save command to create an archive of an existing Docker image.

First, let's pull a simple Docker image that we can use for this demonstration. We will use the hello-world image.

docker pull hello-world

You should see output indicating that the image is being pulled and that it is a very small image.

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, we can save it to a tar archive file. We will save it to a file named hello-world.tar in the ~/project directory.

docker save -o ~/project/hello-world.tar hello-world

The -o flag specifies the output file path. The command will save the hello-world image to the specified file. You should not see any output if the command is successful.

To verify that the archive file was created, you can list the files in the ~/project directory.

ls ~/project

You should see hello-world.tar listed among the files.

hello-world.tar

This hello-world.tar file now contains the Docker image data for the hello-world image. You can transfer this file to another machine and load the image from it using the docker load command, which we will cover in the next steps.

Load a Docker image from STDIN

In this step, we will learn how to load a Docker image from standard input (STDIN). This is another way to load an image archive, particularly useful when piping the output of docker save directly to docker load.

First, let's remove the hello-world image that we pulled and saved in the previous step. This will ensure that we are loading the image from the archive and not using the existing one.

docker rmi hello-world

You should see output indicating that the image has been deleted.

Untagged: hello-world:latest
Deleted: sha256:...

Now, we will use the cat command to output the content of the hello-world.tar file to standard output, and then pipe that output to the docker load command.

cat ~/project/hello-world.tar | docker load

The docker load command, when used without the --input flag, reads the image archive from STDIN. You should see output indicating that the image layers are being loaded.

...
Loaded image: hello-world:latest

To verify that the image has been loaded successfully, you can list the available Docker images.

docker images

You should see the hello-world image listed in the output.

REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    ...            ...           ...

This demonstrates how to load a Docker image from an archive file piped to STDIN.

Load a Docker image from a file using --input

In this step, we will learn how to load a Docker image from a file using the --input flag with the docker load command. This is the most common way to load an image archive from a file.

First, let's again remove the hello-world image to ensure we are loading it from the archive file.

docker rmi hello-world

You should see output confirming the image deletion.

Untagged: hello-world:latest
Deleted: sha256:...

Now, we will use the docker load command with the --input flag to specify the path to the hello-world.tar file we created in the first step.

docker load --input ~/project/hello-world.tar

The --input flag tells docker load to read the image archive from the specified file instead of STDIN. You should see output similar to when loading from STDIN, indicating the layers are being loaded.

...
Loaded image: hello-world:latest

To confirm that the image has been loaded, list the Docker images again.

docker images

The hello-world image should be present in the list.

REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    ...            ...           ...

This method is generally preferred when you have the image archive saved as a file, as it is more explicit than piping the file content to STDIN.

Load a specific platform variant using --platform

In this step, we will explore how to load a specific platform variant of a Docker image from an archive using the --platform flag. This is particularly useful when an image archive contains variants for multiple architectures (e.g., linux/amd64, linux/arm64).

First, let's remove the hello-world image again to start fresh.

docker rmi hello-world

You should see output confirming the image deletion.

Untagged: hello-world:latest
Deleted: sha256:...

Now, we will use the docker load command with the --input flag to specify the archive file and the --platform flag to specify the desired platform. For this example, we will specify linux/amd64, which is the architecture of the LabEx VM.

docker load --input ~/project/hello-world.tar --platform linux/amd64

The --platform flag ensures that Docker only loads the image variant that matches the specified architecture and operating system. Even though the hello-world image is very simple and likely doesn't have significant platform variations in its archive, this demonstrates the usage of the flag. You should see output indicating the layers are being loaded.

...
Loaded image: hello-world:latest

To confirm that the image has been loaded, list the Docker images.

docker images

The hello-world image should be present in the list.

REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    ...            ...           ...

Using the --platform flag is important when working with multi-architecture image archives to ensure you load the correct variant for your environment.

Summary

In this lab, we learned how to prepare a Docker image archive using the docker save command, which is essential for transferring images without a registry. We successfully saved the hello-world image to a tar file.

We then explored different methods for loading Docker images from archives using the docker load command. We learned how to load an image from standard input (STDIN), which is useful for piping operations, and how to load an image directly from a file using the --input flag. Finally, we discovered how to load a specific platform variant of an image from an archive using the --platform flag, demonstrating the flexibility of the docker load command for managing multi-platform images.