Set Up NFS Sever in Docker
This document will introduce to set up a NFS server in Docker container and use another Docker container to act as a Client to test this NFS server.
To set up a NFS server in your Host, you can check NFS server installed and configured.
Start Up NFS Server
Use docker image gists/nfs-server to start up a NFS server container.
In OSX, it's critical to use volume mount
and avoid using bind mount
as we mentioned above.
In Linux, it's okay to use either volume mount
or bind mount
.
docker run --rm -d \
--name nfs \
--privileged \
-p 2049:2049 \
-v /tmp/volume:/nfs-share \
-e NFS_DIR=/nfs-share \
-e NFS_OPTION="rw,fsid=0,async,no_subtree_check,no_auth_nlm,insecure,no_root_squash" \
gists/nfs-server
Before we use an old nfs server image itsthenetwork/nfs-server-alpine which was not maintained more than 4 years and not supported natively in platform linux/arm/v6
.
docker run -it --rm \
--name nfs \
--privileged \
-v /tmp/volume:/nfs-share \
-e SHARED_DIRECTORY=/nfs-share \
-p 2049:2049 \
itsthenetwork/nfs-server-alpine:latest
In OSX, due to the docker desktop itself is running in VM, it will cause some error like Operation not supported
when binding a local file folder via bind mount
even you set 777
mask on the folder. So it's recommended to use volume
to bind to /mnt
in Samba server in OSX.
Furthermore, the Samba server will log such message: error reading meta xattr: Not supported
.
Get the ip address of the NFS server, which will be used later to connect the NFS server when mounting in a Docker container client. If you only want to mount the NFS server from the host, you can just know the ip address of your host.
docker inspect \
-f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' \
nfs
Here the output is
172.17.0.2
Use NFS Client in Docker
Let's make use of a Docker container to act in a NFS client to access shared data in the running-up NFS server.
run container as root by option --privileged
or --cap-add SYS_ADMIN
when permissions denied inside the container:
docker run -it --rm --privileged busybox sh
Inside the container:
Due to the fsid=0 parameter set in the /etc/exports file, there's no need to specify the folder name when mounting from a client. For example, this works fine even though the folder being mounted and shared is /nfs-share:
# In the container
mkdir /mnt
# nfs v4
mount -v -o vers=4,loud 172.17.0.2:/ /mnt
# create a file to test
echo "some text here" > /mnt/file1.txt
Then go to the Host to list directory /data/volume/test
, where you will find the file1.txt
is sitting.
# In the host
cat /data/volume/test/file1.txt
Use NFS Client With Volume Mount in Docker
- Create a NFS volume in Docker
docker volume create --driver local \
--opt type=nfs \
--opt o=addr=172.17.0.2,nfsvers=4 \
--opt device=:/ \
nfs-volume
docker inspect nfs-volume
- Run the container with the created volume
nfs-volume
.
docker run -it --rm \
--privileged \
--name nfs-test \
-v nfs-volume:/mnt \
busybox \
sh
Alternative, you can use the combined one command which will create a volume nfsvolume
,
docker run -it --rm \
--privileged \
--name nfs-test \
--mount 'type=volume,source=nfsvolume,volume-driver=local,volume-opt=type=nfs,volume-opt=device=:/,"volume-opt=o=addr=172.17.0.2,rw,nfsvers=4,async",target=/mnt' \
busybox \
sh