# 📚 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:
    
    <div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary"><div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none rounded-t-[5px]">bash</div><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-sidebar-surface-primary text-token-text-secondary dark:bg-token-main-surface-secondary flex items-center rounded-sm px-2 font-sans text-xs">  
    </div></div></div><div class="overflow-y-auto p-4" dir="ltr"></div></div>```
    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`

<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary" id="bkmrk-yaml"><div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none rounded-t-[5px]">yaml</div><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-sidebar-surface-primary text-token-text-secondary dark:bg-token-main-surface-secondary flex items-center rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr"></div></div>```
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:

<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary" id="bkmrk-bash"><div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none rounded-t-[5px]">bash</div><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-sidebar-surface-primary text-token-text-secondary dark:bg-token-main-surface-secondary flex items-center rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr"></div></div>```
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.