How to use NFS with Docker containers?

Published on Aug. 22, 2023, 12:05 p.m.

To use NFS (Network File System) with Docker containers, you can mount an NFS share as a volume in your container. Here is an example of how to do this:

  1. First, make sure NFS is installed and configured on your host machine.
  2. Create an NFS export with the files you want to share. You can do this by editing the /etc/exports file on your host machine and specifying the directory you want to export and the IP addresses or hostnames of the machines you want to allow access.
  3. On your Docker host, create a new Docker volume that maps to the NFS export. You can do this by running the following command:
docker volume create --driver local \
--opt type=nfs \
--opt o=addr=,rw \
--opt device=:/ \
my-nfs-volume

This will create a Docker volume called my-nfs-volume that maps to the NFS export at /path-to-nfs-export on the server with IP address <nfs-server-ip>. The rw option specifies that the volume is read-write.
4. When running your container, mount the NFS volume by specifying the -v option with the name of the Docker volume you created:

docker run -v my-nfs-volume:/path/to/mount my-image

This will mount the NFS volume as a directory at /path/to/mount in your container.

Note that you may also need to configure permissions to ensure that your container has access to the files in the NFS share. This can be done by setting appropriate permissions on the NFS export and/or by specifying the UID/GID of the user inside the container using the --user option when running the container.

Tags: