Learn How to Mount a Local Drive in a Pod in Minikube (2021)
Tags: tutorial, kubernetes, docker, containerization
Published: 2020-12-10 03:36:55

In the following article, we're using Minikube (v1.14.2) with Kubectl (1.19) running on Ubuntu 20.04.1 LTS.

The Problem
-----------

The background to this problem is as follows:

You're using Minikube and you want to mount a local drive in a pod and that's it.

This should be fairly straightforward however it's *not exactly*. We'll cover a fully working example here.

The real issue is that we need to mount a local directory to a directory in Minikube and *then* 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 example is not provided.

The following pod configuration file should work but, as we'll see in a moment, it doesn't.

{% gist https://gist.github.com/thospfuller/b36e4b4b9dd2bb41e3a4a00f4cfd8c15 %}

Here's an example of what the minikube start script might look like --- we need to change this in order to fix this issue.

```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.

[![](https://i0.wp.com/thospfuller.com/wp-content/uploads/2020/12/minikube-nginx-minimounted-directory-empty.png?resize=813%2C229&ssl=1)](https://i0.wp.com/thospfuller.com/wp-content/uploads/2020/12/minikube-nginx-minimounted-directory-empty.png?ssl=1)

Now let's take a look at a solution.

The Solution
------------

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 /minimount/ in Minikube and when we run the nginx-minimounted pod the /minimount/ directory will be mounted as /etc/minimounted/ in the pod; we can see the directory contents in the image below.

[![](https://i2.wp.com/thospfuller.com/wp-content/uploads/2020/12/minikube-etc-minimounted-hello-txt.png?resize=811%2C137&ssl=1)](https://i2.wp.com/thospfuller.com/wp-content/uploads/2020/12/minikube-etc-minimounted-hello-txt.png?ssl=1)

And that's it for this article.

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](https://thospfuller.com/2020/11/29/easily-install-the-metrics-server-on-minikube-in-5-steps/)
-   [Answers to Five Kubernetes CKAD Practice Questions (2020)](https://thospfuller.com/2020/11/09/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
-------

[Q McCallum](https://qethanm.cc/news/)

References
----------

1.  [Mount local directory into pod in minikube](https://stackoverflow.com/questions/48534980/mount-local-directory-into-pod-in-minikube)
2.  [MountVolume.SetUp failed for volume "mongo" : hostPath type check failed: /mongo/data is not a directory](https://stackoverflow.com/questions/48927312/mountvolume-setup-failed-for-volume-mongo-hostpath-type-check-failed-mongo)
3.  [hostpath : MountVolume.SetUp failed for volume "test-volume" : hostPath type check failed: /data is not a directory](https://github.com/kubernetes/kubernetes/issues/90263)
4.  [Invalid Error: .container.volumes: hostPath type check failed: "not a directory"](https://github.com/kubernetes/kubernetes/issues/83125)