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.
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 <p>where -i (i means import) and </p><p>View the image after importing: </p><p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/067/6714297cc660492da31f34cedb861b74-11.png?x-oss-process=image/resize,p_40" class="lazy" alt=""></p><p>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. </p><h2 id="Import-and-export-of-docker-containers">3. Import and export of docker containers</h2><p>Next we demonstrate the import and export of containers. </p><h2 id="Export-of-docker-container">1. Export of docker container</h2><p>Involves the command export. </p><pre class="brush:php;toolbar:false">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。
四、总结
下面我们来总结一下镜像和容器导入导出的区别:
- 镜像导入是一个复制的过程,容器导入是将当前容器变成一个新的镜像。
- docker save命令保存的是镜像(image),docker export命令保存的是容器(container)。
- export命令导出的tar文件略小于save命令导出的。
- 因为export导出的是容器,export导出的文件在import导入时,无法保留镜像所有的历史(即每一层layer信息),不能进行回滚操作。而save是根据镜像来的,所以导入时可以完整保留下每一层layer信息。如下图所示:dockerdemo:latest是save导出load导入的,dockerdemo:imp是export导出import导入的。
docker load不能对导入的镜像重命名,而docker import导入可以为镜像指定新名称。例如,上面导入的时候指定dockerdeom:imp。
对于是使用镜像导入导出还是使用容器导入导出该如何选择呢?有下面两点建议:
- 若是只想备份image,使用save和load。
- 若是在启动容器后,容器内容有变化,需要备份,则使用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!

docker中rm和rmi的区别:rm命令用于删除一个或者多个容器,而rmi命令用于删除一个或者多个镜像;rm命令的语法为“docker rm [OPTIONS] CONTAINER [CONTAINER...]”,rmi命令的语法为“docker rmi [OPTIONS] IMAGE [IMAGE...]”。

docker官方镜像有:1、nginx,一个高性能的HTTP和反向代理服务;2、alpine,一个面向安全应用的轻量级Linux发行版;3、busybox,一个集成了三百多个常用Linux命令和工具的软件;4、ubuntu;5、PHP等等。

docker容器重启后数据会丢失的;但是可以利用volume或者“data container”来实现数据持久化,在容器关闭之后可以利用“-v”或者“–volumes-from”重新使用以前的数据,docker也可挂载宿主机磁盘目录,用来永久存储数据。

docker对于小型企业、个人、教育和非商业开源项目来说是免费的;2021年8月31日,docker宣布“Docker Desktop”将转变“Docker Personal”,将只免费提供给小型企业、个人、教育和非商业开源项目使用,对于其他用例则需要付费订阅。

docker能安装oracle。安装方法:1、拉取Oracle官方镜像,可以利用“docker images”查看镜像;2、启动容器后利用“docker exec -it oracle11g bash”进入容器,并且编辑环境变量;3、利用“sqlplus /nolog”进入oracle命令行即可。

解决方法:1、停止docker服务后,利用“rsync -avz /var/lib/docker 大磁盘目录/docker/lib/”将docker迁移到大容量磁盘中;2、编辑“/etc/docker/daemon.json”添加指定参数,将docker的目录迁移绑定;3、重载和重启docker服务即可。

有类似docker的软件:1、Podman,是一个无守护程序的开源Linux原生容器引擎;2、LXD,是一个专为LXC Linux容器设计的开源容器引擎;3、Buildah,是是红帽为容器化系统开发的一个OCI镜像构建工具;4、RunC等。

容器管理ui工具有:1、Portainer,是一个轻量级的基于Web的Docker管理GUI;2、Kitematic,是一个GUI工具,可以更快速、更简单的运行容器;3、LazyDocker,基于终端的一个可视化查询工具;4、DockStation,一款桌面应用程序;5、Docker Desktop,能为Docker设置资源限制,比如内存,CPU,磁盘镜像大小;6、Docui。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
