在很多环境中,由于网络问题,无法docker pull 镜像,就需要tar包方式获取镜像,以下就制作tar包的方式和区别加以介绍:
一、docker commit && docker save && docker load 保存容器内容为镜像
docker run -itd redis:6.0.16 /bin/bash
721156a2acb38fc8391c36c409c9cb6ab8942fcda78a828af2e32b9c5b842612
docker commit 721156a2acb38fc8391c3 redis_use
sha256:6044d3a75a1706d67b5e49650ca749aa1dfc3ed95bc8f44043d9d0d0c2bdc971
docker save -o redis_use.tar redis_use
docker save -o redis_test.tar redis_use
ll
total 112676
-rw------- 1 root root 115378688 Nov 7 11:18 redis_test.tar
# 把tar 包发送到其他机器加载
docker load -i redis_test.tar
3070f050ae24: Loading layer [==================================================>] 4.096kB/4.096kB
Loaded image: redis_use:latest
docker images | grep redis
redis_use latest 6044d3a75a17 16 minutes ago 112MB
如果要定义tag,如下操作
docker run -itd redis:6.0.16 /bin/bash
acd739e8fead16c20e553dc916440054c03c3e44ad40b020db1391aaa94e7a99
docker commit acd739e8fead16c20e553 redis_use:v1
sha256:46e1e86e2562b227c3ced13c7c5e97af0d0321d6b4a10a224a416f753515d1cf
docker save -o redis_use_v1.tar redis_use:v1
# 把tar 包发送到其他机器加载
docker load -i redis_use_v1.tar
1d2d823b0f8c: Loading layer [==================================================>] 4.096kB/4.096kB
Loaded image: redis_use:v1
docker images | grep redis
redis_use v1 46e1e86e2562 10 minutes ago 112MB
二、docker export && docker import 保存容器内容为镜像
docker run -itd redis:6.0.16 /bin/bash
7cbed6522d8a0e7f4d0b85b069cd199c70401d3d4a650347e7d6b51280503a02
docker export -o redis_use_v2.tar 7cbed6522d8a0e7f4d0b85
docker import redis_use_v2.tar redis:v2
sha256:a78a9f22b085edae03eb1248a6e84d82ff6ed0ed353446e3af4cd2fe182b5b79
docker images | grep redis
redis v2 a78a9f22b085 5 seconds ago 108MB
三、异常情况
3.1、docker import docker save的包
docker import redis_use_v1.tar redis:v1
sha256:f079d70bb557518c9f7215f80720c4a9b13a48fe728c86dfb6800c015f7b9389
docker images | grep redis
redis v1 f079d70bb557 4 seconds ago 115MB
#可以加载,但是没有办法使用
docker run -itd redis:v1 /bin/bash
d48784a1618cc7c03c6571bec6c1ecb1cd9905adcfec18ff105a8da63a9512aa
docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"/bin/bash\": stat /bin/bash: no such file or directory": unknown.
docker inspect redis:v1
[
{
"Id": "sha256:f079d70bb557518c9f7215f80720c4a9b13a48fe728c86dfb6800c015f7b9389",
"RepoTags": [
"redis:v1"
],
"RepoDigests": [],
"Parent": "",
"Comment": "Imported from -",
"Created": "2022-11-07T07:06:39.683003465Z",
"Container": "",
"ContainerConfig": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": null,
"Cmd": null,
"Image": "",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": null
},
"DockerVersion": "18.09.9",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": null,
"Cmd": null,
"Image": "",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": null
},
"Architecture": "amd64",
"Os": "linux",
"Size": 115356256,
"VirtualSize": 115356256,
"GraphDriver": {
"Data": {
"MergedDir": "/iso2/root/var/lib/docker/overlay2/ac95e2b15114808fa46c93855f1edbd8d7dc1972421482f2f7e392993ce25604/merged",
"UpperDir": "/iso2/root/var/lib/docker/overlay2/ac95e2b15114808fa46c93855f1edbd8d7dc1972421482f2f7e392993ce25604/diff",
"WorkDir": "/iso2/root/var/lib/docker/overlay2/ac95e2b15114808fa46c93855f1edbd8d7dc1972421482f2f7e392993ce25604/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:9df4129a40789ea469a97ec538470791e9c9a531c514f02f9188ec3ac145ef93"
]
},
"Metadata": {
"LastTagTime": "2022-11-07T15:06:39.695859731+08:00"
}
}
]
# 可以看到没有镜像元信息,无法使用
3.2、docker load docker export 的包
docker load -i redis_use_v2.tar
open /iso2/root/var/lib/docker/tmp/docker-import-109823248/bin/json: no such file or directory
# 报错
四、docker save 和docker export 区别总结
docker save 保存镜像为镜像文件,使用docker load 加载,docker export 保存容器为镜像文件,使用docker import 加载
docker commit 与docker save结合,也可实现导出容器为镜像文件
docker export 导出的镜像是不带历史记录的,导入后镜像变为一层,docker commit 会保留原有镜像层,经过commit后增加一层
docker export 比 docker save的包要小,原因是save的是一个分层的文件系统,export导出的只是一层文件系统
五、docker load 和docker import 区别总结
docker load 加载docker save 保存的镜像,docker import 加载docker export保存的镜像
docker load不能对载入的镜像重命名,而docker import可以为镜像指定新名称
六、参考文档
1、https://blog.csdn.net/Wqr_18390921824/article/details/123083151
2、https://blog.csdn.net/JineD/article/details/118761569文章来源:https://www.toymoban.com/news/detail-717576.html
3、https://m.php.cn/article/452245.html文章来源地址https://www.toymoban.com/news/detail-717576.html
到了这里,关于docker 本地保存镜像方法--docker export 和docker save 使用介绍的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!