问题
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