Docker
NFS
Containerization
Docker Compose
Volume Mounting

How to directly mount NFS share/volume in container using docker compose v3

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Docker Compose is a highly powerful tool for defining and managing multi-container Docker applications. It allows users to specify services, networks, and volumes in a simple YAML file. A common use case for Docker Compose is handling file system mounting, especially when it comes to Network File System (NFS). This article will guide you through the process of mounting an NFS share or volume directly into a Docker container using Docker Compose version 3.

Understanding NFS

Network File System (NFS) is a distributed file system protocol originally developed by Sun Microsystems. It allows a user on a client computer to access files over a network as if they were on the local storage, providing shared access to files, directories, and file systems over a network.

Prerequisites

Before you begin, ensure you have the following prerequisites:

  • A working Docker installation.
  • Docker Compose version 3 or higher.
  • An accessible NFS server with the necessary NFS share/export configured.
  • Proper access permissions from the client to the NFS share.

Mounting NFS in Docker Compose v3

1. NFS Setup

Ensure that your NFS server is running and that the export is accessible from the Docker host. An example configuration for an NFS export (`/etc/exports` on the NFS server) can look like this:

  • "80:80"
  • type: volume
  • volumes: The `volumes` section defines a volume called `nfs-volume` with a `local` driver.
  • driver_opts: The `driver_opts` settings specify:
    • type: The type of the driver, which is `nfs` in this case.
    • o: Mount options, which include the NFS server address (`addr`) and `rw` for read-write access.
    • device: The path to the NFS export, prefixed with a colon.
  • services: The `app` service uses the `nfs-volume` to mount the NFS share at `/usr/share/nginx/html`.
  • Permission Denied: Ensure the NFS share is exported with sufficient permissions and that the Docker host IP is allowed access.
  • NFS Version Support: Your NFS server might support different versions. Validate the client and server are compatible.
  • Network Configuration: Double-check the firewall settings and network configurations to ensure seamless communication between the Docker host and NFS server.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.