Hi,
This is a little late, but it is the second time I bump into this question and I think I have an explanation that will be useful for the future me.
When you init a container using the -v
parameter AND an absolute path in the left side of the mapping like in
docker run -d --link httpd:httpd --name=test10 -v /data/docker/volumes/test10:/jboss-eap-7.0/domain/configuration bcf43571d397 tailf /dev/null
You are declaring a bind mount. A bind mount obscures the original content of the directory inside the container. So, inside the container you will see only the contents of the directory in your host (empty in your case). You can write into the mount from the container or from the host and both parties will see the changes.
When you start a container mounting a named volume into a container directory and the container directory is not empty, the files in that directory are copied into the named volume. To use a volume you should create it(https://docs.docker.com/storage/volumes/#create-and-manage-volumes
) first and map it by name in the -v
parameter like you did in
docker run -d -v benjamin:/jboss-eap-7.0/domain/configuration/ --name=test10 bcf43571d397
A little warning to your solution: When using volumes, only Docker processes should modify volume data (https://docs.docker.com/storage/#choose-the-right-type-of-mount
) in the host (/var/lib/docker/volumes/
on Linux).
I can imagine an alternative solution (be careful as I didn’t test the following snippets):
- Start your container with a bind mount mapping your desired host location (
/data/docker/volumes/test10
) to a temporary empty directory inside the container (/tmp/newdirectory
)
docker run -d --name=test10 -v /data/docker/volumes/test10:/tmp/newdirectory bcf43571d397 tailf /dev/null
- Use
docker exec
to cp
your desired files into the temporary directory inside the container
docker exec -d test10 cp -r /jboss-eap-7.0/domain/configuration /tmp/newdirectory/
Now, your files should be reflected in your host directory.
- Stop and delete your container
docker stop test10
docker rm test10
- Start your container again but this time bind mount your host directory (
/data/docker/volumes/test10
) to your desired directory inside your container (/jboss-eap-7.0/domain/configuration
).
docker run -d --name=test10 -v /data/docker/volumes/test10:/jboss-eap-7.0/domain/configuration bcf43571d397 tailf /dev/null
The original content of your container directory will be obscured by the contents of the mount, but it should be fine, because it has the contents of the previous instance of the container.
In this way, you do not modify contents that only Docker should (volume contents), also the host and the container share the files.
Hope this helps anyone.
Best regards