Difference Between Docker Save and Export – Docker保存和导出的区别

最后修改: 2022年 5月 2日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

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.

Docker生态系统有许多工具和功能,有时会让人感到困惑。在这篇短文中,我们将看看Dockersaveexport命令之间的区别。

2. Docker Images vs. Container

2.Docker图像与容器

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

要理解这两个命令的区别,我们必须首先理解Docker图像和容器之间的区别。

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.

Docker镜像是一个文件,它包含运行一个应用程序所需的所有文件。这包括所有的操作系统文件,以及应用程序代码和任何所需的支持库。

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容器是一个已经启动的Docker镜像。容器本质上是一个正在运行的应用程序。容器像普通进程一样消耗内存和CPU资源,还可以访问文件系统,并通过网络协议与其他容器通信。

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.

Docker容器和映像类似于Java类和对象。一个Java类是如何创建一个对象的蓝图,就像Docker镜像是创建一个容器的蓝图一样。就像一个类可以被实例化为多个对象一样,一个Docker镜像可以用来启动多个容器。

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

考虑到这一点,我们可以仔细看看Dockersaveexport命令之间的区别。

3. docker save

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.

Docker save命令用于将Docker镜像保存为一个tar文件。该命令有助于将Docker镜像从一个注册表转移到另一个注册表,或者只是使用Linux的tar命令检查镜像的内容

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

默认情况下,该命令将tar文件的内容打印到STDOUT,所以典型的用法是。

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:

在这两种情况下,IMAGE参数可以是两个值之一。

  • 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

4、docker导出

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.

Docker export命令用于将Docker容器保存为一个tar文件。这既包括镜像文件,也包括容器运行时的任何变化。

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:

其语法与save命令完全相同。就像saveexport命令将输出发送到STDOUT,所以我们必须将其重定向到一个文件。

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:

在这两种情况下,CONTAINER参数可以是以下值之一。

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

5. Differences

5.差异

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.

虽然这些命令在本质上是相似的,但也有一些需要注意的区别。两个命令都会产生tar文件,但所包含的信息是不同的。

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.

save命令保留了镜像层信息,包括所有历史和元数据。这使得我们可以将tar文件完全导入任何Docker注册中心,并使用它来启动新的容器。

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.

相反,export命令并不保留这些信息。它包含与启动容器的镜像相同的文件,但没有历史和元数据。

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.

此外,导出命令包括容器运行时的变化,如新的或修改的文件。这意味着来自同一镜像的不同容器在导出时可能产生不同的 tar 文件

6. Conclusion

6.结语

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.

在本教程中,我们已经看到了Dockersaveexport命令之间的区别。虽然它们都有类似的语法,并创建了tar文件,但它们有两个不同的目的save命令用于镜像,而export命令则用于容器。