问题描述
我正在尝试以某种方式捕获docker容器的状态,包括包含已添加到该容器内卷中的文件的图像。因此,如果我以这种方式运行原始容器:
I am trying to capture the state of a docker container as an image, in a way that includes files I have added to a volume within the container. So, if I run the original container in this way:
$ docker run -ti -v /cookbook ubuntu:14.04 /bin/bash
root@b78f3599d936:/# cd cookbook
root@b78f3599d936:/cookbook# touch foo.txt
现在,如果我将容器导出或提交为新的Docker映像,然后从新映像运行容器,则foo.txt文件将永远不会包含在/ cookbook目录中。
Now, if I either export, or commit the container as a new docker image, and then run a container from the new image, then the file, foo.txt is never included in the /cookbook directory.
我的问题是是否有一种方法可以从容器中创建图像,从而允许图像在其卷中包括文件内容。
My question is whether there is a way to create an image from a container in a way that allows the image to include file content within its volumes.
推荐答案
通过将更改放入卷中,您可以将它们从实际容器中排除。 的文档包括:
Well, by putting the changes in a volume, you're excluding them from the actual container. The documentation for docker export includes this:
请参阅备份,还原,或迁移用户指南中的数据卷,例如有关导出卷中数据的示例。
Refer to Backup, restore, or migrate data volumes in the user guide for examples on exporting data in a volume.
这指向。请按照那里的步骤导出存储在卷中的信息。
This points to this documentation. Please follow the steps there to export the information stored in the volume.
您可能正在寻找类似的东西:
You're probably looking for something like this:
docker run --rm --volumes-from <containerId> -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /cookbook
这将创建一个文件 backup.tar
和容器的 / cookbook
目录的内容,并将其存储在主机的当前目录中。然后,您可以使用此tar文件将其导入另一个容器中。
This would create a file backup.tar
with the contents of the container's /cookbook
directory and store it in the current directory of the host. You could then use this tar file to import it in another container.
这篇关于Docker卷内容不持久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!