📚 How to Mount NFS Storage for Docker Containers (with Proper Permissions)
Purpose
Mount a remote NFS share on a Linux server for use by Docker containers, ensuring stable operation, correct permissions, and automatic remounting.
1. Install NFS Client on the Server
sudo apt update
sudo apt install nfs-common -y
2. Create a Local Mount Directory
Create a local directory where the NFS share will be mounted:
sudo mkdir -p /srv/nfs-mount
sudo chown $(whoami):$(whoami) /srv/nfs-mount
(You can replace
/srv/nfs-mount
with your preferred path.)
3. Mount the NFS Share (Manual Test)
Example:
sudo mount -t nfs4 192.168.100.11:/mnt/hdd-storage/my-nfs-share /srv/nfs-mount
nfs4
: Use NFS version 4 for better performance and locking.proto=tcp
: Reliable transport protocol.hard
: Wait for server recovery instead of failing immediately.timeo=600
: Timeout setting for NFS operations.retrans=2
: Retry failed operations twice.sec=sys
: Default authentication method._netdev
: Ensure mount occurs only after network is ready.
4. Verify That the Mount Worked
mount | grep nfs
You should see output like:
192.168.100.11:/mnt/hdd-storage/my-nfs-share on /srv/nfs-mount type nfs4 (...)
5. Make the Mount Persistent (Auto-Mount on Boot)
Edit your /etc/fstab
file:
sudo nano /etc/fstab
Add the following line at the bottom:
192.168.100.11:/mnt/hdd-storage/my-nfs-share /srv/nfs-mount nfs4 rw,relatime,hard,proto=tcp,timeo=600,retrans=2,sec=sys,_netdev 0 0
Save and exit (Ctrl+O
, Enter
, Ctrl+X
).
6. Test the fstab Entry Without Rebooting
sudo mount -a
No errors = success! ✅
7. Using the NFS Mount with Docker
When running your containers, bind-mount the NFS storage into the container:
docker run -d \
--name my-container \
-v /srv/nfs-mount:/app/data \
my-docker-image
This will allow your Docker containers to directly access the NFS storage.
✅ Summary
- Use NFSv4 (
nfs4
) whenever possible. - Always include
_netdev
in your/etc/fstab
entries. - Use
hard
mounts to protect container file operations during NFS issues. - Bind-mount NFS paths carefully into containers.
- Avoid using NFS for database storage unless network latency is extremely low.
🛠️ Useful Commands
- Check NFS mounts:
mount | grep nfs
- Manual remount all:
sudo mount -a
- Test connectivity:
ping nfs-server-ip
🦠 Additional Notes
- If the NFS server reboots, containers may pause temporarily.
- If using
docker-compose.yml
, you can map volumes to/srv/nfs-mount
. - For production, consider creating a systemd mount unit for better recovery behavior.
No Comments