DEV Community

May Meow
May Meow

Posted on

Docker Volume on NFS?

No Problem. Did you know you can have your volumes on nfs server? Yes i know you can mount NFS folder on server and then on point docker volume to this folder, but i will show you
how to mount volume right with docker. Btw you can read more about docker storage on official docker documentation. As prerequisity you will need to have installed NFS server.

Now you can run docker image with volume pointed to your nfs server

docker service create -d \
    --name nfs-service \
    --mount 'type=volume,source=nfsvolume,target=/app,volume-driver=local,volume-opt=type=nfs,volume-opt=device=:/var/docker-nfs,"volume-opt=o=addr=10.0.0.10,rw,nfsvers=4,async"' \
    nginx:latest
Enter fullscreen mode Exit fullscreen mode

If you want to mount nfs volume right in your docker-composer (docker-composer file version 3.2 and up is required) then you can do it as follows:

version: "3.9"

# ...

services:
  wordpress-app:
    # ...
    volumes:
      - wordpress:/var/www/html

volumes:
  wordpress:
    driver: local
    driver_opts:
      type: nfs
      o: addr=server_ip,rw,vers=4.1
      device: ":/path/to/nfs/folder"
Enter fullscreen mode Exit fullscreen mode
  • device: is full path to your folder, it coresponds with path in your server's nfw export config
  • addr=server_ip,rw,vers=4.1: Change server_ip to your server ip address, rw gives container both read and write access to the nfs mount, vers tells to docker which verison of NFS to use.

If your container needs root access to the folder do not forget add no_root_squash to you exports as server by default translating all rood operations on remote server to the non privileged user because of security.

For what it is all good? For sharing data between containers. For example if you know Gitlab and their gitlab-runner. You can make it autoscale or you can have them more. When you do this you will need to share build chache otherwise you will run to errors when you trying access cache form runner that is diferrent than that one which wrote cache file ;)

I recommend this to use for storing files like images or website files (like on wordpress) but not database. You newer know how database server reacts when you mount will disconnect, it will cause datafile corruption and you server hang on start. If you need more than one Database server with same date then you will need configure replication.

Originally posted on themaymeow.com
Thats all. Bye!

Top comments (0)