How to Mount a Custom Volume Inside Containers in Docker Swarm

If the new volume has been mounted incorrectly, see the Remove Incorrectly Mounted Volume section first; otherwise, proceed directly to the Mount New Volume section.

Remove Incorrectly Mounted Volume

  1. List dangling containers based on status:
    1. docker ps -a -f status=created
    2. docker ps -a -f status=exited
  2. Remove the dangling containers:
    1. docker rm $(docker ps -a -f status=created -q)
    2. docker rm $(docker ps -a -f status=exited -q)
  3. Remove the volume:
    • docker volume rm <volume name>

Mount New Volume

  1. On the NFS server, access /etc/exports
  2. Add  "<directory on the NFS server>" <IP address of the container>(rw,fsid=1,crossmnt,no_subtree_check,no_root_squash,async) to the exports file, for example:
"/opt/mi/data" 172.XX.XX.XX(rw,fsid=1,crossmnt,no_subtree_check,no_root_squash,asynс
  1. Restart the NFS server
  2. Access the container and edit the .yml manifest file
  3. Add the new volume to the container manifest in the following format:  - <volume_name>:<mount path inside the container>:rights, for instance:
volumes:
- custom:/opt/mi/data:rw
  1. Add the new volume instance:
volumes:
  custom:
    driver_opts:
      type: "nfs"
      o: "addr=<NFS server IP address>, nolock, soft, rw" 
      device: "<path to the shared directory on NFS server>"
  1. Save the changes
  2. Re-apply the manifest:
$ docker stack deploy -c mi-swarm.yml --with-registry-auth mi