73

I am running minikube v0.24.1. In this minikube, I will create a Pod for my nginx application. And also I want to pass data from my local directory.

That means I want to mount my local $HOME/go/src/github.com/nginx into my Pod

How can I do this?

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - image: nginx:0.1
    name: nginx
    volumeMounts:
    - mountPath: /data
      name: volume
  volumes:
  - name: volume
    hostPath:
      path: /data
Abu Hanifa
  • 2,857
  • 2
  • 22
  • 38
  • 1
    Check this simple answer, [kubernetes share a directory from your local system to kubernetes container](https://stackoverflow.com/questions/52037957/kubernetes-share-a-directory-from-your-local-system-to-kubernetes-container) – sngjuk Mar 17 '20 at 10:42

9 Answers9

66

You can't mount your local directory into your Pod directly.

First, you need to mount your directory $HOME/go/src/github.com/nginx into your minikube.

$ minikube start --mount-string="$HOME/go/src/github.com/nginx:/data" --mount

Then If you mount /data into your Pod using hostPath, you will get you local directory data into Pod.

There is another way

Host's $HOME directory gets mounted into minikube's /hosthome directory. Here you will get your data

$ ls -la /hosthome/go/src/github.com/nginx

So to mount this directory, you can change your Pod's hostPath

hostPath:
  path: /hosthome/go/src/github.com/nginx
gog
  • 10,367
  • 2
  • 24
  • 38
Shahriar
  • 13,460
  • 8
  • 78
  • 95
  • 14
    I don't have /hosthome in the pod – aclowkay Jul 09 '18 at 08:22
  • 5
    @aclokay Mountpoint varies depending on VM driver, as per [this](https://kubernetes.io/docs/setup/minikube/#mounted-host-folders) table. Note that it is generally not available for the KVM driver. There also appears to be a currently unresolved bug when using VirtualBox in version 0.28, see here. – Fredrik Jonsén Aug 12 '18 at 22:10
  • I tried it, the data did transfer in minikube's folder but not inside Pods. What am I doing wrong? – Volatil3 Jan 04 '19 at 16:28
  • 1
    @Volatil3, yes, first part mounts local volume into minikube, then you need to add hostPath into pod. – Shahriar Jan 04 '19 at 16:57
  • @aerokite can you please address the `--mount` answer below? – Mugen Jan 08 '19 at 13:16
  • 3
    It seems the mount feature is shaky: https://github.com/kubernetes/minikube/issues/3048 – ebaklund Feb 14 '19 at 14:30
  • 1
    For `minikube start` the syntax from this answer is no correct (at least for version v1.0.0). Correct syntax (from @K.S answer below) is `minikube start --mount-string ${HOME}/go/src/github.com/nginx:/data --mount` – Mcmil Apr 25 '19 at 15:46
  • I agree with @ebaklund and i found supporting link as well https://github.com/kubernetes/kubernetes/issues/83125 – SRJ Jan 15 '20 at 15:32
  • 3
    I found I only got empty directories on OSX until I switched from --vm-driver=docker to virtualbox, and finally it was happy – David Plumpton Feb 20 '20 at 03:57
  • The hosthome didn't work but I liked the idea. To get this to work I combined the 2 `minikube start --mount-string="$HOME:/hosthome" --mount` – Luke Jun 22 '23 at 04:45
24

I tried out aerokite's solution, but found out that I had to pass --mount as well as --mount-string "local-path:minikube-path" to mount a directory in minikube.

minikube start --mount-string ${HOME}/go/src/github.com/nginx:/data --mount. Spent some time figuring this out.

K.S.
  • 381
  • 3
  • 6
  • In minikube 0.35 this is not work. My home dir laways mounted. But i see massage Creating mount C:\\Users\\grigo\\IdeaProjects\\bigkube\\target\\scala-2.11:/data/jars. – Grigoriev Nick Mar 13 '19 at 15:12
  • 2
    @SanghyunLee A lot of googling, and then finally just looking at `minikube start --help` and from there found the `--mount` parameter's description: "This will start the mount daemon and automatically mount files into minikube" – K.S. Jan 16 '20 at 08:17
  • This unfortunately doesn't support `minikube mount`'s `--gid` & `--uid` options. Also, beware, I've read on their repo issues that there it can have buggy behaviours like empty mounted folder (especially with hypervisor rather than using virtualbox). – Kamafeather Nov 22 '20 at 02:15
16

I found a way.

This way you can directly mount directory to container. You do not have to mount your directory to minikube first.

We can specify the directory we want to add into container by using hostPath in volumes

  volumeMounts:
    - name: crypto-config
      mountPath: <PATH IN CONTAINER>
    - name: channel-artifacts
      mountPath: /opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
    - name: chaincode
      mountPath: /opt/gopath/src/github.com/chaincode
  volumes:
    - name: crypto-config
      hostPath:
        path: <YOUR LOCAL DIR PATH>
    - name: channel-artifacts
      hostPath:
        path: /Users/akshaysood/Blockchain/Kubernetes/Fabric/network/channel-artifacts
    - name: chaincode
      hostPath:
        path: /Users/akshaysood/Blockchain/Kubernetes/Fabric/network/chaincode
Akshay Sood
  • 6,366
  • 10
  • 36
  • 59
  • 9
    Can you share the complete yaml where you specified volumes? Or elaborate a bit about where you did the change. It is not clear to me where you may do so. I tried and was informed that volumes was unknown that context I tried. – Thomas Sundberg Sep 06 '18 at 07:58
  • 6
    This works because by default minikube will mount /Users of host machine into /Users inside VM in macos. For linux it will be /home -> /hosthome. For windows C://Users -> /c/Users. For ref - https://kubernetes.io/docs/setup/minikube/#mounted-host-folders – devansvd May 28 '19 at 10:12
  • https://v1-18.docs.kubernetes.io/docs/setup/learning-environment/minikube/#mounted-host-folders – Kamafeather Nov 22 '20 at 02:21
  • no longer mounts the /Users by default. I had to mount it manually with minikube start --mount-string "/Users:/Users" --mount – Tudor Jun 19 '23 at 08:20
11

DefaultMountDir

Minikube already mounts by default home directory to VM:

  • on Mac it mounts dir of all users - /Users
  • on Linux and Windows just the home of current user - homedir.HomeDir()

You can see how it does this, if you browse through Minikube sources.
Here is the search for the moment, but result might change over time:
https://github.com/kubernetes/minikube/search?q=DefaultMountDir&unscoped_q=DefaultMountDir

The definition of the HomeDir() is: https://godoc.org/k8s.io/client-go/util/homedir

You can always do minikube ssh into the Minikube VM and explore it:

$ df -hl
Filesystem      Size  Used Avail Use% Mounted on
...
/Users          466G  442G   25G  95% /Users

As Minikube is a single node Kubernetes cluster, you can then mount /Users/... inside your pods.

Recommended way

minikube mount /path/to/dir/to/mount:/vm-mount-path is the recommended way to mount directories into minikube so that they can be used in your local Kubernetes cluster. The command works on all supported platforms.

See documentation and example: https://minikube.sigs.k8s.io/docs/tasks/mount/

Ivan
  • 9,089
  • 4
  • 61
  • 74
  • the internal container complains about permissions once I mount the folder, is there a common workaround rather than chmod 777ing it? – Ryu S. Aug 08 '23 at 23:30
8

After struggling to do this for an hour & reviewing these answers, I got something slightly similar to work:

First off, I am running minikube with virtualbox as the driver.

minikube version
minikube version: v1.9.2

My start command: minikube start --mount=true --mount-string=$(HOME)/somedir/on/host/:/somedir/on/vm/

If you are struggling with this, I highly suggest running minikube help to see what the flags are. I suspect these could be changing for different versions/builds.


If you're looking to get this mounted directory into your pods, you must then establish a volume and volume mount in a manifest file. Here's a simplified version of mine.

apiVersion: v1
kind: Service
...
---

apiVersion: apps/v1
kind: Deployment
...
spec:
  replicas: 1
  ...
  template:
    ...
    spec:
      containers:
      ...
      volumeMounts:
      - name: someName
        mountPath: /somedir/on/vm/
    ...
    volumes:
    - name: someName
      hostPath:
        path: /somedir/on/vm/

Hopefully the extra polish, details and organization helps other people move a little faster on this.


Revisiting this as it seems other people like it: If you are tied to pure minikube, I'd suggest using the above, but I have found much better ways to locally develop minikube clusters using skaffold and docker for mac. Docker for mac can run a minikube like k8s cluster locally (in containers) using your local machine's docker daemon (so mounting files and such is a breeze). It is lovely and using it with skaffold makes hot reload and all other heavenly features possible. Do check those technologies out if you're not satisfied with the heaviness of minikube.

Chad Van De Hey
  • 2,716
  • 3
  • 29
  • 46
7

For an already running minikube you can do the following:

nohup minikube mount <host-directory-path>:<desired-minikube-directory-path> &

hokiegeek2
  • 303
  • 3
  • 7
1

to check which passes are mounted in the minikube run minikube ssh and then ls -la

Artem Kozlenkov
  • 975
  • 9
  • 11
0

Not sure if I joined the party late but I did a root:root mapping by doing following command:

minikube start --mount-string="/:/"

This just will just mount your local file system root to minikube and keeps things seamless between the systems.Hope it helps.

Lost
  • 12,007
  • 32
  • 121
  • 193
  • You should avoid to mount your host root folder. Minikube already [mounts automatically](https://v1-18.docs.kubernetes.io/docs/setup/learning-environment/minikube/#mounted-host-folders) (on virtualbox) the host home folder. If you want to mount a specific directory that is not located under your host's user folder then you should mount its path explicitly; just change the mount paths in `mount-string`. – Kamafeather Nov 22 '20 at 02:27
0

Provided answers are correct, but since I'm a beginner to k8s it's taking time to understand what happening. So I just updated the answer.

We can't directly mount local directory to your pod directory.

So, we're doing following things;

  1. Mounting data to minikube's data directory
  2. Through manifest.yml file we're copying data from minikube's data directory to pod's directory

For step-1:

 Sx: minikube start --mount --mount-string localpath-for-your-file:data
 Ex: minikube start --mount --mount-string ~/Desktop/data1:/data 

In the above command we’re saving data to minikube’s data folder not inside of your pod. ~ symbol represents root user directory i.e. home/user_name so we can use ~ symbol or we’ve to give full path. You can check this from minikube ssh command.

According to my ubuntu system I've to run following commands inside minikube ssh shell:

df
cd /dev
cd ..
ls 
cd data
ls

For step-2: Here we've to provide volume details in our manifest.yml file

volumes:
  - name: minimountdata
    hostPath:
      path: /data 
      # this is the root path for your minikube directory where you copied the code from your local system, 
#by running the command:=> minikube start --mount --mount-string ~/Desktop/data1:/data 
containers:
  - name: django-client 
  image: django-client-service
  ports:
  - containerPort: 5000
  imagePullPolicy: Never
  volumeMounts:
    - name: minimountdata
    mountPath: /demo-app/custom-addons #mountPath is the folder path in your pod