NFS Docker Volumes: How to Create and Use

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

  • NFS server installed and configured on the remote machine.
  • Docker installed on the local machine.
  • A user account with root/sudo privileges.

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.

原创文章,作者:kepupublish,如若转载,请注明出处:https://blog.ytso.com/222363.html

(0)
上一篇 2022年1月6日
下一篇 2022年1月6日

相关推荐

发表回复

登录后才能评论