How to mount a host directory on minikube

minikube mount host directory

2 min read | by Jordi Prats

To be able to test Kubernetes applications, minikube is a great tool: You can create an ephemeral Kubernetes cluster to test whatever is needed and delete it as easily as it was created. Futhermore, since it can use your computer's resources you won't get billed as you would if you'd choose to use a cloud provider.

Another advantage is that we can make available local directories to the cluster using minikube mount

To do so we'll need to specify first the local directory we want to share and to what directory we want it mapped on the Kubernetes host. So, it's format is:

minikube mount <local directory>:<host directory>

An example command would be:

$ minikube mount /home/pet2cattle/Downloads:/downloads
📁  Mounting host path /home/pet2cattle/Downloads into VM as /downloads ...
     Mount type:   
     User ID:      docker
     Group ID:     docker
     Version:      9p2000.L
     Message Size: 262144
     Options:      map[]
     Bind Address: 192.168.49.1:41889
🚀  Userspace file server: ufs starting
  Successfully mounted /home/pet2cattle/Downloads to /downloads

📌  NOTE: This process must stay alive for the mount to be accessible ...

While this process stays running, we can mount the host directory on a pod using hostPath, for example:

apiVersion: v1
kind: Pod
metadata:
  name: minikube-mount
spec:
  volumes:
  - name: minikube-mount
    hostPath:
      path: /downloads
      type: Directory
  containers:
  - image: alpine:latest
    name: one
    command:
    - sleep
    - 24h
    volumeMounts:
      - name: minikube-mount
        mountPath: /mnt/minikube

Once applied this yaml file, we can run a command on the target Pod to check that we can access the files shared using minikube mount:

$ kubectl apply -f minikube-mount.yaml 
pod/minikube-mount created
$ kubectl get pods
NAME             READY   STATUS    RESTARTS   AGE
minikube-mount   1/1     Running   0          45s
$ kubectl exec -it minikube-mount -- ls /mnt/minikube
(...)
zoom_amd64(1).deb
zoom_amd64(2).deb
zoom_amd64.deb

Posted on 14/03/2022

Categories