在Docker中,如何在容器之间共享文件,然后将其保存到图像? [英] In Docker, how can I share files between containers and then save them to an image?

查看:179
本文介绍了在Docker中,如何在容器之间共享文件,然后将其保存到图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将容器的共享卷中的数据提交到一个图像。我似乎无法做到这一点我得到这样的印象,这可能在Docker中是不可能的,但这似乎完全不符合不将数据留在主机上的整体哲学,所以我认为必须有办法做到这一点。

I want to commit the data in a container's shared volume to an image. I cannot seem to do it? I kind of get the impression this perhaps is not possible in Docker but that seems totally at odds with the whole philosophy of not leaving data on the host so part of me thinks there must be a way to do this.

在1号航站楼中开启一个容积。

Start up a container in Terminal 1 with a volume.

$ docker run -it -v /data ubuntu:14.10 /bin/bash
root@19fead4f6a68:/# echo "Hello Docker Volumes." > /data/foo.txt



2。终端2



在终端2中启动第二个容器,容器1中的文件就是这样,所以Docker卷都可以正常工作。

2. Terminal 2

Start up a second container in Terminal 2 the file from container 1 is there so docker volumes are all working.

$ docker run -it --volumes-from 19fead4f6a68 ubuntu:14.10 /bin/bash

root@5c7cdbfc67d8:/# cat /data/foo.txt
Hello Docker Volumes.



3。终端3



我的理解是我只能将差异提交给图像,所以我检查两个容器上的diff是什么。对于一些奇怪的原因,我的更改不显示!

3. Terminal 3

My understanding is that I can only commit diffs to images so I check what the diffs are on both the containers. For some bizarre reason my changes do not show up!??

$ docker diff 19fead4f6a68
A /data

$ docker diff 5c7cdbfc67d8
A /data



4。返回终端1



我在卷文件夹外创建一个文件

4. Back in Terminal 1

I create a file outside of the volume folder

 root@19fead4f6a68:/# echo "Docker you are a very strange beast...." > /var/beast.txt



5。返回终端3



我们现在有一些我们可以提交的修改,虽然我很沮丧,因为这不是我需要与我的另一个容器共享的卷的数据。

5. Back in Terminal 3

We now have some changes we can commit although I am rather frustrated as this is not the data from the volume I needed to share with my other container.

$ docker diff 19fead4f6a68
A /data
C /var
A /var/beast.txt

显然这是设计。任何人都有任何想法,为什么docker不允许我将卷数据保存到提交?有没有办法在容器之间共享文件,然后将它们保存到图像?我觉得有一些我失踪的东西?特别是在共享数据的同时避免主机依赖关系。

Clearly this is by design. Anyone have any ideas as to why docker don't allow me to save volume data to a commit? Is there anyway at all to share files between containers and then save them to an image? I feel like there must be something I am missing? Especially to the ends of sharing data whilst avoiding host dependencies.

推荐答案

卷不在容器图像之外。这就是他们所用的 - 将数据导入不在映像中的容器。

Volumes are outside of container images. That's exactly what they are for - bringing data inside a container that isn't in the image.

Docker文档


一个数据卷是一个特别的指定的目录在一个或多个容器内绕过联盟文件系统,为持久或共享数据提供几个有用的功能:

A data volume is a specially-designated directory within one or more containers that bypasses the Union File System to provide several useful features for persistent or shared data:


  • 数据卷可以在容器之间共享和重复使用

  • 直接对数据量进行更改

  • 更新图像时不会对数据卷进行更改

如果要将某些更改保存为图像的一部分,请在图像内进行更改而不是在一个卷。如果要跨多个容器共享更改,请将该数据放入卷中,但必须自己安排快照,回滚等,因为Docker没有该功能。

If you want to save some changes as part of an image, make the changes inside the image and not in a volume. If you want to share changes across multiple containers, put that data in a volume but you have to make your own arrangements for snapshots, rollback, etc., because Docker doesn't have that feature.

也许你会对 Flocker 感兴趣。

这篇关于在Docker中,如何在容器之间共享文件,然后将其保存到图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆