gpt4 book ai didi

import - docker 恢复对容器的更改

转载 作者:行者123 更新时间:2023-12-04 14:44:19 25 4
gpt4 key购买 nike

我正在尝试对我的 docker 容器进行快照,以便我可以恢复到单个时间点。

我看过 docker savedocker export但这些似乎都没有做我正在寻找的。我错过了什么吗?

最佳答案

您可能想使用 docker commit .此命令将从您的一个 docker 容器创建一个新的 docker 镜像。通过这种方式,您可以稍后基于该新镜像轻松创建新容器。

请注意 docker commit命令不会保存存储在 Docker 中的任何数据 data volumes .需要制作的 friend 们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

关于import - docker 恢复对容器的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26322247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com