- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to directly mount NFS share/volume in a container using Docker Compose v3?
Docker is a widely used tool for developing and managing containerized applications. It enables programmers to combine their applications with their dependencies into small, portable containers that are simple to set up and use on any platform. Directly mounting Network File System (NFS) shares or volumes inside a container using Docker Compose v3 is one practical Docker capability.
In this article, we will explore how to use Docker Compose v3 to directly mount an NFS share or volume in a container.
Method to directly mount NFS share/volume in the container using Docker Compose v3
Here are some important terms and facts that we need to understand and learn to directly mount NFS share/volume in a container using Docker Compose v3 −
Target Path − The target key is used to designate where the NFS share or volume should be mounted inside the container. Any directory path inside the container that you want the volume to be mounted to can be used as this.
Volume Type − To indicate the type of volume that is being mounted, use the type key. The type should be set to "volume" for NFS shares or volumes.
Source Path − The path of the NFS share or volume on the host machine is specified using the source key. Any directory path on the host system that leads to the NFS share or disc can be used here.
Volume Choices − The volume key can be used to specify additional volume options. The nocopy option is one of them; it can be set to true to stop the volume's contents from being copied into the container. This can be helpful for optimizing the final image's size and cutting down on the number of layers that are there.
We will now understand and demonstrate this better through an example.
Step 1 − Create a new directory for your project and navigate to it −
$ mkdir directoryname $ cd directoryname
Step 2 − Make a file called docker-compose.yml with the below content in this new directory −
version: "3" services: web-server: image: nginx:latest ports: - 80:80 volumes: - type: volume source: nfs-volume target: /nfs volume: nocopy: true volumes: nfs-volume: driver_opts: type: "nfs" o: "addr=10.40.0.199,nolock,soft,rw" device: ":/var/data"
This indicates the version of Docker Compose to use in the file, gives a service a name, a suitable image to use to define it, adds a volumes key under the service, and also specifies the volume's type, source path, and target path. Further, it also sets the nocopy option under the volume key to true to stop the volume's contents from being copied into the container.
Step 3 − Create a Dockerfile in the same directory with the following content −
FROM nginx:latest COPY . /usr/share/nginx/html EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]
In this example, we are mounting the NFS share or volume stored at /mnt/nfs/share on the host system to /app inside the container using the nginx:latest image. To prevent the volume's contents from being duplicated into the container, the nocopy option is specified as true.
Step 4 − Start the container with the mounted volume by using the docker-compose up command −
The output from the docker-compose up command should look similar to this −
[+] Running 2/2 - Network examp_default Created 0.9s - Container examp-web-1 Created 0.1s Attaching to examp-web-1 examp-web-1 | * Serving Flask app 'app' examp-web-1 | * Debug mode: on examp-web-1 | WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. examp-web-1 | * Running on all addresses (0.0.0.0) examp-web-1 | * Running on http://127.0.0.1:5000 examp-web-1 | * Running on http://172.19.0.2:5000 examp-web-1 | Press CTRL+C to quit examp-web-1 | * Restarting with stat examp-web-1 | * Debugger is active! examp-web-1 | * Debugger PIN: 630-981-535 examp-web-1 | 172.19.0.1 - - [09/Jan/2023 16:46:53] "GET / HTTP/1.1" 200 - examp-web-1 | 172.19.0.1 - - [09/Jan/2023 16:46:53] "GET /favicon.ico HTTP/1.1" 404 - Gracefully stopping... (press Ctrl+C again to force) [+] Running 1/1 - Container examp-web-1 Stopped 2.0s canceled PS C:\DikshaDen\docker-apps\examp> cd .. PS C:\DikshaDen\docker-apps> docker-compose up no configuration file provided: not found PS C:\DikshaDen\docker-apps> docker-compose up no configuration file provided: not found PS C:\DikshaDen\docker-apps> docker-compose up --build no configuration file provided: not found PS C:\DikshaDen\docker-apps> docker-compose up --build no configuration file provided: not found PS C:\DikshaDen\docker-apps> docker-compose build no configuration file provided: not found PS C:\DikshaDen\docker-apps> cd mount-nfs PS C:\DikshaDen\docker-apps\mount-nfs> docker-compose up --build [+] Running 7/7 - web-server Pulled 40.6s - 3f4ca61aafcd Pull complete 18.0s - 50c68654b16f Pull complete 19.3s - 3ed295c083ec Pull complete 19.4s - 40b838968eea Pull complete 19.6s - 88d3ab68332d Pull complete 19.7s - 5f63362a3fa3 Pull complete 19.8s time="2023-01-09T22:48:42+05:30" level=warning msg="Found orphan containers ([mount-nfs-web-1]) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up."
This output demonstrates that the host system's /mnt/nfs/share NFS share or volume has been mounted to the container's /app using the nginx:latest image. To stop the contents of the volume from being copied into the container, the nocopy option is set to true.
In this article, we explored how to directly mount Network File System (NFS) shares or volumes in a Docker container using Docker Compose v3. We discussed the various terminologies and facts involved, such as the volume type, source path, target path, and volume options. We also supplied an example code and output to show how the process works. You may successfully mount an NFS share or volume in a Docker container using Docker Compose v3 by following the instructions in this article. This will enable you to access stored data on an external NFS server or transfer files across containers.
- Related Articles
- Mounting a volume inside docker container
- Build a flask App using Docker Compose
- Using docker-compose With Private Repositories
- Equivalent of Docker option --config in docker-compose
- How to Use Multiple Databases with docker-compose?
- How to manage secret values with docker-compose v3.1?
- How to find Docker container processes?
- How to get a Docker Container IP address?
- How to backup and restore a Docker Container?
- How to run a command inside Docker Container?
- How to Debug a Node.js app in a Docker Container?
- How to fix Ctrl+C inside a Docker container?
- Creating a MySQL Docker Container
- How many CPUs does a docker container use?
- How to rebuild Docker container on file changes?