How to mount a NFS file system in a docker agent

Issue

  • We need to mount a NFS filesystem inside our CJE Agent Docker images.

Environment

Resolution

Mount the filesystem inside the Docker image

In order to mount an NFS filesystem, you will need a Customize entrypoint on CJE Agent Docker images, this allows the Docker Agent to execute custom commands, so you should create a Dockerfile like the following

FROM openjdk

RUN apt-get -y update &&\
	apt-get -y install nfs-common &&\
	mkdir /mnt/cache &&\
	chmod ugo+rwx /mnt/cache

ADD entrypoint.sh .
RUN chmod ugo+rx /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]

You will need a custom entrypoint like the following that shows the environment variables, start the rpcbind service, mount the NFS filesystem, and execute the commands passed from CJE.

#!/bin/bash

SERVER=${SERVER:-"nfs-server.example.com"}
SHARE=${SHARE:-"/var/nfsshare"}
FOLDER=${FOLDER:-""}
MOUNT_POINT=${MOUNT_POINT:-"/mnt/cache"}
MOUNT_OPS=${MOUNT_OPS:-"-o vers=3"}

if [ $# -gt 0 ]
then
  # first argument is the custom docker command shell provided by plugin
  # second argument is the java command line generated by the plugin (passed as a single arg)
  shift
  export
  /sbin/rpcbind -w &
  mount -t nfs ${MOUNT_OPS} ${SERVER}:${SHARE}/${FOLDER} ${MOUNT_POINT}
  exec /bin/sh -c "$@"
fi

The Docker template has to be a privileged container to be able to mount the NFS filesystem. The example entrypoint allow you to pass a parameter to the Docker Image to change the behavior, these are the possible parameters:

  • SERVER: IP/DNS of the NFS server.
  • SHARE: Name of the export in the NFS server, you can use showmount --exports NFS_SERVER to see the available exports.
  • FOLDER: Optional, folder inside the NFS export that you want to mount as the root folder.
  • MOUNT_POINT: folder where you want to mount the NFS filesystem inside the Docker Container, The folder should exists.
  • MOUNT_OPS: additional options to the mount command (e.g. the NFS version -o vers=3).

Use an NFS Docker Volume

Alternatively, you can create an NFS docker volume with the following command and use it on your container without any modification. This mode does not required a privileged container.

sudo docker volume create --driver local --opt type=nfs --opt o="vers=3,addr=nfs-server.example.com,rw" --opt device=":/var/nfsshare/" test-nfs-vol

To use this volume, you have to add the parameter volume with the value NFS_VOLUME_NAME:CONTAINER_PATH in the example it will be test-nfs-vol:/mnt/cache

Tested product/plugin versions

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.