如何在Docker Compose中为每个容器创建单独的卷 [英] How to create separate volumes for each container in docker compose

查看:279
本文介绍了如何在Docker Compose中为每个容器创建单独的卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个docker图像,要为其运行多个容器.该docker映像依赖于几件事,所有容器都将保持不变,但唯一的不同将是容器的配置.

I have created a docker image for which I want to run multiple containers. This docker image is dependent on few things which will remain same for all the containers but the only difference will be configuration of the containers.

version: '2.4'

services:
    s1:
        image: testdockerimg:latest
        volumes:
            - /home/andrew/Documents/CVAI/configs/s1config.json:/home/andrew/Documents/CVAI/configs/config.json
            - /home/andrew/Documents/CVAI:/home/andrew/Documents/CVAI/
        restart: always
        mem_limit: 2g

    s2:
        image: testdockerimg:latest
        volumes:
            - /home/andrew/Documents/CVAI/configs/s2config.json:/home/andrew/Documents/CVAI/configs/config.json
            - /home/andrew/Documents/CVAI:/home/andrew/Documents/CVAI/
        restart: always
        mem_limit: 2g

在上面的文件中,您可以看到我为s1 s2容器提供了s1configs2config,但其余内容保持不变,这就是为什么/home/andrew/Documents/CVAI体积对于两个容器都相同的原因容器.因此,泊坞窗在两个容器之间共享卷,因此所有数据在容器之间混合并且没有分离.

In the above file, you can see that I am giving s1config and s2config for s1 s2 containers but rest of the things remains same and this is why /home/andrew/Documents/CVAI volume is same for both the containers. Due to this, docker is sharing the volume between both the containers and thus all the data is mixing between containers and not getting separated.

有什么办法可以在多个容器之间分隔卷?? 我不想为此创建多个docker.请帮忙.谢谢

Is there any way we can separate the volumes between multiple containers.? I do not want to create multiple dockers for this. Please help. Thanks

编辑

更新的docker-compose文件:

Updated docker-compose file :

version: '2.4'

services:
    s1:
        image: testdockerimg:latest
        volumes:
            - /home/andrew/Documents/s1/configs/s1config.json:/home/andrew/Documents/CVAI/configs/config.json
            - /home/andrew/Documents/s1:/home/andrew/Documents/CVAI/
        restart: always
        mem_limit: 2g

    s2:
        image: testdockerimg:latest
        volumes:
            - /home/andrew/Documents/s2/configs/s2config.json:/home/andrew/Documents/CVAI/configs/config.json
            - /home/andrew/Documents/s2:/home/andrew/Documents/CVAI/
        restart: always
        mem_limit: 2g

推荐答案

在主机上,每个容器应具有自己的卷.它们可以以相同的路径安装在容器中.每个容器可以使用相同的映像,并且将以相同的名称在主机上看到其专用的卷.

On the host each container should have its own volume. They can be mounted in the container at the same path. Each container can use the same image and will see its dedicated volume on the host under the same name.

这篇关于如何在Docker Compose中为每个容器创建单独的卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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