How to Mount an NFS Share Into a Container


This post will show you how to mount an NFS share into a container. We will explore mounting an NFS to the container via Portainer and Docker Compose. It is truly super easy to do!

Off course! You can mount your NFS to the host, and then mount the host’s NFS folder into the container. But, this method can be avoided. Mounting the NFS directly to the container avoids a manual NFS mount on the host and/or using the /etc/fstab.

Adding an NFS drive requires you to create a Volume. Volumes simply reference files and folders on the host, or (in this case) a Network File System (or NFS for short).

Create an NFS Volume Using Portainer

Click on the ‘Volumes’ menu item and the ‘Add Volume’ button.

Portainer, Volumes, Add Volume
Portainer, Volumes, Add Volume

Next, you need to specify the following options:

  • The name of the NFS volume you want to use in the container.
  • The driver configuration must be set to ‘local’.
  • Tick the ‘NFS’ volume radio button.

In the NFS settings section, set the:

  • address of the NFS server you wish to use. You can use a DNS name or IP address.
  • NFS version can be set to ‘NFS’ or ‘NFS4’. When in doubt use ‘NFS’.
  • Mount point on your NFS server.
  • Set the various option that applies to your implementation.

Lastly, click the ‘Create the volume’ button for the NFS volume to be added.

Portainer, Volumes, Create Volume
Portainer, Volumes, Create Volume

You can now reference your NFS volume using the volume name you specified. See the example in the Docker Compose section below for an example.

Create an NFS Volume Using Docker Compose

You can also use Docker Compose to specify an NFS volume to mount into the container. The example below configures a MySQL container and the mounts the ‘nfs-mysql-vol’ to the MySQL container under ‘/var/lib/mysql’.

The Volumes section at the bottom specifies all the properties that were described above. Note that the compose file below has been tested using Portainer with Docker Compose version 2 and you can change this if needed.

version: '2'

    container_name: mysql
      MYSQL_ROOT_PASSWORD: somePassword
      - 3306:3306
      # The volume you have created in the 'volumes' section further below.
      - nfs-mysql-vol:/var/lib/mysql 
    image: 'mysql:latest'
    command: --default-authentication-plugin=mysql_native_password
    restart: always

# The volume that you want to define for use int he containers.    
    driver: local # Must be set to avoid issues.
      type: "nfs"
      o: "addr=my.nfs.server,rsize=65536,wsize=65536,timeo=14,tcp,rw,noatime"
      device: ":/volume1/mysql/"

NFS Volume Options

Additionally, you have various options available to help you configure the NFS volume for your container.

For example: “addr=my.nfs.server,rsize=65536,wsize=65536,timeo=14,tcp,rw,noatime”:

  • You can set the read and write size of your NFS to help increase the performance.
  • Also, you can also set the protocol to TCP, which allows only lost frames to be resent.

Finally, you can also set the user and group ID by adding: uid=1024,gid=100. This is quite useful for setting up Synology NAS/NextCloud servers!

Hi, my name is Anto! I am a cloud computing hobbyist! Give me anything to do with the cloud, and I am interested. I work for a Cloud computing company by day and as a Cloud computing hobbyist by night! My projects use PHP, NodeJs, Ubuntu, MySQL and of course Amazon Web Services. Hopefully, my blog aids your cloud journey! Feel free to post a comment and share your thoughts.

Leave a Reply

Your email address will not be published. Required fields are marked *