Skip to main content
Docker Volume on NFS?
  1. Posts/

Docker Volume on NFS?

·367 words·2 mins·
Docker Linux WIP
May Meow
Author
May Meow
MayMeow is a developer and cybersecurity enthusiast with a passion for cryptography, DevSecOps, and open-source contributions. They enjoy creating tools that strengthen digital security, blending creativity and technology to innovate in fields like PHP and .NET. Always exploring new frontiers in tech, MayMeow is dedicated to safeguarding the digital landscape through their work.

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

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"
  • 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.

Thats all. Bye!

This is Work In Progress #WIP post. That means it may contain errors, and probably contains them.

Reply by Email

Related

🐋 How to Run Wordpress in Docker
·1239 words·6 mins
Docker Wordpress
How to Install and Configure Samba
·432 words·3 mins
Administration Notes Guide Wiki File Sharing Linux Windows
Xfce Terminal Color Emoji
·133 words·1 min
Tutorial Linux Xfce 100DaysToOffload