Nadilson Ferreira
Nadilson Ferreira

Reputation: 171

docker shared volumed not working as described in the documentation

I am now learning docker and according to the documentation a shared data volume shall solely be destroyed when the last container holding a link to the shared volume is removed with the -v flag. Nevertheless, in my initial tests this is not the behaviour that I saw.

From the documentation: Managing Data in Containers

If you remove containers that mount volumes, including the initial dbdata container, or the subsequent containers db1 and db2, the volumes will not be deleted. To delete the volume from disk, you must explicitly call docker rm -v against the last container with a reference to the volume. This allows you to upgrade, or effectively migrate data volumes between containers.

I did the following:

  1. docker run -d -v /dbdata --name dbdata ubuntu:14.04 echo Data-only container for postgres
  2. docker run -d --volumes-from dbdata --name db1 ubuntu:14.04 /bin/bash
  3. Created some files on the /dbdata directory
  4. Exited the db1 container
  5. docker run -d --volumes-from dbdata --name db2 ubuntu:14.04 /bin/bash
  6. I could access the files created on item 3 and create some new files
  7. Exited the db2 container
  8. docker run -d --volumes-from dbdata --name db3 ubuntu:14.04 /bin/bash
  9. I could access the files created on item 3 and 6 and create some new files
  10. Exited the db3 container
  11. Removed all containers without the -v flag
  12. Created the db container again, but the data was not there.

As stated in the user manual:

This allows you to upgrade, or effectively migrate data volumes between containers.

I wonder what I am doing wrong.

Upvotes: 2

Views: 618

Answers (1)

Behe
Behe

Reputation: 7940

You are doing nothing wrong. In step 12, you are creating a new container with the same name. It has a different volume, which initially is empty.

Maybe the following example can illustrate what is happening (ids and paths will/may vary on your system or in other docker versions):

$ docker run -d -v /dbdata --name dbdata ubuntu:14.04 echo Data-only container for postgres
7c23cc1e6637e29f36c6cdd4c1461f6e1742b201e05227279ac3db55328da674

Run a container that has a volume /dbdata and give it the name dbdata. The Id is returned (your Id will be different).

Now lets inspect the container and print the "Volumes" information:

$ docker inspect --format "{{ .Volumes }}" dbdata
map[/dbdata:/var/lib/docker/vfs/dir/248641a5f51a80b5004f72f622a7329835e93881e9915a01b3c7112189d0b55e]

We can see that your /dbdata volume is located at /var/lib/docker/vfs/dir/248641...

Let's create some new data inside the container's volume:

$ docker run --rm --volumes-from dbdata ubuntu:14.04 /bin/bash -c "echo fuu >> /dbdata/test"

And check if it is available

$ docker run --rm --volumes-from dbdata -it ubuntu:14.04 cat /dbdata/test
fuu

Afterwards you delete the containers, without the -v flag.

$ docker rm dbdata

The dbdata container (with id 7c23cc1e6637) is gone, however is still present on your filesystem, as you can see if you inspect the folder:

$ cat /var/lib/docker/vfs/dir/248641a5f51a80b5004f72f622a7329835e93881e9915a01b3c7112189d0b55e/test
fuu

(Please note: if you use the -v flag and delete the container with docker rm -v dbdata the files of the volume on your host filesystem will be deleted and the above cat command would result in a No such file or directory message or similar)

Finally, in step 12. you start a new container with a different volume and give it the same name: dbdata.

docker run -d -v /dbdata --name dbdata ubuntu:14.04 echo Data-only container for postgres
2500731848fd6f2093243da3be064db79e76d731904e6f5349c3f00f054e5f8c

Inspection yields a different volume, which is initially empty.

docker inspect --format "{{ .Volumes }}" dbdata
map[/dbdata:/var/lib/docker/vfs/dir/faffba00358060024026412203a1562125f73d2bdd69a2202483e858dda04740]

If you want to re-use the volume, you have to create a new container and import/restore the data from the filesystem into the data container. In your case, you should not delete the data container in the first place, as you want to reuse the volume from it.

Upvotes: 3

Related Questions