docker工人将更改还原到容器

我正在尝试快照我的docker集装箱,以便我可以恢复到一个单一的时间点。

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

你可能想使用docker commit 。 这个命令将从你的一个docker容器创build一个新的docker镜像 。 这样,您可以稍后根据新图像轻松创build新的容器。

请注意, docker commit命令不会保存存储在Docker 数据卷中的任何数据。 对于那些你需要做备份 。


例如,如果您正在使用以下Dockerfile来声明一个卷,并且将每5秒钟将date写入两个文件(一个在卷中,另一个不在):

 FROM base VOLUME /data CMD while true; do date >> /data/foo.txt; date >> /tmp/bar.txt; sleep 5; done 

从它build立一个形象:

 $ docker build --force-rm -t so-26323286 . 

并从中运行一个新的容器:

 $ docker run -d so-26323286 

稍等一下,以便正在运行的Docker容器有机会将date写入两个文件几次。

 $ 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 

现在让我们来validation一个从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文件,可以使用--volumes-from 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