TEL:
Docker 导出和导入容器
| 发布时间: 2018-12-02 15:47:29 | 596 次浏览

使用docker export命令可以将本地容器导出为容器快照文件。
使用docker import命令可以将容器快照文件导入到本地镜像库,也可以通过指定URL或者某个目录来导入。

特别注意:
docker load命令是将镜像存储文件导入到本地镜像库。
镜像存储文件将保存完整记录,体积较大。
而容器快照文件将丢弃所有的历史记录和元数据信息,仅保存容器当时的快照状态。而且导入时可以重新指定标签等元数据信息。

容器导出的示例

[root@CentOS-7 ~]# docker images
 REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/ubuntu        latest              6a2f32de169d        13 days ago         117.2 MB
[root@CentOS-7 ~]# 
[root@CentOS-7 ~]# docker run --name test -itd ubuntu bash
f0e681fa079b084777df27bf1d8f2d7246e8c864e98b22324f789390a143cc7e
[root@CentOS-7 ~]#
[root@CentOS-7 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
f0e681fa079b        ubuntu "bash" 18 seconds ago      Up 16 seconds test 
[root@CentOS-7 ~]#
[root@CentOS-7 ~]# docker attach test
root@f0e681fa079b:/#
root@f0e681fa079b:/# echo "this is a test" > test.log
root@f0e681fa079b:/# 
[root@CentOS-7 ~]#
 [root@CentOS-7 ~]# docker ps
 CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
f0e681fa079b        ubuntu "bash" About a minute ago   Up About a minute test 
[root@CentOS-7 ~]#
[root@CentOS-7 ~]# docker logs test 
root@f0e681fa079b:/# 
root@f0e681fa079b:/# echo "this is a test" > test.log
[root@CentOS-7 ~]# 
[root@CentOS-7 ~]# docker export test > Ubuntu-Test.tar
[root@CentOS-7 ~]# 
[root@CentOS-7 ~]# ls -lh Ubuntu-Test.tar
-rw-r--r-- 1 root root 97M Apr 26 14:05 Ubuntu-Test.tar
[root@CentOS-7 ~]# 

容器导入的示例

[root@CentOS-7 ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/ubuntu        latest              6a2f32de169d        13 days ago         117.2 MB
[root@CentOS-7 ~]#
[root@CentOS-7 ~]# cat Ubuntu-Test.tar | docker import - ubuntu-test:v1
sha256:143fdc90050b4b45312e7c118aab8a9fefc2ebf19b5a5b7f4f9960e13798f0ae
[root@CentOS-7 ~]# 
[root@CentOS-7 ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
ubuntu-test             v1                  143fdc90050b        5 seconds ago       97.33 MB
docker.io/ubuntu        latest              6a2f32de169d        13 days ago         117.2 MB
[root@CentOS-7 ~]#
[root@CentOS-7 ~]# docker history ubuntu-test:v1
IMAGE               CREATED             CREATED BY          SIZE                COMMENT
143fdc90050b        10 seconds ago                          97.33 MB            Imported from -
[root@CentOS-7 ~]# 
[root@CentOS-7 ~]# docker run --name ubuntu-test -idt ubuntu-test:v1 bash 
05aa05f0a501f42e2c92e3801ee24258f43f02ad0b3214423f74171ea2991f45
[root@CentOS-7 ~]#
[root@CentOS-7 ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
05aa05f0a501        ubuntu-test:v1 "bash" 11 seconds ago      Up 9 seconds                            ubuntu-test
[root@CentOS-7 ~]#
[root@CentOS-7 ~]# docker logs ubuntu-test 
[root@CentOS-7 ~]#  [root@CentOS-7 ~]# docker attach ubuntu-test
root@f6e8ad517d3e:/#  
root@f6e8ad517d3e:/# ls -l test.log
-rw-r--r-- 1 root root 15 Apr 26 06:04 test.log
root@f6e8ad517d3e:/# cat test.log 
this is a test 
root@f6e8ad517d3e:/#