1. Introduction

The Docker ecosystem has many tools and features that can sometimes be confusing. In this short article, we’ll 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 creating 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 look closer at the difference between the Docker save and export commands.

3. docker save

The Docker save command saves 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 saves 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 Between Save and Export

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. Differences Between Import and Load in Docker

As we’ve seen above, the save/export commands allow the creation of shared tar images. In order to exploit those images, Docker offers two options through the import/load commands.

The first option is the import command. It works with an exported container’s file system and imports it as a Docker image. This command can create a new image from either a file or a URL that contains a snapshot of a container’s filesystem.

The second option is the load command. It allows running an image from a tar archive that was exported with the Docker save command. It restores both images and tags.

To resume, the import command is used with tars created with Docker export, it dumps the container to a file and flattens the image by removing all the history of the container. On the other hand, the load command is used with tars created with Docker save. It grabs all of an image or a repository and preserves the history.

7. 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.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.