我正在尝试对我的docker容器进行快照,以便我可以恢复到单个时间点。
我看过docker save
和docker export
,但这些似乎都没有找到我正在寻找的东西。我错过了什么吗?
答案 0 :(得分:5)
您可能想要使用docker commit。此命令将从您的 docker容器中创建一个新的 docker image 。这样,您可以稍后根据新图像轻松创建新容器。
请注意docker commit
命令不会保存存储在Docker data volumes中的任何数据。对于那些你需要制作backups的人。
例如,如果您正在使用以下Dockerfile来声明一个卷,并且每隔5秒将该日期写入两个文件(一个在卷中,另一个不在卷中):
FROM base
VOLUME /data
CMD while true; do date >> /data/foo.txt; date >> /tmp/bar.txt; sleep 5; done
从中构建图像:
$ docker build --force-rm -t so-26323286 .
并从中运行一个新容器:
$ docker run -d so-26323286
稍等一下,以便正在运行的docker容器有机会将日期写入两个文件几次。
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07b094be1bb2 so-26323286:latest "/bin/sh -c 'while t 5 seconds ago Up 5 seconds agitated_lovelace
然后将容器提交到新图像so-26323286:snapshot1
:
$ docker commit agitated_lovelace so-26323286:snapshot1
您现在可以看到有两张图片可用:
$ docker images | grep so-26323286
so-26323286 snapshot1 03180a816db8 19 seconds ago 175.3 MB
so-26323286 latest 4ffd141d7d6f 9 minutes ago 175.3 MB
现在让我们验证从so-26323286:snapshot1
运行的新容器是否有/tmp/bar.txt
个文件:
$ docker run --rm so-26323286:snapshot1 cat /tmp/bar.txt
Sun Oct 12 09:00:21 UTC 2014
Sun Oct 12 09:00:26 UTC 2014
Sun Oct 12 09:00:31 UTC 2014
Sun Oct 12 09:00:36 UTC 2014
Sun Oct 12 09:00:41 UTC 2014
Sun Oct 12 09:00:46 UTC 2014
Sun Oct 12 09:00:51 UTC 2014
见证这样的容器没有任何/data/foo.txt
文件(因为/data
是一个数据卷):
$ docker run --rm so-26323286:snapshot1 cat /data/foo.txt
cat: /data/foo.txt: No such file or directory
最后,如果要访问第一个(仍在运行的)容器中的/data/foo.txt
文件,可以使用docker run --volumes-from
选项:
$ docker run --rm --volumes-from agitated_lovelace base cat /data/foo.txt
Sun Oct 12 09:00:21 UTC 2014
Sun Oct 12 09:00:26 UTC 2014
Sun Oct 12 09:00:31 UTC 2014
Sun Oct 12 09:00:36 UTC 2014
Sun Oct 12 09:00:41 UTC 2014
Sun Oct 12 09:00:46 UTC 2014
Sun Oct 12 09:00:51 UTC 2014
Sun Oct 12 09:00:56 UTC 2014
Sun Oct 12 09:01:01 UTC 2014
Sun Oct 12 09:01:06 UTC 2014
Sun Oct 12 09:01:11 UTC 2014
Sun Oct 12 09:01:16 UTC 2014
答案 1 :(得分:0)
这里是如何使用Docker Hub中的hello-world
映像进行操作的示例
首先运行hello-world映像,然后下载该映像:
docker run hello-world
然后获取要获取的图像的哈希值
docker history hello-world
您将看到类似的内容:
IMAGE CREATED
fce289e99eb9 15 months ago
fce289e99eb9
是您的哈希码。
要标记此图像,请运行:
docker tag fce289e99eb9 hello-world:SNAPSHOT-1.0
要列出存储库的所有标签,请使用:
docker image ls hello-world
您将得到类似的东西:
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world SNAPSHOT-1.0 fce289e99eb9 15 months ago 1.84kB
hello-world latest fce289e99eb9 15 months ago 1.84kB