We often receive the question “What is the difference between a Docker Volume and a Flocker Volume?”. This post serves as a useful walkthrough for those who are unfamiliar with persistent data and Docker containers.
We will look at four ways to deal with data and Docker containers:
--volume-driver
flagIf you want your data to persist outside the lifecycle of the container or host, you will want to use the Flocker plugin for Docker. If you want to be able to move or reschedule your containers to different hosts then you will also need to use the Flocker plugin for Docker. If you don’t need your data to persist outside the lifecycle of the container or its host, your options are wider.
Let’s looks at each use case in turn and its advantages and disadvantages.
The first three cases entail using Docker’s native volume capabilities. You have the flexibility to store the data in the container itself, or on the local host. In some cases, your data outlives the life of the container. However, in all cases, your data does not outlive the life of the container host.
docker run --name my-special-container busybox
#then write to a directory you want to use inside the container
In the above case:
docker run --name my-special-container -v /container/dir busybox
In the above case:
Let’s look at what happens in Case #1 and Case #2 if you remove the container.
docker rm -f my-special-container
In Case #1 your data does not persist if you destroy the container: you destroy all the data and changes you made inside it. In Case #2 your data persists if you destroy the container but if you destroy the Docker host itself, all the data and changes you made inside it are also gone.
Both of these cases are far from ideal. However, Docker allows for you to mount the directory within the host filesystem. Let’s look at this use case.
The following command shows how to mount a path on the Docker host’s filesystem as a volume inside the container.
docker run --name my-special-container -v /host/dir:/container/dir busybox
In this example, everything the container process writes to the directory /container/dir is instead written directly to the /host/dir directory on the host’s filesystem. This bypasses Docker’s union filesystem so you get the fastest write speed possible. With this method you can allow the container to write to a path which could be an existing NFS share, formatted block device, or anything which can be mounted as a POSIX filesystem on your host. However, managing these mounts is a manual process, especially if you need to reschedule your container somewhere else - you must make sure that the same storage system that was available on the previous host is mounted on the new host before you start the container again.
Let’s look at what happens if we remove a container.
docker rm -f my-special-container
In this case:
To re-use your data in another container on the same host, simply reference the same directory:
docker run --name my-special-container -v /host/dir:/container/dir busybox
Alternatively you can create a volume first and run the container separately referenced that volume:
docker volume create --name somethinglocal
docker run --name my-special-container -v somethinglocal:/container/dir busybox
In the case above:
-v /some/directory
except we give the volume a name first.To move data to a new host, you have a few options:
You can imagine how tough trying to keep track of which directories belong to which containers and building a home-brewed orchestration tool to migrate the data could be. One way to solve this is by using systems that support Docker Volume Drivers also know as Docker Volume Plug-ins.
Takeaway: In all the above cases If you migrate a container to another host or your host machine dies you will leave your data behind or potentially lose your data.
Docker takes a basic view of volume management which means that it’s easy to get up and running but leaving you to customize and configure for durability, fail-over, and high availability. External storage management is not Docker’s core concern, instead leaving it to the community to provide specialized data management solutions. ClusterHQ worked with the Docker team to enable a framework for Docker plug-ins that provides added functionality to the end user.
How do you use Flocker via the Flocker plugin for Docker? Let’s see.
--volume-driver
flagThis example uses Amazon Elastic Block Storage (EBS) for storage. Flocker supports a growing list of storage options including EBS, OpenStack Cinder, EMC, NetApp, Dell, HP, Huawei and more. To see options other than Amazon EBS, scroll down.
The following command allows you to create a container and a data volume that will persist beyond the life of the container and the container host. Additionally, the volume will automatically follow your container no matter where they are scheduled within a cluster of servers.
user@host1 $ docker run --volume-driver flocker -v flocker-volume:/container/dir --name=container-xyz
An external block storage device or share is created (if it does not already exist) and mounted to our host and our directory “/container/dir” is bound to it. Meaning when we write to /container/dir we are really writing to a external disk such as an Amazon EBS volume.
This means all your data will remain safe even if your container or Docker hosts crash, or get removed and moved from one host to another. In-fact, Flocker will automatically move your data volume from one host to another when your container moves so that all your data remains available. This helps with planned moves, server upgrades, or HA for container applications.
Let’s see this in action and migrate our container from our initial host (host1) to another one (host2) by deleting and rerunning our container.
Remove container from host1
user@host1 $ docker rm -f container-xyz
Run container on host2
user@host2 $ docker run --volume-driver flocker -v flocker-volume:/container/dir --name=container-xyz
We still have our data volume without doing any extra work! Boom!
This also works with the docker volume create
command if you prefer to create your volume separately from the docker run
command.
user@host1 $ docker volume create --name persistent-vol-1 -d flocker
user@host1 $ docker run -v persistent-vol-1:/container/dir
Takeaway: Flocker will make sure to remount the external storage device as your container moves from host to host. With a Flocker Volume your data will persist on the external storage provider.
You can pick and choose from one of the many backend storage vendors best suited for your needs.
If you have other questions about your Docker Volumes you are more than welcome to reach out to our team.