Reputation: 9183
I want the source code of my project to be accessible via a named volume (rather than using bind mount method). But there is a confusion for me, I don't know how should I mount my host directory to a named volume. So, I define a volume in my docker-compose.yml:
volumes:
appData:
services:
nginx:
.
.
.
volumes:
- appData:/usr/share/data
Everything works, but then how should I add my host file to the named volume? And when.
Upvotes: 1
Views: 3335
Reputation: 14903
Unfortunately, there is no such straightforward way to achieve this as if now. Docker docs suggest using bind mounts OR 3rd party volume plugins in such cases. Couldn't see any near future plan to implement the same.
However, you can do it in an alternate way manually -
Create the volume(while using compose, volume is created with current directory(PROJECTNAME) as prefix) -
$ docker volume create ${PROJECTNAME}_appData
Create a dummy container & copy the host directory into the named volume & verify it -
$ docker container create --name dummy -v ${PROJECTNAME}_appData:/root alpine #Create
$ docker cp ${PWD}/source_myfile.txt dummy:/root/myfile.txt #copy
$ docker run -v ${PROJECTNAME}_appData:/root alpine ls -l /root #Verify
Now when you do a docker-compose up -d
, it will skip creating a volume since it already exists.
PS - I understand it's not a standard fix but that's all I could found as a quick solution. For a permanent fix, you can use 3rd party volume plugins.
https://docs.docker.com/engine/extend/legacy_plugins/
Get more details on the issue -
Ref - https://github.com/moby/moby/issues/25245
Upvotes: 2