Home>Article>Operation and Maintenance> What does docker image import and export mean?

What does docker image import and export mean?

WBOY
WBOY Original
2022-03-08 10:47:55 6187browse

In docker, image import uses the "load [options]" command to copy the current image into a new image; image export uses the "save [options] images" command to save the history including the mirror all the information.

What does docker image import and export mean?

The operating environment of this tutorial: linux7.3 system, docker-1.13.1 version, Dell G3 computer.

What does docker image import and export mean

With the development of container technology, many application systems now choose to use docker containers for deployment, but sometimes docker containers are used We will encounter problems when deploying. For example, our application needs to rely on other third-party images. If the server cannot connect to the external network from the internal network at this time, it will not be deployed. Based on this situation, Docker officially supports the import and export of Docker images and containers. We can compile the image on a machine with Internet access, then export the image or container, and finally upload the exported image or container to the intranet server, and then Import the image or container, and that's it.

The import and export operations of images and containers mainly involve the following commands: save, load, export, and import.

During the demonstration, we generated the image or container locally, then exported the image or container, and finally uploaded it to the Alibaba Cloud server to demonstrate the import function.

We use VS 2019 to create an ASP.NET Core MVC project, add the Dockerfile file:

# 使用运行时镜像 FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim # 设置工作目录 WORKDIR /app # 把目录下的内容都复制到当前目录下 COPY . . # 暴露80端口 EXPOSE 80 # 运行镜像入口命令和可执行文件名称 ENTRYPOINT ["dotnet", "DockerDemo.dll"]

and then publish the project. We look at the existing docker images

and we can see that there are only two .net core images. We generate the image:

docker build -t dockerdemo .

As shown below:

View the generated image

Then we run the container based on the generated image. First check the existing containers:

You can see that there are no containers at this time. We run the container:

#You can see that the container has run successfully.

2. Import and export of docker image

1. Export of docker image

Involved commands:

docker save [options] images [images...]

We use the above image to Export of the demo image:

docker save -o dockerdemo.tar dockerdemo

As shown in the figure below:

The path to the exported file is specified when exporting. If the path is not specified, the default is the current folder.

Or you can use the following command to export:

docker save > dockerdemo.tar dockerdemo

where -o and > indicate output to a file, dockerdemo.tar is the exported target file, and dockerdemo is the source image name.

We check whether there is an exported file locally:

You can see that there is already the file just exported under the directory.

2. Import of docker image

We first use XFtp to upload the image file exported above to the Alibaba Cloud server

and then enter The directory where the file is located

Let’s check what images are on the Alibaba Cloud server:

As can be seen from the picture above : There are currently no images on the Alibaba Cloud server.

Involved import command load

docker load [options]

Next we import the image just uploaded.

docker load -i dockerdemo.tar

As shown below:

#Or you can also use the following command

docker load < dockerdemo.tar

where -i (i means import) and < means input from file. The above two commands will successfully import the image and related metadata, including tag information.

View the image after importing:

You can see that we have the image we just imported. After importing the image, you can run the container based on the image and finally run the application.

3. Import and export of docker containers

Next we demonstrate the import and export of containers.

1. Export of docker container

Involves the command export.

docker export [options] container

We export the container generated above:

docker export -o D:\containers\dockerdemocontainer.tar dockerdemo

As shown below:

其中,-o表示输出的文件,这里指定了输出的路径,如果没有指定路径,则默认生成到当前文件夹。dockerdemocontainer.tar为目标文件,dockerdemo为源容器名。

我们查看目录下面是否生成了导出的容器:

2、docker容器的导入

我们首先把导出的容器使用XFTP上传到阿里云服务器。

涉及到的导入命令import。

docker import [options] file|URL|- [REPOSITORY[:TAG]]

如下图所示

我们导入刚才上传的容器

docker import dockerdemocontainer.tar dockerdemo:imp

dockerdemocontainer.tar表示要导入的容器,dockerdemo:imp表示导入后的镜像名称,imp表示给导入的镜像打tag。

如下图所示

然后我们查看镜像:

可以看到这时有我们刚才导入的镜像了,导入的镜像tag为imp。

四、总结

下面我们来总结一下镜像和容器导入导出的区别:

  1. 镜像导入是一个复制的过程,容器导入是将当前容器变成一个新的镜像。
  2. docker save命令保存的是镜像(image),docker export命令保存的是容器(container)。
  3. export命令导出的tar文件略小于save命令导出的。
  4. 因为export导出的是容器,export导出的文件在import导入时,无法保留镜像所有的历史(即每一层layer信息),不能进行回滚操作。而save是根据镜像来的,所以导入时可以完整保留下每一层layer信息。如下图所示:dockerdemo:latest是save导出load导入的,dockerdemo:imp是export导出import导入的。
  5. docker load不能对导入的镜像重命名,而docker import导入可以为镜像指定新名称。例如,上面导入的时候指定dockerdeom:imp。

对于是使用镜像导入导出还是使用容器导入导出该如何选择呢?有下面两点建议:

  1. 若是只想备份image,使用save和load。
  2. 若是在启动容器后,容器内容有变化,需要备份,则使用export和import。

推荐学习:《docker视频教程

The above is the detailed content of What does docker image import and export mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn