Docker Data Volumes and data volume containers — Use Cases

Sunny Kay
@sunkay IMHO
Published in
3 min readMay 14, 2016

It is tough to find pragmatic information on how to configure and use data volumes in docker images and containers. The following use case approach helped demystify data volumes and data containers.

Use case 1: Make a host (OSX/Mac) directory to be available in the container (Data Volume)

There are several advantages for this and recent docker versions (1.1.11 and above + Docker for Mac Beta) make it easier to mount/bind the directories to be available on the container.

  • On a Mac the following directories are available to be mounted on the container: /Users/*, /Volumes, /private and /tmp
  • /Mac mounts the entire user accessible file system. I don’t know what purpose this would serve and it it better to avoid doing this.
  • You have to provide absolute paths for the source or host directory
  • You cannot mount a host directory in a Dockerfile since it voilates the portability of the docker images
> docker run -ti --rm -v ~/src/server:/src busybox> docker run -ti --rm -v /Mac:/src busybox

Use case 2: Create a persistent named data volume container that can be reused across containers (Data Container)

Docker containers are ephemeral and they do not persist, all the data created by a running container is gone when it is terminated or if the image is recreated. To persist data across containers using the data volume containers is an excellent pattern, recommended by Docker.

  • Create a named data volume container
  • Mount this data volume container to the non persistent and portable containers
  • Use an extremely light weight image like tianon/true which is 125 bytes
> docker create -v /data --name mongo-db-data tianon/true /bin/true> docker ps -a
bbef8fe5095a tianon/true "/bin/true" 37 seconds ago Created mongo-db-data
> docker run -ti --rm --volumes-from mongo-db-data busybox> docker run -ti --rm --volumes-from mongo-db-data busybox---> /data should be mounted in both these 2 running containers

Use case 3: Where the heck are my volumes located on my mac?

Beta version of Docker for Mac uses xhyve and not VirtualBox. The way to get into this VM is to run the following command

> screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty Log in as root> ls -l /var/lib/docker

A better way is to use the docker volume ls & docker volume inspect commands. Docker volume rm will remove the volumes

Use case 4: Removing data volumes & data containers

# list shared data volume containers
> docker ps -a
# removing a data volume container
> docker rm <container-id>
You can remove a docker data volume container while there are active running containers. The volume itself will be deleted when the last container exits.

Use case 5: Sharing volumes between containers

This is very similar to the data volume containers, but not as portable as a container. You can create a shared volume with the following command:

> docker volume create --name shared-data# access in the container as such
> docker run -ti --rm -v shared-data:/data busybox
# remove docker volumes using the following commands
> docker volume list
> docker volume rm shared-data

Use Case 6: Using docker-compose to initiantiate web-app, mongo and database container

Use case #: Back up and restore a persistent data volume

** need to update **

#backup
$ docker run --rm --volumes-from mongo-db-data -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata
#restore$ docker run --rm --volumes-from mongo-db-data -v $(pwd):/backup ubuntu bash -c "cd /dbdata && tar xvf /backup/backup.tar --strip 1"

--

--