Reputation: 2084
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
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
Upvotes: 0
Views: 95
Reputation: 4451
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
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.
So what is happening is that your volume is initiated with the data from your c1 container when it is created.
Then the pre-populated volume is mounted to both c1 and c2.
Data pre-population to the volume happens create time. After that the volume is mapped with that data that was populated during creation.
Upvotes: 1