Docker volume deleting content from container when started

Hi,

I want to create a docker volume for a specific folder in the container and maps this folder locally.
This is my command

docker run -d --link httpd:httpd --name=test10 -v /data/docker/volumes/test10:/jboss-eap-7.0/domain/configuration bcf43571d397 tailf /dev/null

The result is an empty folder in container test10 /jboss-eap-7.0/domain/configuration
and
an empty folder in /data/docker/volumes/test10

What am I doing wrong?
When I create files in the container and outside they appear in both sides.

What volume mapping does is simply create a link between the local folder and the one in the docker host.

I think you have to copy the contents of /jboss-eap-7.0/domain/configuration into you local folder the first time.

It should not be like this, in my previous job, I used to create the volumes and all was fine. Here this doesn’t work. I can’t remember the docker version in my previous job but here it is Docker version 1.12.0
Could this be effecting?

Everything you describe sounds correct:

  • You have an empty directory on the host.
  • When you use docker run -v to bind-mount it into a container, you see the same content there.
  • If you create or edit a file either on the host or in the container in the shared directory, the same change appears on the other side.

What are you expecting to work differently?

(What are you trying to do with this container setup in the first place? It very vaguely looks like you’re trying to mount a host directory into a container that does nothing. On current Docker you should look at the docker volume command set to manage containers, rather than the older data-volume container pattern.)

Hi,

What I wanted is to build a container and take the content in the folder /jboss-eap-7.0/domain/configuration and map it on the docker host. So initially on the Docker host, I have an empty folder. I wanted this folder to be populated by the content created in the container. This was not working.

Actually after some testing I found a work around. I created a named volume and this copied the new content from the container to my empty host folder. The only issue is that I have to create a symbolic link to the default path. Not sure if this can be changed. Below are the commands

docker run -d -v benjamin:/jboss-eap-7.0/domain/configuration/ --name=test10 bcf43571d397

docker inspect test10 | grep -A1 volume
"Source": “/var/lib/docker/volumes/benjamin/_data”,
“Destination”: “/jboss-eap-7.0/domain/configuration”,

cd /data/docker/volumes/test10/ #this is the path on my docker host
ln -s /var/lib/docker/volumes/benjamin/_data/ benjamin

cd benjamin
ls
application-roles.properties default-server-logging.properties host-master.xml host_xml_history mgmt-users.properties
application-users.properties domain.xml host-slave.xml logging.properties
benjamin domain_xml_history host.xml mgmt-groups.properties

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):

  1. 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
  1. 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.

  1. Stop and delete your container
docker stop test10
docker rm test10
  1. 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

You could just use docker cp instead of docker exec and cp inside, but there is a better solution which I never remember.

This way Docker does its job