Mount local directory into pod in minikube
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Mounting a local directory into a pod in Minikube is an efficient way to make local files accessible to applications running in a Kubernetes pod. This is particularly helpful for local development and testing when you need to provide your applications with easy access to configuration files, code, or other data located on your host machine. In this article, we'll delve into the process of mounting local directories into pods with Minikube, and include both technical explanations and practical examples to guide you through the steps.
Introduction to Minikube
Minikube is a tool that enables you to run a Kubernetes cluster locally on your machine. It creates a virtual machine (VM) and deploys a simple one-node Kubernetes cluster. Minikube is widely used for local development and learning Kubernetes concepts without the need for a multi-node cluster.
Why Mount Directories?
There are several reasons to mount a local directory into a pod:
- Code Synchronization: While developing applications, you may want to automatically sync changes in the source code on your local development machine with the application running in your pod.
- Configuration Management: Often, you may have configuration files or environment variables that should be shared between your development environment and your applications.
- Data Access: You may need to test your application with local data files without copying them into Docker images or remote storage.
Pre-requisites
Before proceeding, ensure the following is set up:
- Minikube: Installed and running on your system.
- kubectl: Command-line tool to manage Kubernetes clusters.
- Virtualization Software: Such as VirtualBox or Docker Desktop, to run Minikube.
Configuring Minikube
Typically, to mount a local directory into a pod running in Minikube, you perform two primary steps: enabling the Minikube mount feature and configuring your pod to use the mounted directory.
Step 1: Start Minikube
Start Minikube with the desired driver and resource settings:
Step 2: Enable the Mount Feature
Minikube allows you to mount directories from your local machine into the Minikube VM. Use the minikube mount command to achieve this:
/path/to/local/dir: The local directory you wish to mount./path/to/mount/in/minikube: The target directory path in the Minikube environment.
Keep this terminal window open, as closing it terminates the mount.
Step 3: Configure a Pod to Use Mounted Directory
Create a Kubernetes manifest file (pod.yaml) that specifies a Kubernetes pod with a volume pointing to the mounted directory:
mountPath: Where your application accesses the mounted data within the container.path: Same as/path/to/mount/in/minikubedefined earlier in theminikube mountcommand.
Deploy the pod using:
Validating the Setup
Once the pod is running, you can validate our setup by executing commands within the pod. Access the pod using:
Inside the pod, navigate to /data, where you should now see the contents of /path/to/local/dir from your local machine:
Table Summary
| Step | Command/Description |
| 1. Start Minikube | minikube start --driver=docker |
| 2. Enable Mount | minikube mount /path/to/local/dir:/path/mount/in/minikube |
| 3. Pod Config | Create pod.yaml with volume & mountPath |
| 4. Deploy Pod | kubectl apply -f pod.yaml |
| 5. Validate | kubectl exec -it demo-pod -- sh
Navigate to /data |
Additional Considerations
- Persistent Changes: Minikube's mount is not persistent across reboots. You need to re-run the
minikube mountcommand whenever you restart Minikube. - Security: Be cautious with what directories you expose to your pods, especially when dealing with sensitive data.
- Performance: Mounting directories may have performance implications depending on the number and size of files.
Conclusion
Mounting a local directory into a Minikube pod is a straightforward yet powerful technique for local development and testing. By following the steps outlined in this guide, you can ensure seamless access to local files within your Kubernetes applications, enhancing your development workflow significantly.
Related reading
- Mounting kubernetes volume with User permission
- Mounting NFS Persistent Volumes with authentication
- MountVolume.SetUp failed for volume kube-api-access-cvwdt object default/kube-root-ca.crt not registered
- MountVolume.SetUp failed for volume kube-api-access-fcz9j object default/kube-root-ca.crt not registered
- MountVolume.SetUp failed for volume nfs mount failed exit status 32
- MountVolume.Setup failed for volume xxx couldn't get secret
- mTLS between two kubernetes clusters
- multiple app nodes how to expose jmx in 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.