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.

Shell access to minikube with a pointer to the /Users directory, which is mounted by default.
The /Users directory has been mounted by default by minikube.

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: {}
				
			
In this example, we start minikube absent any of the mount-related args. When the pod is created the /etc/minimounted/ directory will exist and it will be correctly mapped to the local directory.

 

We can see the output of this example in the following image:

Terminal / Oh My Zsh output for Solution #1: Mount a subdirectory in a pod in Minikube within user home. We 1.) restart minikube with no mount-related args, then 2.) we create the 'myns' namespace and run the nginx pod, finally 3.) we get terminal access to the pod and can see that the /etc/minimounted directory exists and contains the hello.txt file.
We mount a subdirectory within the pod's user home and display the contents of the hello.txt file.
Finally, we include the complete script for testing this solution here.

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.

Terminal shell output demonstrating that the /etc/minimounted/ directory is empty after applying the Kubernetes minikube configuration changes.
Command-line output of a container running in k8s minikube with the empty /etc/minimounted/ directory.

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.

Terminal output where we get command line access to the pod and then cat the hello.txt file and we can see that the contents of this file.
Example bash shell output for a container running in minikube with the /etc/minimounted/ directory which now contains the hello.txt file.

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.

Terminal output where 1.) minikube is restarted 2.) we delete a pod 3.) we mount the directory 4.) we run the pod 5.) we verify that we can now see the contents of the hello.txt file in the pod.
Mounting a directory in minikube without requiring a restart.

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:

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

thospfuller

I am a Web Design, Technical SEO, and WordPress Specialist based in Northern Virginia. I am interested in software development, content engineering, and business. I'm originally from Chicago, IL, and currently reside in Reston, VA.