NFS Docker Volumes: How to Create and Use

December 15, 2021

Introduction

Docker volumes are the preferred mechanism for setting up persistent storage for your Docker containers. Volumes are existing directories on the host filesystem mounted inside a container. They can be accessed both from the container and the host system.

Docker also allows users to mount directories shared over the NFS remote file-sharing system. The volumes created for this purpose use Docker's own NFS driver, eliminating the need to mount the NFS directory on the host system.

This tutorial will show you how to create and use NFS Docker Volumes.

NFS Docker Volumes: How to Create and Use

Prerequisites

Create NFS Docker Volume

The simplest way to create and manage Docker volumes is using the docker volume command and its subcommands.

The syntax for creating an NFS Docker volume includes two options.

  1. The --driver option defines the local volume driver, which accepts options similar to the mount command in Linux.
  2. The --opt option is called multiple times to provide further details about the volume.

The details include:

  • The volume type.
  • The write mode.
  • The IP or web address of the remote NFS server.
  • The path to the shared directory on the server.
docker volume create --driver local \
  --opt type=nfs \
  --opt o=addr=[ip-address],rw \
  --opt device=:[path-to-directory] \
  [volume-name]

The example below illustrates creating an NFS Docker volume named nfs-volume. The volume contains the /mnt/nfsdir directory located on the server, with the rw (read/write) permission. The IP address of the server is 10.240.12.70.

The successfully executed command outputs the name of the volume.

Creating an NFS volume in Docker.

Note: Before you create an NFS volume, make sure the IP address of your Docker host system is listed in the /etc/exports file on the server.

List the available Docker volumes.

docker volume ls

The output lists the volume you created.

Listing available Docker volumes.

Inspect the volume with the inspect subcommand.

docker volume inspect [volume-name]

The output shows the volume configuration.

Inspecting a Docker volume.

Mount NFS In a Container

To mount the NFS volume into a container, install the nfs-common package on the host system.

Start by updating the repositories.

sudo apt update

Use APT to install the nfs-common package.

sudo apt install nfs-common

Confirm that you want to install the package and wait for the installation to finish.

Installing the NFS client package with APT.

Note: If you use YUM or RPM for package management, the DNS client package is called nfs-utils.

Use the docker run command to start the container. Specify the NFS volume and the mount point in the --mount section.

docker run -d -it \
  --name [container-name] \
  --mount source=[volume-name],target=[mount-point]\
  [image-name]

The example below mounts the NFS volume named nfs-volume to the /mnt directory in the container.

Mounting a Docker volume in a Docker container.

Confirm that the volume was successfully mounted by using the docker inspect command.

docker inspect [container-name]

The Mounts section of the output contains the volumes mounted into the container.

The Mounts section of the output of the docker inspect command.

Enter the container environment bash shell with docker exec:

docker exec -it [container-name] /bin/bash
Using the docker exec command to access the shell of a Docker container.

List the contents of the /mnt directory.

ls /mnt

The output shows the files hosted in the /mnt/nfsdir directory on the server.

Checking the contents of the mnt directory in the container.

If you create a file inside the Docker container, it will also be accessible in the original directory on the server. To test, use the touch command to create an empty file in the /mnt directory.

touch /mnt/docker1.txt

On the server, navigate to the directory you shared and list its contents. The file created in the Docker container appears.

Checking the contents of the directory shared with Docker over NFS.

Mounting NFS Volumes with Docker Compose

If you use Docker Compose to manage your containers, mount the NFS volume by defining it in the YML file.

Create the YML file.

nano docker-compose.yml

Define the NFS volume in the volumes section.

version: "3.2"

services:
  [service-name]:
    image: [docker-image]
    ports:
      - "[port]:[port]"

    volumes:
      - type: volume
        source: [volume-name]
        target: /nfs
        volume:
          nocopy: true
volumes:
  [volume-name]:
    driver_opts:
      type: "nfs"
      o: "addr=[ip-address],nolock,soft,rw"
      device: ":[path-to-directory]"

Note: The nolock and soft options ensure that Docker does not freeze if the connection to the NFS server is lost.

Create and start the container with the docker-compose up command.

Conclusion

The article explained how to create and mount the Docker volumes that contain directories shared over NFS. The methods covered in the article involve mounting volumes using the command line or Docker Compose.

If you are interested in Docker, read how hosting containers on Bare Metal Cloud can help you optimize docker performance.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
Install NFS Server on Ubuntu
November 10, 2020

NFS utilizes standard client/server architecture, supporting sharing between Linux machines, regardless of their distribution. In this tutorial, you will learn how to install and configure the NFS server and clients on Ubuntu.
Read more
How to Share Data Between Docker Containers
March 26, 2019

As you start using Docker, you will face situations where you need to know how to share data between containers. This simple tutorial will show you how to set up container sharing and create read-only volumes.
Read more
How To Remove Docker Images, Containers, Networks & Volumes
February 7, 2019

Learn how to organize a Docker environment by removing Docker images, containers, volumes, and networks. Using these commands makes Docker container management fast and simple.
Read more
How to Set Up and Use Private Docker Registry
December 5, 2019

Creating a private Docker registry is a practical way of distributing resources and sharing Docker images among containers. Learn how to set up and configure a private Docker registry.
Read more
  • © 2022 Copyright phoenixNAP | Global IT Services. All Rights Reserved.