在Docker容器之间共享卷 [英] Sharing volume between Docker containers

查看:166
本文介绍了在Docker容器之间共享卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Docker部署一些服务,并且我想在不同容器之间共享Docker卷。

I am using Docker to deploy some services and I want to share the Docker volumes between different containers.

假设我有一个Docker容器A,它在/ data上安装了一个卷。这是它的Dockerfile:

Suppose I have a Docker container A which mounts a volume at /data. Here is its Dockerfile:

VOLUME /data

据我了解,这会将一个卷附加到容器,但不会将主机目录挂载到该容器。因此,该卷中的数据仍位于容器A中。

From my understanding, this will attach a volume to the container but it will not mount a host directory to the container. So the data inside this volume is still inside the container A.

我有另一个提供FTP服务的容器B。它访问卷/ public下的数据。它的Dockerfile是:

I have another container B which provides an FTP service. It accesses the data under volume /public. Its Dockerfile is:

VOLUME /public


<现在,我想将它们链接在一起,以便可以使用B来管理A的数据。来自Docker文档 https://docs.docker.com/engine/userguide/containers / dockervolumes / 我将使用-volumes-from 标志将A的数据量装载到B。但是此命令会将A的数据装载到<$ c $ B中的c> / data 而不是 / public ,在这种情况下,容器B无法访问数据。我没有找到重命名安装点的任何方法。

Now I want to link them together so that I can use B to manage A's data. From the Docker doc https://docs.docker.com/engine/userguide/containers/dockervolumes/ I shall use the --volumes-from flag to mount A's data volume to B. But this command will mount A's data to /data in B instead of /public, and in this case, the container B is not able to access the data. I didn't see any way to rename the mount point.

有任何建议或最佳实践来处理这种情况吗?

Any suggestions or best practices to handle this case?

仅数据容器为这种情况提供了很好的解决方案。但是,如果您要使用volume-from并将数据装入到不同的装入点,则此问题可能会有所帮助!
如何使用Docker的--volumes映射卷路径-from?

The data-only container gives a good solution for this case. But if you want to use volumes-from and mount the data to different mount point, this question may be helpful! How to map volume paths using Docker's --volumes-from?

推荐答案

您可能会发现很多指向纯数据容器和-来自的数量。但是,从docker 1.9开始,卷已成为一等公民,他们可以使用名称并具有更大的灵活性:

You may find a lot of pointers mentioning data-only containers and --volumes-from. However, since docker 1.9, volumes have become first class citizens, they can have names, and have more flexibility:

现在很容易实现所需的行为,这是示例:

It's now easy to achieve the behavior you want, here's an example :


  1. 创建名称为 service-data 的命名数据卷:

docker volume create --name service-data


  • 然后您可以使用 -v 标志创建一个将其装入/ public文件夹的容器:

  • You can then create a container that mounts it in your /public folder by using the -v flag:

    docker run -t -i -v service-data:/public debian:jessie /bin/bash
    

    出于测试目的,我们在映射的文件夹中创建一个小的文本文件:

    For testing purpose we create a small text file in our mapped folder:

    cd public
    echo 'hello' > 'hello.txt'
    


  • 然后可以将命名卷附加到第二个容器,但是这次在数据文件夹下:

  • You may then attach your named volume to a second container, but this time under the data folder:

    docker run -t -i -v service-data:/data debian:jessie /bin/bash
    ls /data       #-->shows "hello.txt"
    


  • 请记住,如果两个容器都使用不同的图像,请注意所有权和权限!

    Just remember, if both containers are using different images, be careful with ownership and permissions!

    这篇关于在Docker容器之间共享卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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