说明:在实际运用中,经常会遇到服务迁移、升级或者部署等问题,想要快速的实现,可以使用docker镜像的方式,把事先准备好的docker镜像导入在目标服务器中即可。
一、save与export、load与import比较
save:导出镜像, 会将镜像完整保存,包括历史版本和元数据信息,所以文件会比export导出大,适合做服务迁移或备份;
load:导入镜像,会把版本和元数据信息一起导进去;
export:把容器导出为镜像,只把镜像当前的状态保存下来,没有历史版本等信息,所有文件比较小;
import:把容器导入为镜像;
注:导出的文件皆为 .tar 格式的
二、docker镜像的导入导出命令: save、load
1. 命令-- save
[root@mvxl8369 ~]# docker save --help
Usage: docker save [OPTIONS] IMAGE [IMAGE...]
Save one or more images to a tar archive (streamed to STDOUT by default)
Options:
-o, --output string Write to a file, instead of STDOUT
解释: 将一个或多个镜像以流的方式保存在tar中
公式:
# docker save -o xxxx.tar <image1>:<tag1> <image2>:<tag2> .....
或 # docker save > xxxx.tar <image1>:<tag1> <image2>:<tag2> .....
翻译 # docker save -o 目标.tar <镜像名称>:<标签> ....
实例:
[root@mvxl8369 test]# docker save -o organization.tar organization:v1
[root@mvxl8369 test]# docker save > organization2.tar organization:v1
2. 命令-- load
[root@mvxl8369 ~]# docker load --help
Usage: docker load [OPTIONS]
Load an image from a tar archive or STDIN
Options:
-i, --input string Read from tar archive file, instead of STDIN
-q, --quiet Suppress the load output
公式:
# docker load -i xxxx.tar
或 # docker load < xxxx.tar
三、docker容器的导入导出 import、export
1. 命令-- export
[root@mvxl8369 backup]# docker export --help
Usage: docker export [OPTIONS] CONTAINER
Export a container's filesystem as a tar archive
Options:
-o, --output string Write to a file, instead of STDOUT
公式:
# docker export -o xxxx.tar 容器名/ID
或 # docker export > xxxx.tar 容器名/ID
2. 命令-- import
[root@mvxl8369 backup]# docker import --help
Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
Import the contents from a tarball to create a filesystem image
Options:
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Set commit message for imported image
公式:
# docker import xxxx.tar 导入原镜像名称和tag
# docker import xxxx.tar <image:tag> 导入时对镜像重命名和tag