Mounting an NFS share to a CentOS Docker container
Note: a much better way to achieve the same result is to create a local image, based on e.g. the latest CentOS, with nfs-utils already installed.
This example will cover mounting an NFS share to a Docker container that is using a CentOS 7 image. Please note that this example will cover mounting the NFS share without the use of systemd, meaning that the mounting will have to be done with the nolock option.
The mounting doesn’t require anything special from the Dockerfile, the installation of the nfs-utils package will be done after running the container.
Firstly build the image from your Dockefile.
docker build -t image .
The -t option defines the tag for the image, in this case “image”. More information about options can be found from the docker build documentation.
Once the image is built a Docker container can be created from it with the docker run command.
docker run -t -d --privileged=true --name container image
The command runs the container in detached mode, meaning that once you are done with it you need to close and remove it manually. Privileged is required here as the mounting operation requires privileges. The container name is defined after the name tag.
In order to mount the NFS drive the nfs-utils need to be installed, which can be done with the following command.
sudo docker exec -it container yum install nfs-utils
Here the -it options allows for interactive connection, as defined in the docker exec documentation. The -i option is the shorthand for interactive, while the -t allows for pseudo-TTY. These two can be combined, as done in the example. Without the interactive options, the installation cannot be done as it requires user input.
Once the installation is completed the mounting of the share can be done in the following way.
sudo docker exec --privileged=true container mount -t nfs -o nolock ip:/path mount/point
If you know a better way of achieving the same result, especially without using the nolock option, please let me know!