11

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!

sendmoreinfo
  • 582
  • 6
  • 22
Ross Dargan
  • 5,876
  • 4
  • 40
  • 53

2 Answers2

12

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.

redcalfee
  • 146
  • 1
  • 6
  • 1
    Both links from the answer above are dead :( – adamsfamily Feb 16 '19 at 08:09
  • I think I found the (dead) RexRay link so adding it here: https://collabnix.com/category/docker/docker-volume-plugin/dellemc-rexray/ and another interesting link from the same blog: https://collabnix.com/category/docker/docker-volume-plugin/dellemc-rexray/ – adamsfamily Feb 16 '19 at 08:28
  • I updated the links to point to Docker's docs now that it's online – redcalfee Feb 18 '19 at 01:21
8

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.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • I'm using rc4, and have mounted a network share at /mnt/docker/jackett. Should this work? `docker service create --mount bind,src=/mnt/docker/jackett,dst=/config/.config -p 9117:9117 --name files_jackett dreamcat4/jackett`. The error I get is for --mount: invalid field 'bind' must be a key=value pair. – Ross Dargan Jul 16 '16 at 08:03
  • 3
    It looks like the syntax should be: 'docker service create --mount type=bind,source=/mnt/docker/jackett,target=/config/.config -p 9117:9117 --name files_jackett dreamcat4/jackettclear'! Thanks – Ross Dargan Jul 16 '16 at 10:59