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
- List dangling containers based on status:
docker ps -a -f status=created
docker ps -a -f status=exited
- Remove the dangling containers:
docker rm $(docker ps -a -f status=created -q)
docker rm $(docker ps -a -f status=exited -q)
- Remove the volume:
docker volume rm <volume name>
Mount New Volume
- On the NFS server, access
/etc/exports
- Add
"<directory on the NFS server>" <IP address of the container>(rw,fsid=1,crossmnt,no_subtree_check,no_root_squash,async)
to theexports
file, for example:
"/opt/mi/data" 172.XX.XX.XX(rw,fsid=1,crossmnt,no_subtree_check,no_root_squash,asynс
- Restart the NFS server
- Access the container and edit the .yml manifest file
- 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
- 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>"
- Save the changes
- Re-apply the manifest:
$ docker stack deploy -c mi-swarm.yml --with-registry-auth mi