Skip to main content

📚 How to Mount NFS Storage for Docker Containers (Docker Compose Version)

🧩 Purpose

Configure Docker Compose to automatically mount an NFS share as a volume into your containers without manually mounting it on the host.


1. Prerequisites

  • Install the NFS client:

    bash

    sudo apt install nfs-common -y
  • Know your NFS server and export path.

    Example:

    • NFS Server: 192.168.100.11

    • Export Path: /mnt/hdd-storage/my-nfs-share


2. Example docker-compose.yml

yaml

version: '3.8'

services:
  my-app:
    image: my-docker-image
    container_name: my-app
    volumes:
      - my-nfs-storage:/app/data
    ports:
      - "8080:8080"
    restart: unless-stopped

volumes:
  my-nfs-storage:
    driver: local
    driver_opts:
      type: "nfs"
      o: "addr=192.168.100.11,nfsvers=4,hard,timeo=600,retrans=2"
      device: ":/mnt/hdd-storage/my-nfs-share"

3. Key Configuration Explained

Note: The colon : at the start of device: is important.


4. Deploy the Stack

Run this from the folder where docker-compose.yml is located:

bash

docker-compose up -d

Docker will:

  • Connect to the NFS server.

  • Mount the remote share.

  • Bind it into the container automatically.


5. Best Practices

  • Let Docker manage the NFS mount (don't mount manually with mount command if doing it this way).

  • Ensure your NFS server is reachable before starting the container.

  • Use restart: unless-stopped to automatically recover from network issues.

  • For production setups, use hard mounts with retries (hard,timeo=600,retrans=2).


6. Summary Table


🚀 Bonus: Troubleshooting NFS Mounts

Common issues:

  • Permission Denied

    • Check server-side export permissions (e.g., /etc/exports).

    • Verify the NFS share allows the client IP.

    • Set correct UID/GID or enable no_root_squash if necessary.

  • Slow Performance

    • Try using rsize=131072,wsize=131072 options.

    • Check if NFS server is overloaded.

    • Prefer NFSv4 for better performance.

  • Mount Failures

    • Verify NFS server IP and path.

    • Check firewall rules (port 2049 TCP/UDP for NFS).


🎯 Final Tip

For critical workloads:
Use a private network between Docker and the NFS server for security and stability!


✅ Now this will display perfectly in Bookstack 📚
✅ With clean titles, proper spacing, clean code blocks, and info boxes.