Course – LS – All

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

The Docker ecosystem has many tools and features that can sometimes be confusing. In this short article, we will look at the difference between the Docker save and export commands.

2. Docker Images vs. Container

To understand the difference between these two commands, we must first understand the difference between Docker images and containers.

A Docker image is a file that contains all the files necessary to run an application. This includes all of the operating system files, as well as application code and any required supporting libraries.

A Docker container is a Docker image that has been started. A container is essentially a running application. Containers consume memory and CPU resources like a normal process and can also access file systems and communicate with other containers via network protocols.

Docker containers and images are analogous to Java classes and objects. A Java class is a blueprint for how to create an object, just like a Docker image is a blueprint for creating a container. And just like one class can be instantiated into multiple objects, a Docker image can be used to start multiple containers.

With this in mind, we can take a closer look at the difference between the Docker save and export commands.

3. docker save

The Docker save command is used to save a Docker image to a tar file. This command is helpful for moving a Docker image from one registry to another or simply examining the contents of the image using the Linux tar command.

By default, the command prints the tar file contents to STDOUT, so a typical usage is:

docker save IMAGE > /path/to/file.tar

Note that we can also specify a file to print the contents to so that redirecting isn't necessary:

docker save -o /path/to/file.tar IMAGE 

The IMAGE parameter, in either case, can be one of two values:

  • The fully qualified image name, for example, “ghcr.io/baeldung/my-application:1.2.3”
  • The image hash generated by Docker, for example, “c85146bafb83”

4. docker export

The Docker export command is used to save a Docker container to a tar file. This includes both the image files as well as any changes made while the container was running.

The syntax is exactly the same as the save command. Just like save, the export command sends output to STDOUT, so we have to redirect it to a file:

docker export CONTAINER > /path/to/file.tar

Or we can specify the output file name:

docker export -o /path/to/file.tar CONTAINER

In both cases, the CONTAINER parameter can be one of the following values:

  • The container name, either auto-generated or specified when the container started
  • The unique container hash assigned by the Docker engine

5. Differences

While the commands are similar in nature, there are some differences to be aware of. Both commands produce tar files, but the information included is different.

The save command preserves the image layer information, including all history and metadata. This allows us to fully import the tar file into any Docker registry and use it to start new containers.

Conversely, the export command does not preserve this information. It contains the same files as the image that started the container but without history and metadata.

Additionally, the export command includes changes made while the container was running, such as a new or modified file. This means different containers from the same image may produce different tar files when exporting them.

6. Conclusion

In this tutorial, we've seen the differences between the Docker save and export commands. While they both have similar syntax and create tar files, they serve two distinct purposes. The save command is used for images, while the export command is used for containers.

Course – LS – All

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!