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.
The operating environment of this tutorial: linux7.3 system, docker-1.13.1 version, Dell G3 computer.
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.
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.
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.
Next we demonstrate the import and export of containers.
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为源容器名。
我们查看目录下面是否生成了导出的容器:
我们首先把导出的容器使用XFTP上传到阿里云服务器。
涉及到的导入命令import。
docker import [options] file|URL|- [REPOSITORY[:TAG]]
如下图所示
我们导入刚才上传的容器
docker import dockerdemocontainer.tar dockerdemo:imp
dockerdemocontainer.tar表示要导入的容器,dockerdemo:imp表示导入后的镜像名称,imp表示给导入的镜像打tag。
如下图所示
然后我们查看镜像:
可以看到这时有我们刚才导入的镜像了,导入的镜像tag为imp。
下面我们来总结一下镜像和容器导入导出的区别:
docker load不能对导入的镜像重命名,而docker import导入可以为镜像指定新名称。例如,上面导入的时候指定dockerdeom:imp。
对于是使用镜像导入导出还是使用容器导入导出该如何选择呢?有下面两点建议:
推荐学习:《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!