docker 操作手册(三)镜像打包
前言
用户既可以使用 docker load 来导入镜像存储文件到本地镜像库,也可以使用 docker import 来导入一个容器快照到本地镜像库。
这两者的区别在于容器快照文件将丢弃所有的历史记录和元数据信息(即仅保存容器当时的快照状态),而镜像存储文件将保存完整记录,体积也要大。
此外,从容器快照文件导入时可以重新指定标签等元数据信息。
参考:https://yeasy.gitbook.io/docker_practice/container/import_export
保存镜像
不会保存容器对文件系统内容的变更
docker image save --help
Save image(s) to an archive
Description:
Save an image to docker-archive or oci-archive on the local machine. Default is docker-archive.
Usage:
docker image save [options] IMAGE [IMAGE...]
Examples:
podman image save --quiet -o myimage.tar imageID
podman image save --format docker-dir -o ubuntu-dir ubuntu
podman image save > alpine-all.tar alpine:latest
Options:
--compress Compress tarball image layers when saving to a directory using the 'dir' transport. (default is same compression type as source)
--format string Save image to oci-archive, oci-dir (directory with oci manifest type), docker-archive, docker-dir (directory with v2s2 manifest type) (default "docker-archive")
-m, --multi-image-archive Interpret additional arguments as images not tags and create a multi-image-archive (only for docker-archive)
-o, --output string Write to a specified file (default: stdout, which must be redirected)
-q, --quiet Suppress the output
docker image save postgres -o myimg.tar
导出容器快照
将容器的文件系统内容导出为 tar 存档
docker container export --help
Export container's filesystem contents as a tar archive
Description:
Exports container's filesystem contents as a tar archive and saves it on the local machine.
Usage:
docker container export [options] CONTAINER
Examples:
podman container export ctrID > myCtr.tar
podman container export --output="myCtr.tar" ctrID
Options:
-o, --output string Write to a specified file (default: stdout, which must be redirected)
docker container export -o myimg.tar postgres
导入镜像
docker image load -i myimg.tar
导入快照
cat myimg.tar | docker import - my_postgres/ubuntu:v1.0
此外,也可以通过指定 URL 或者某个目录来导入,例如
docker import http://example.com/exampleimage.tgz example/imagerepo
docker import myimg.tar my_postgres
解决快照无法启动的错误
如果遇到Error: no command or entrypoint provided, and no CMD or ENTRYPOINT from image,可以在导入快照时修改CMD命令:
docker import --change 'CMD ["/usr/bin/bash"]' myimg.tar my_postgres
然后就可以启动容器了:
docker run -it -d --name test my_postgres
docker exec -it test bash
root@acd481576a8b:/#
但是好像不太行,服务没有创建,接下来试试docker commit
docker commit
根据容器的更改创建新映像
docker commit --help
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:
-a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
-c, --change list Apply Dockerfile instruction to the created image (default [])
--help Print usage
-m, --message string Commit message
-p, --pause Pause container during commit (default true)
docker commit 容器ID my_image
docker image save my_image -o myimg.tar
docker image load -i myimg.tar