BoxBoat Blog

Service updates, customer stories, and tips and tricks for effective DevOps

x ?

Get Hands-On Experience with BoxBoat's Cloud Native Academy

Mounting Volumes in Docker Swarm 1.12

by Brandon Mitchell | Friday, Aug 12, 2016 | Docker

featured.png

If you've tried to mount a volume with your services in Docker's new swarm, you've likely hit at least one road block, the primary one being the lack of a -v option:

$ docker service create --name nginx \
  -v /mysite:/usr/share/nginx/html -p 80:80 nginx
unknown shorthand flag: 'v' in -v
See 'docker service create --help'.

The solution to this comes in the form of --mount but it has a completely different syntax than volume mounts. The syntax for the mount involves using a name=value pair for type, source, and target.

Host Volumes

If you want to do a host volume, that is implemented under the covers as a bind mount using the following syntax:

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

Named Volumes

If you've created a volume with docker volume create then the syntax to mount that is:

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

Now that you can do a named volume, you can create any type of volume that uses a driver from your choice of sources.

There is a Catch

The big catch to be aware of is any mount you create in your swarm service needs to be portable to any node where you service can run. Using constraints, you can limit where swarm will schedule your container to only the nodes where you have this data copied. See the docs on configuring a constraint to use this. If you need to have only a single version of your data, rather than multiple copies, then solutions like flocker, gluster, or a simple nfs mount will be needed to have each node point to the same data.