Docker Containers are stateless, if you want a persistent service like MySQL I always recommend having your database services outside the swarm, but if you need to look at Persistent Storage, there's a lot, including GlusterFS and NFS.

Using Docker Swarm with NFS

Today we will setup MySQL backed with NFS. Use this blog post if you have not setup NFS yet.

Errors I Faced

I ran into this error when trying to bind mount my volume path:

chown: changing ownership of `/var/lib/mysql/': Operation not permitted mysql | Cannot change ownership of the database directories to the 'mysql'

The Workaround

I had to create a Docker Volume first, specifying the NFS configuration then use those details when spinning up my service.

Setting it all up

Create the MySQL Data Directory:

mkdir /mnt/volumes/mysql

Create the Volume on NFS:

docker volume create --driver local \
  --opt type=nfs \
  --opt o=addr=192.168.1.2,uid=1000,gid=1000,rw \
  --opt device=:/mnt/volumes/mysql-test \
  vol_mysql

Create the MySQL Service:

docker service create --name mysql \
  --network docknet \
  --mount "type=volume,source=vol_mysql,destination=/var/lib/mysql,readonly=false" \
  --env MYSQL_ROOT_PASSWORD=password \
  --replicas 1  hypriot/rpi-mysql

Other Options

If you are looking for something else like GlusterFS, look at my blogs on Docker Swarm and GlusterFS