Since docker defaults to directories as volumes and only bind-mounts a file when the source is an existing file it is a bit harder to use.
With the file you want to share in your current directory you can use bind-mounts in docker-compose.yml
services:
app1:
# ...
volumes:
- ./file:/path/to/file1:rw
app2:
# ...
volumes:
- ./file:/path/to/file2:rw
This is requires the file to already exist outside the containers. See also the caveats with bind-mounting files in jubnzv's answer.
Depending on your use case maybe a better workaround could be using symlinks and mounting the shared file in a directory volume:
# tree layout in containers:
ContainerA:
|- shared/
| ` shared-file
`- app_folder_a/
|- file-1
|- file-2
|- file-3
`- shared-file-a -> /shared/shared-file
ContainerB:
|- shared/
| ` shared-file
`- app_folder_b/
|- file-a
|- file-b
|- file-c
`- shared-file-b -> /shared/shared-file
# docker-compose.yml
services:
app1:
# ...
volumes:
- ./shared/:/shared/:rw
app2:
# ...
volumes:
- ./shared/:/shared/:rw
Or if you do not want/can have the file or the shared directory on the host you can use named volumes with symlinks:
services:
app1:
# ...
volumes:
- shared:/app_folder_a
app2:
# ...
depends_on:
- app1
volumes:
- shared:/shared
volumes:
shared:
and the shared file symlinked in containerB:
ContainerB:
|- shared/
| ` ...
`- app_folder_b/
|- file-a
|- file-b
|- file-c
`- shared-file-b -> /shared/shared-file-a
The important part here is that when creating the new volume shared the first time docker will prepopulate it with the contents of the target directory in the container. So to work correctly app1 need to be started first so the shared volume is populated with the contents of app_folder_a - therefore the depends_on.
With this you then have the shared volume, populated with the contents of containerA's /app_folder_a/ and mounted to the same as well as on containerB's /shared/ while /app_folder_b/shared-file-b is a symlink to /shared/shared-file-a.