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.
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
- How to disable core file dumps in docker container
- How to Dockerfile FROM another Dockerfile?
- How to dockerize a Maven project? How many ways to accomplish it?
- How to download file from docker container?
- How to easily install and uninstall Docker on macOS
- How to edit Docker container files from the host?
- How to edit files in stopped/not starting docker container
- How to enable Network Policies in Docker for Mac with Kubernetes

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.