Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mounting nfs shares inside docker container

Does anyone know how to mount nfs share inside docker container with centos base image? I've tried this command:

mount server:/dir /mount/point 

and got the next error:

mount.nfs: rpc.statd is not running but is required for remote locking.
mount.nfs: Either use '-o nolock' to keep locks local, or start statd.
mount.nfs: an incorrect mount option was specified

when I try to use it with -o nolock option, the error is:

mount.nfs: Operation not permitted 
like image 203
Anatoli Avatar asked Oct 07 '16 16:10

Anatoli


People also ask

Can I mount NFS share?

With NFS, you can mount remote directories on your system and work with the remote files as if they were local files. On Linux and UNIX operating systems, you can use the mount command to mount a shared NFS directory on a particular mount point in the local directory tree.


2 Answers

Starting from docker 17.06, you can mount NFS shares to the container directly when you run it, without the need of extra capabilities

export NFS_VOL_NAME=mynfs export NFS_LOCAL_MNT=/mnt/mynfs export NFS_SERVER=my.nfs.server.com export NFS_SHARE=/my/server/path export NFS_OPTS=vers=4,soft  docker run --mount \   "src=$NFS_VOL_NAME,dst=$NFS_LOCAL_MNT,volume-opt=device=:$NFS_SHARE,\"volume-opt=o=addr=$NFS_SERVER,$NFS_OPTS\",type=volume,volume-driver=local,volume-opt=type=nfs" \   busybox ls $NFS_LOCAL_MNT 

Alternatively, you can create the volume before the container:

docker volume create \   --driver local \   --opt type=nfs \   --opt o=addr=$NFS_SERVER,$NFS_OPTS \   --opt device=:$NFS_SHARE \   $NFS_VOL_NAME  docker run --rm -v $NFS_VOL_NAME:$NFS_LOCAL_MNT busybox ls $NFS_LOCAL_MNT 
  • Got the hint from https://github.com/moby/moby/issues/28809
  • official docs from docker: https://docs.docker.com/storage/volumes/#create-a-service-which-creates-an-nfs-volume
like image 179
ThiagoAlves Avatar answered Sep 18 '22 20:09

ThiagoAlves


For using mount, you'll need the CAP_SYS_ADMIN capability, which is dropped by Docker when creating the container.

There are several solutions for this:

  1. Start the container with the --cap-add sys_admin flag. This causes Docker to retain the CAP_SYS_ADMIN capability, which should allow you to mount a NFS share from within the container. This might be a security issue; do not do this in untrusted containers. [A previous version of this answer suggested using the --privileged=true to retain all capabilities, thanks to @earcam for the suggestion to use --cap-add instead].
  2. Mount the NFS share on the host and pass it into the container as a host volume:

    you@host > mount server:/dir /path/to/mount/point you@host > docker run -v /path/to/mount/point:/path/to/mount/point 
  3. Use a Docker volume plugin (like the Netshare plugin) to directly mount the NFS share as a container volume:

    you@host > docker run \   --volume-driver=nfs \   -v server/dir:/path/to/mount/point \   centos 
like image 26
helmbert Avatar answered Sep 19 '22 20:09

helmbert