【发布时间】:2014-12-06 23:17:48
【问题描述】:
我正在尝试对我的 docker 容器进行快照,以便可以恢复到单个时间点。
我查看了docker save 和docker export,但这些似乎都没有达到我想要的效果。我错过了什么吗?
【问题讨论】:
标签: import load save export docker
我正在尝试对我的 docker 容器进行快照,以便可以恢复到单个时间点。
我查看了docker save 和docker export,但这些似乎都没有达到我想要的效果。我错过了什么吗?
【问题讨论】:
标签: import load save export docker
这是一个如何使用来自 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
【讨论】:
您可能想使用docker commit。此命令将从您的一个 docker 容器 创建一个新的 docker 映像。这样,您以后可以轻松地基于该新图像创建一个新容器。
请注意,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
【讨论】: