In this post I will show you how to install a NFS Volume Plugin for Docker Swarm to persist your data on NFS
Pre-Requisites
If you have not installed your NFS Server, you can have a look at this post
Installing the Driver
We will mount the NFS server to create the directory so first install the dependencies:
$ sudo apt install nfs-common -y
Download the latest version of netshare:
$ wget https://github.com/ContainX/docker-volume-netshare/releases/download/v0.36/docker-volume-netshare_0.36_armhf.deb
Install, enable and start:
$ sudo dpkg -i docker-volume-netshare_0.36_armhf.deb
$ sudo systemctl enable docker-volume-netshare
$ sudo service docker-volume-netshare start
Demo
Let's mount the NFS export directory to our filesystem:
$ sudo mount 192.168.0.115:/opt/nfs /mnt
Create the directory on the NFS server:
$ mkdir /mnt/volumes/voltest
Run a container with the volume mapping:
$ docker run -i -t --volume-driver=nfs -v 192.168.0.115/opt/nfs/volumes/voltest4:/mount alpine /bin/sh
Create a file:
$ touch /mount/file.txt
Exit the container:
$ exit
Now run the container again and see if the data persisted:
$ docker run -i -t --volume-driver=nfs -v 192.168.0.115/opt/nfs/volumes/voltest4:/mount alpine /bin/sh
$ ls /mount/
file.txt
You can also created namespace volumes:
$ mkdir /mnt/volumes/voltest2
$ docker volume create --driver nfs --name voltest2 -o share=192.168.0.115:/opt/nfs/volumes/voltest2
$ docker run -i -t -v voltest2:/mount alpine /bin/sh
And also with docker-compose:
version: "3.5"
services:
web:
image: nginx
volumes:
- nginx.vol:/usr/share/nginx/html
ports:
- 86:80
networks:
- web
networks:
web:
driver: overlay
name: web
volumes:
nginx.vol:
driver: nfs
driver_opts:
share: 192.168.0.115:/opt/nfs/volumes/voltest2