Tutorial: Easily Mount a Local Directory Into a Pod in minikube (2022)
The background to this problem is as follows: You need to mount a local directory into a pod in minikube. The reason a software engineer would need this is that pod storage is ephemeral and will be deleted when the pod is deleted. If we want the storage to survive a pod deletion or to be shared between pods, then we can mount a directory from the local filesystem. This could be a requirement, for example, when you have a database running in the pod but the storage is located on a local filesystem. Another example where this could be appropriate would be when a software developer keeps the project on their local drive but runs the application in a container.
In this article, we’ll cover several examples that demonstrate how to accomplish mounting a local directory in a pod running in minikube. We rely on minikube (v1.15.1) with kubectl (1.19.4) on Ubuntu (20.04.1 LTS) as well as on Mac OSX (10.13.6 High Sierra).
The k command, which appears below, is defined as follows:
alias k='kubectl'
Let’s take a look at three solutions that address this requirement.
Table of Contents
Solution #1: Mount a subdirectory within user's home (easiest)
The first solution, which is also the easiest solution in this article, was inspired by [1] (Akshay Sood, specifically) facilitates the same result as Solution #2 however without the requirement to pass the –mount and –mount-string args. The pod configuration file looks very close to the one used in Solution #2 as well — direct your attention to lines 14 (volumeMounts) and 17 (volumes).
This works because minikube mounts the /Users directory by default on Mac OSX, which is what we’re using for this specific solution.
Refer to the homedir package documentation as well as the for more information regarding the directory paths per specific supported operating systems (Apple OSX, Linux, and Microsoft Windows) [12 & 13]. I also have a simple script, which is written in Go, in GitHub, which displays the value of the user’s home directory.
In the image below we can see which directory has been mounted in minikube by invoking the df command with the h option, for displaying human-readable output, and the l option, which reveals information about locally-mounted filesystems.

Here’s the pod configuration:
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx-minimounted
name: nginx-minimounted
namespace: myns
spec:
containers:
- image: nginx
name: nginx-minimounted
resources: {}
volumeMounts:
- mountPath: /etc/minimounted
name: minimounted
volumes:
- name: minimounted
hostPath:
path: /Users/thospfuller/k8s-study/minimounted
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
We can see the output of this example in the following image:
minikube start --hyperv-virtual-switch "My Virtual Switch" --v=4
kubectl apply -f ./nginx-hp-minimounted.yaml
kubectl exec nginx-minimounted -n myns -it -- /bin/sh
ls -la /etc/minimounted
exit
Let’s take a look at Solution #2.
Solution #2: Mount a directory outside of user home
The second solution should be fairly straightforward however it’s not exactly given the pod configuration in the example included below. Solution #2 attempts to mount the /etc/minimount directory and since the /etc directory is not mounted by default, we need to add command line args when minikube is started to instruct minikube to mount a local directory to a directory in minikube and then when the pod is created it will mount that directory to a directory in the pod itself.
If we just follow an example for mounting a local directory in a pod in Kubernetes, when we have shell access to the pod and check the path, the directory will exist however it will be empty.
Reference [1] (Shahriar, specifically) explains the solution pretty closely however a fully working solution is not provided and we’ll deliver that here.
The following pod configuration file should work but, as we’ll see in a moment, it doesn’t.
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx-minimounted
name: nginx-minimounted
namespace: myns
spec:
volumes:
- name: minimounted
hostPath:
path: /minimounted/
type: DirectoryOrCreate
containers:
- image: nginx
name: nginx-minimounted
resources: {}
volumeMounts:
- mountPath: /etc/minimounted
name: minimounted
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
Here’s an example of what the minikube start script might look like — we need to change this in order to get this working.
minikube start --hyperv-virtual-switch "My Virtual Switch" --v=4
The following image demonstrates the problem inside the pod — note the /etc/minimounted/ directory is empty whereas we’re expecting the hello.txt file to be there.

In order to make this work, we need to start minikube with the following mount and mount-string options set:
minikube start --hyperv-virtual-switch "My Virtual Switch" --v=4 --mount --mount-string="/etc/minimount/:/minimounted/"
We have /etc/minimount/ mounted as /minimounted/ in minikube and when we run the nginx-minimounted pod the /minimounted/ directory will be mounted as /etc/minimounted/ in the pod; we can see the directory contents in the image below.

Let’s take a look at Solution #3.
Solution #3: Mount a directory other than user home without restart
It is possible to mount the directory without restarting minikube and we’ll demonstrate this here. Unfortunately, however, we’ll need to recreate the pod otherwise the pod’s /etc/minimounted/ directory will still be empty. If you know how to achieve this same result without restarting the pod, please include the details in the comments.
Regarding the following image: first, we restart minikube and can see that the /etc/minikube/ directory is empty.
Next, we mount the directory. Keep in mind that this command must be executed as a background process or we’ll need to open another terminal.
Finally, we can see the contents of the /etc/minimounted/hello.txt file.

For convenience, we include the entire script here:
minikube stop && minikube start
kubectl apply -f ./nginx-minimounted.yaml
kubectl exec nginx-minimounted -n myns -it -- /bin/sh
ls -la /etc/minimounted
exit
kubectl delete pods nginx-minimounted -n myns
minikube mount /Users/thospfuller/k8s-study/minimounted:/minimounted/ &
kubectl apply -f ./nginx-minimounted.yaml
kubectl exec nginx-minimounted -n myns -it -- /bin/sh
cat /etc/minimounted/hello.txt
And that’s it for Solution #3.
Mount a Local Directory Into a Pod in minikube: Article Conclusion
If you liked this article you may also like the following articles, also written by yours truly:
- Easily Install the Metrics Server on Minikube in 5 Steps
- Answers to Five Kubernetes CKAD Practice Questions (2020)
Please add comments below if you find an error with anything written here or have a question about this document.
Hat Tip
I’d like to express my thanks to the following colleagues who assisted with the review and/or editing of this article:
A poem about mounting a local directory into a pod in minikube
I’ve been working with ChatGPT lately and while refreshing this article I instructed it as follows: write a poem about mounting a local directory into a pod in minikube as if the author was Emily Dickinson — for fun I’ve included the result here.
A local directory, now mounted in a pod, In minikube, where Kubernetes doth reign, An act of power, by a modern god, Whose tools and code can make our apps sustain.
Oh, what a feat to see this world collide, Of code and data, merged in harmony, A local path now in a container abide, A work of genius, born of technology.
And yet, amidst this digital terrain, There lies a truth that poets oft confess, That nature’s laws still hold an ancient reign, And all that’s born of code must still address.
For like a garden that we tend and grow, Our apps must still find roots in what we know.
"Mount A Local Directory Into a Pod in minikube" Article References
- Mount local directory into pod in minikube
- MountVolume.SetUp failed for volume “mongo” : hostPath type check failed: /mongo/data is not a directory
- hostpath : MountVolume.SetUp failed for volume “test-volume” : hostPath type check failed: /data is not a directory
- Invalid Error: .container.volumes: hostPath type check failed: “not a directory”
- Kubernetes Volumes
- Mounting filesystems in minikube
- Configure a Pod to Use a Volume for Storage
- Configure a Pod to Use a PersistentVolume for Storage
- Intro to Windows support in Kubernetes
- juliohm1978 / kubernetes-cifs-volumedriver
- kubernetes / examples (staging volumes NFS)
- Kubernetes DefaultMountDir (constants in Golang for Darwin, Linux, and Windows) — the link is no longer available.
- GoDoc package homedir
- thospfuller
- December 9, 2020
- 6:10 pm
You must log in to post a comment.