How to use NFS with Docker "Local" volume driver in Portainer.io
** Revised Sept 16, 2018 to accommodate Portainer 1.19.2 changes **
So, you have your Docker environment up and running, and now you want start experimenting with persistent volumes, and redirecting the persistent volumes to an external NFS server; this article is here to help...
The first thing you need to do is make sure that you have nfs-utils installed on your docker hosts. For CentOS, the command is "yum install -y nfs-utils" and for Ubuntu the command is "sudo apt-get install nfs-common"
Once you have this installed, then you have the underlying binaries Docker needs to establish NFS connections.
On your NFS server, create a share and then an export for that share, make sure that your Docker Hosts IP is set as an authorised IP address (often seen as NFS client IP address or Admin Host). If you have a swarm cluster, you need to repeat this for each Docker Host's IP. Take a note of your export path, you will need that later. In my example below, i am using RockStor as the NFS server, but you can use any NFS server.
Now that is done, switch to Portainer UI, and click on "Volumes", then "Add Volume".
When the "local" driver is selected, you can see an option to "Use NFS Volume". Enable this setting to configure NFS for the new volume.
Give the volume a name that reflects the intended use (eg nfsdata)
In the "address" box, put in the IP address or FQDN of your NFS server. In the "NFS Version" drop-down box, select either NFS v3 or NFS v4 (depending on what your NFS server supports). In the "mount point" box, put in the share you are exposing from NFS for this volume (in my case for this article its :/export/container).
Review the options we are setting for you (chosen for maximum performance, but you can change if you understand NFS mount options).
Click "create the volume"
The volume will now be created (linked/mounted)
Now, you can attach the volume to your container. Switch to containers (or services) and create a new container. Click on Volumes, select the volume from above, and then set a mount point inside the container. Start the container (in my case, CentOS for example purposes).
To check all is well, open a console to the container, navigate to the container path (/nfsdata) and you can see it exists and you can read/write to it.
If you want to be 100% sure all is well, open a shell connection to your docker host, and type "mount" and you can see that Docker has mounted the NFS export for you (with the options you set).
All done, you now have your container writing its persistent data off to a NFS server without needing any 3rd party volume drivers.
Be aware that if/when you delete the volume, you are only deleting the MOUNT, not the actual data stored in NFS.
If you are interested to know more about NFS options (the o line), then read here: https://www.cyberciti.biz/faq/linux-unix-tuning-nfs-server-client-performance/
Senior Site Reliability Engineer at N26
6 年Thanks for this post, it will help us in or project!, also I now that there are some software that can replicate folders thru servers, do you think thats also a good approach for data persistence?