For local dev I need to mount a different config file in a Docker container. This is easy with the command line -v $(pwd)/bla.yaml:/location/bla.yaml
. Is it possible to do this from a volume
(created with docker volume create bla
) in a docker-compose
file?

- 3,889
- 7
- 37
- 54
-
Is this for swarm mode? – BMitch Feb 12 '19 at 22:39
-
Nope. Not swarm mode. – Jason Leach Feb 13 '19 at 05:14
-
this could be useful: https://stackoverflow.com/questions/66473273/share-single-files-between-docker-containers – Lety Aug 12 '23 at 14:55
3 Answers
You should be able to do it with a bind mount:
version: "3.2"
services:
app:
image: app:latest
volumes:
- type: bind
source: ./bla.yaml
target: /location/bla.yaml

- 511
- 3
- 4
-
3I think this means the file needs to exist on the local file system. I'm looking to mount from a docker volume `docker volume create bla` then mount a file from `bla`. – Jason Leach Feb 12 '19 at 20:24
-
2Ah, I see. I don't see anywhere in the docs where tat is possible. Although, in Kubernetes I think you can mount a subdir of a volume, so I would think that that functionality must exist somewhere. I'll circle back if I come across something. – Mike Breed Feb 15 '19 at 14:02
From what I see, Docker Compose still (Aug. 2023) does not have a built-in feature to mount a single file from a Docker volume directly.
Since using a bind mount to map a single file from your local filesystem into the container is not satisfactory (because based on the local filesystem), you might consider a volume, with initialization.
When you use a Docker volume and that volume is empty, Docker will copy the contents of the directory (or file) from the container into the volume.
See for instance "Understanding Docker Volumes " from Shingai Zivuku
So, if you start with a volume and mount it to a directory in the container where only that single file exists, then your volume will have that file. That does not necessarily help you mount only that file elsewhere, though.
The alternative would be to:
- Mount the whole volume to a temporary directory inside the container.
- Use a script that runs when the container starts to copy the specific file from the temporary directory to the desired location inside the container.

- 1,262,500
- 529
- 4,410
- 5,250
I'm not aware of anything by means of which docker-compose allows copying a single file from the docker volume out of the box.
You can do some tricks to achieve this:
- Mount the entire volume from which you want to copy the single file.
- Since you have all files from the volume available in the mounted directory on a container, you can write some script that copies the required file to the appropriate location in your container. You can automate this using configuration management tools like Ansible. (More on how to configure containers using Ansible is here.)
Note that you will not be able to unmount the volume later on in the same script since your container will be running.

- 1,043
- 1
- 6
- 20