Docker 1.12 swarm mode and container volumes

I'm not sure the syntax has been finalized on this as the github pull request 24334 shows, but the cli option you're looking for is docker service --mount .... When using something like this, you create a situation where you need to make sure the data is available for mounting, so you're looking at drivers like nfs or gluster. Otherwise if the container needs to move and you've mounted data directly from the host, it would be restarted without the needed mount.


Edit: the current --mount syntax is:

docker service create --name nginx \
  --mount type=bind,source=`pwd`/static-site,target=/usr/share/nginx/html \
  -p 80:80 nginx

for host/bind mounts or

docker service create --name nginx \
  --mount type=volume,source=web,target=/usr/share/nginx/html \
  -p 80:80 nginx

for a named volume mount. I also posted a blog post on the topic because I'm hearing the same question a lot.


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.