[英]Docker Volumes Between 2 containers
version: '3'
services:
c1:
build:
context: .
dockerfile: 1.Dockerfile
volumes:
- data:/folder
c2:
build:
context: .
dockerfile: 2.Dockerfile
volumes:
- data:/folder
depends_on:
- c1
volumes:
data:
FROM ubuntu:latest
RUN mkdir -p /folder/
RUN touch /folder/1.txt
VOLUME /folder
FROM ubuntu:latest
RUN mkdir -p /folder/
RUN touch /folder/2.txt
VOLUME /folder
whenever i do docker-compose up
每当我做
docker-compose up
then do 然后做
docker-compose run --rm c2 bash
ls folder
or 要么
docker-compose run --rm c1 bash
ls folder
i always get the folder from c1
no matter what, isn't c2
supposed to overwrite c1
's volume 无论如何我总是从
c1
获取文件夹, c2
是否不应该覆盖c1
的卷
You can read about the behavior you are describing in the docs here: https://docs.docker.com/storage/volumes/#populate-a-volume-using-a-container 您可以在此处阅读有关文档中描述的行为的信息: https : //docs.docker.com/storage/volumes/#populate-a-volume-using-a-container
If you start a container which creates a new volume, as above, and the container has files or directories in the directory to be mounted (such as /app/ above), the directory's contents are copied into the volume.
如果如上所述启动一个容器来创建新的卷,并且该容器在要挂载的目录中有文件或目录(例如/ app /),则目录的内容将复制到该卷中。 The container then mounts and uses the volume, and other containers which use the volume also have access to the pre-populated content.
然后,该容器将安装并使用该卷,并且使用该卷的其他容器也可以访问预填充的内容。
So what is happening is that your volume is initiated with the data from your c1 container when it is created. 因此,正在发生的事情是在创建卷时使用c1容器中的数据启动了卷。
Then the pre-populated volume is mounted to both c1 and c2. 然后将预填充的卷安装到c1和c2上。
Data pre-population to the volume happens create time. 数据预填充到卷上会产生时间。 After that the volume is mapped with that data that was populated during creation.
之后,将卷与创建期间填充的数据进行映射。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.