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 server installed and configured on the remote machine.
- Docker installed on the local machine.
- A user account with root/sudo privileges.
Note: Bare Metal Cloud offers scalable distributed network file storage for latency-sensitive and high-throughput workloads. Check out our video tutorial that explains how to deploy an NFS server with Bare Metal Cloud.
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.
--driveroption defines the
localvolume driver, which accepts options similar to the
mountcommand in Linux.
--optoption 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
The successfully executed command outputs the name of the volume.
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.
Inspect the volume with the
docker volume inspect [volume-name]
The output shows the volume configuration.
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
sudo apt install nfs-common
Confirm that you want to install the package and wait for the installation to finish.
Note: If you use YUM or RPM for package management, the DNS client package is called
Use the docker run command to start the container. Specify the NFS volume and the mount point in the
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.
Confirm that the volume was successfully mounted by using the
docker inspect command.
docker inspect [container-name]
Mounts section of the output contains the volumes mounted into the container.
Enter the container environment bash shell with
docker exec -it [container-name] /bin/bash
List the contents of the
The output shows the files hosted in the
/mnt/nfsdir directory on the server.
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
On the server, navigate to the directory you shared and list its contents. The file created in the Docker container appears.
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.
Define the NFS volume in the
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]"
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.
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.