How mount NFS volume to container with sub-path via Docker Compose

How mount NFS volume to container with sub-path via Docker Compose

Use volumes block to setting with common NFS setting (don't forget install NFS driver in your machine) in docker compose file

And when you need to attach the volume to same service, your can use volumes properties, set type to your volume and set option subpath in your NFS mountpoint follow example

services:
  backend:
    image: example/backend
    volumes:
      - type: volume
        source: db-data
        target: /data
        volume:
          nocopy: true
          subpath: sub

volumes:
  db-data:
    driver: local
    driver_opts:
      type: "nfs"
      o: "addr=192.168.x.x,rw"
      device: ":/mount/path/to/server"