镜像的导出( 导出 )与导入  &  基于容器( 模板 ),创建镜像

镜像的导出( 打包 )与导入

(1)docker save 导出:将下载好的镜像( 本地仓库中 ),进行打包( 导出到一个目录中 ),以备后用

# 作用:将本地的一个或多个镜像,打包保存为本地 tar 文件
# 命令格式:docker save [命令参数][导出镜像名称][本地镜像镜像]
  # 命令参数:-o, --output string 指定写入的文件名和路径

// 代码示例
[root@centos7 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
mysql        latest    5e229524f286   7 days ago   494MB
nginx        latest    0c404972e130   9 days ago   135MB
[root@centos7 ~]# ls
anaconda-ks.cfg  fisco
  // 导出到当前目录 
[root@centos7 ~]# docker save -o nginx.tar nginx
[root@centos7 ~]# ls
anaconda-ks.cfg  fisco  nginx.tar
  // 指定导出路径和导出名称
[root@centos7 ~]# docker save -o /usr/local/webpiece/nginx.tar nginx
[root@centos7 ~]# cd /usr/local/webpiece
[root@centos7 webpiece]# ls
nginx.tar

(2)docker load 导入镜像:将打包好的镜像,导入到( 本机或其他机器的 )本地镜像仓库中

# 作用:将 docker save 打包的镜像,导入到本地镜像仓库中
// 简单来说,就是 docker save 将镜像打包,可以发给其他机器,然后可以使用 docker load 导入到本地仓库中

# 命令格式
// docker load [命令参数][被导入镜像压缩文件的名称] $ docker load < [被导入镜像压缩文件的名称]
// docker load --input [被导入镜像压缩文件的名称] 
# 命令参数(OPTIONS)
// -i, --input string 指定要打入的文件,如没有指定,默认是STDIN

// 代码示例:为了更好的演示效果,先将以前的nginx删除( 也可以将压缩包发送到其他机器进行测试,效果一样 )
[root@centos7 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
nginx        latest    0c404972e130   9 days ago   135MB
[root@centos7 ~]# docker rmi nginx
[root@centos7 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE

// 导入:将打包好的镜像,导入到本地镜像仓库
[root@centos7 ~]# docker load < nginx.tar
[root@centos7 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
nginx        latest    0c404972e130   9 days ago   135MB

基于模板 / 容器,创建镜像

基于镜像模板,创建镜像cat 模板文件名.tar | docker import - [自定义镜像名]

// 登录系统模板镜像网站
// 找到一个模板,如:ubuntu-16.04-x86_64.tar.gz,进行下载
// 地址为:https://download.openvz.org/template/precreated/ubuntu-16.04-x86_64.tar.gz

[root@centos7 ~]# wget --no-check-certificate https://download.openvz.org/template/precreated/ubuntu-16.04-x86_64.tar.gz

[root@centos7 ~]# ls
anaconda-ks.cfg  ubuntu-16.04-x86_64.tar.gz

// 根据模板,创建镜像,命令格式:cat 模板文件名.tar | docker import - [自定义镜像名]
[root@centos7 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
// 使用镜像模板,创建镜像
[root@centos7 ~]# cat ubuntu-16.04-x86_64.tar.gz | docker import - ubuntu-mini
sha256:0fe96f8049345436fbd067ec480fe0f88e20864cc4b98d4c53d00c0c8304b28b
[root@centos7 ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
ubuntu-mini   latest    0fe96f804934   4 seconds ago   505MB

「 (1)基于容器,创建镜像  /  (2)基于容器,制作模板,然后,创建镜像 」

方式(1)使用 docker commit,基于容器,创建镜像

# 命令格式:docker commit -m '改动信息' -a "作者信息" [container_id][new_image:tag]

// 下载镜像,然后,创建一个容器
[root@centos7 ~]# docker pull centos
[root@centos7 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       latest    e6a0117ec169   12 months ago   272MB
[root@centos7 ~]# docker run -it centos /bin/bash
// 进入容器,并创建一个额外的目录 hello
[root@42f6039f2034 /]# mkdir hello
[root@42f6039f2034 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

// 根据容器,创建镜像
[root@centos7 ~]# docker commit -a "webpiece" -m "mkdir /hello" 42f6039f2034 centos-hello
[root@centos7 ~]# docker images
REPOSITORY     TAG       IMAGE ID       CREATED         SIZE
centos-hello   latest    8489ef47fc67   4 seconds ago   272MB  // 新创建的镜像
centos         latest    e6a0117ec169   12 months ago   272MB

// 使用新创建的镜像,创建一个容器,进入查看,额外的目录 hello 是存在的,符合预期
[root@centos7 ~]# docker run -it centos-hello /bin/bash
[root@aef534a975e7 /]# ls
bin  dev  etc  hello  home  lib  lib64	lost+found  media  mnt	opt  proc  root  run  sbin  srv  sys  tmp  usr	var

方式(2):使用 docker export,基于容器,制作镜像模板  -->  使用 docker import,基于镜像模板,创建镜像

[root@centos7 ~]# docker ps
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS          PORTS     NAMES
aef534a975e7   centos-hello   "/bin/bash"   10 minutes ago   Up 10 minutes             vigorous_rubin
42f6039f2034   centos         "/bin/bash"   12 minutes ago   Up 12 minutes             affectionate_tharp

// 基于容器,创建模板
[root@centos7 ~]# docker export 42f6039f2034 > centos-hello2.tar
[root@centos7 ~]# ls
anaconda-ks.cfg  centos-hello2  centos-hello2.tar

// 基于模板,创建镜像
[root@centos7 ~]# cat centos-hello2.tar | docker import - centos-hello2
sha256:7361c6b09fba521a088121cb907af31a72ef839b755d33013495d390474f3684
[root@centos7 ~]# docker images
REPOSITORY      TAG       IMAGE ID       CREATED          SIZE
centos-hello2   latest    7361c6b09fba   3 seconds ago    272MB
centos-hello    latest    8489ef47fc67   13 minutes ago   272MB
centos          latest    e6a0117ec169   12 months ago    272MB