Docker 1.12 swarm mode and container volumes

Ross Dargan picture Ross Dargan · Jul 15, 2016 · Viewed 11.6k times · Source

I have several container that require state - I will only ever set the scale to 1, but I would like it so that no matter which host they start on the volume would be shared.

I'm guessing I need to use a network mount to achieve this (which is fine), but how on earth do I configure the volume using docker swarm 1.12?

I know I can use docker volume create, and I think I might need to specify a driver but I'm struggling to find a single example of this!

Answer

redcalfee picture redcalfee · Sep 30, 2016

docker service create --mount ... provides two options for persistent data; bind mounts and named volumes. Bind mounts persist on the host created so will not work for you since not shareable.

Named volumes can be created using docker volume create or created implicitly as part of docker service createusing --mount option, e.g.

$ docker volume create -d --driver cio --name cassandradb --opt profile=CASSANDRA
$ docker service create \
--mount source=cassandradb,target=/var/lib/cassandra,volume-driver=cio \
--replicas 1 \
--name cassandra \
cassandra

docker service create defaults to named volumes so the type is not specified in the example. The volume driver supports portable volumes. Other volume drivers such as RexRay or Flocker also support portable volumes. Here is an article with examples on RexRay.

There are also --mount options for volume labels and volume options. You can pickup more info on bind mounts and named volumes.

Additional example using the Storidge volume driver.