简体   繁体   English

2个容器之间的Docker卷

[英]Docker Volumes Between 2 containers

Let's say we have the following docker-compose.yml file 假设我们有以下docker-compose.yml文件

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:

with the following 1.Dockerfile 与以下1.Dockerfile

FROM ubuntu:latest
RUN mkdir -p /folder/
RUN touch /folder/1.txt
VOLUME /folder

and the following 2.Dockerfile 和以下2.Dockerfile

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.

相关问题 Docker-数据量和数据容器之间的区别 - Docker - difference between data volumes and data containers Docker 多个容器之间的命名卷 - Docker named volumes between multiple containers 如何使用Docker Compose中的卷在主机和容器之间共享数据 - How to share data between host and containers using volumes in Docker Compose 无法在 docker-compose 文件中的容器之间共享卷 - Cannot share volumes between containers in a docker-compose file 您如何在 Elastic Beanstalk 应用程序中的 Docker 容器之间共享卷? - How do you share volumes between Docker containers in an Elastic Beanstalk application? 如何在 docker-compose 中的容器之间共享主机卷? - How do I share host volumes between containers in docker-compose? 如何版本控制 Docker 容器的数据量? - How to version control Docker containers' data volumes? 启动 Docker 容器和 ansible 的卷组合列表 - Starting Docker containers with combined list for volumes with ansible 不同容器和系统路径中的 Docker 个卷 - Docker volumes in different containers and system paths 在 Docker 容器中处理持久数据时绑定挂载和卷有什么区别? - What Is The Difference Between Binding Mounts And Volumes While Handling Persistent Data In Docker Containers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM