Docker 2 个容器之间的体积
Docker Volumes Between 2 containers
假设我们有以下 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:
与以下 1.Dockerfile
FROM ubuntu:latest
RUN mkdir -p /folder/
RUN touch /folder/1.txt
VOLUME /folder
及以下2.Dockerfile
FROM ubuntu:latest
RUN mkdir -p /folder/
RUN touch /folder/2.txt
VOLUME /folder
每当我做 docker-compose up
然后
docker-compose run --rm c2 bash
ls folder
或
docker-compose run --rm c1 bash
ls folder
我总是从 c1
获取文件夹,无论如何,c2
不应该覆盖 c1
的卷
您可以在此处的文档中阅读您所描述的行为: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. The container then mounts and uses the volume, and other containers which use the volume also have access to the pre-populated content.
所以发生的事情是您的卷在创建时使用 c1 容器中的数据启动。
然后将预填充的卷安装到 c1 和 c2。
数据预填充到卷发生在创建时间。之后,该卷将映射到创建期间填充的数据。
假设我们有以下 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:
与以下 1.Dockerfile
FROM ubuntu:latest
RUN mkdir -p /folder/
RUN touch /folder/1.txt
VOLUME /folder
及以下2.Dockerfile
FROM ubuntu:latest
RUN mkdir -p /folder/
RUN touch /folder/2.txt
VOLUME /folder
每当我做 docker-compose up
然后
docker-compose run --rm c2 bash
ls folder
或
docker-compose run --rm c1 bash
ls folder
我总是从 c1
获取文件夹,无论如何,c2
不应该覆盖 c1
的卷
您可以在此处的文档中阅读您所描述的行为: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. The container then mounts and uses the volume, and other containers which use the volume also have access to the pre-populated content.
所以发生的事情是您的卷在创建时使用 c1 容器中的数据启动。
然后将预填充的卷安装到 c1 和 c2。
数据预填充到卷发生在创建时间。之后,该卷将映射到创建期间填充的数据。