How to create separate volumes for each container in docker compose

女生的网名这么多〃 提交于 2020-07-04 00:13:55

问题


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

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.

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

EDIT

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

回答1:


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.



来源:https://stackoverflow.com/questions/62708135/how-to-create-separate-volumes-for-each-container-in-docker-compose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!