How to mount hostPath using docker-for-mac + kubernetes

I would like to use a ‘hostPath’ volume so that I can develop an app inside of kubernetes while making changes to the code on my host filesystem. Minikube makes /Users available inside it’s VM but I’m unsure what the analog is for docker containers inside kubernetes inside docker-for-mac. Thanks in advance!

Have a look at File Sharing under Preferences. There you can see the shared folders.
Example for volume, assuming /tmp is in the shared folders.

      volumes:
      - name: hostvolume
        hostPath:
          path: /tmp/folder_to_mount
2 Likes

Is there a nicer than envvar substitution way to overcome absolute paths restriction for mounted folders?

/tmp seems to work but not /Users even though it’s also included in the docker for mac shares (by default, note I reset to factory before doing this):

mike@shazzz
∴ echo text-in-temp-dir > /tmp/text-in-temp-dir.txt
mike@shazzz
∴ echo text-in-home-dir > /Users/mike/text-in-home-dir.txt
mike@shazzz
∴ cat /tmp/text-in-temp-dir.txt
text-in-temp-dir
mike@shazzz
∴ cat /Users/mike/text-in-home-dir.txt
text-in-home-dir
mike@shazzz
∴ cat /tmp/test-pod.yml
apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: k8s.gcr.io/test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /tmp-volume
      name: tmp-volume
    - mountPath: /home-volume
      name: home-volume
  volumes:
  - name: tmp-volume
    hostPath:
      path: /tmp
      type: Directory
  - name: home-volume
    hostPath:
      path: /Users/mike
      type: Directory
mike@shazzz
∴ kubectl apply -f /tmp/test-pod.yml
pod "test-pd" created
mike@shazzz
∴ kubectl get pods --show-all
NAME      READY     STATUS              RESTARTS   AGE
test-pd   0/1       ContainerCreating   0          12s
mike@shazzz
∴ kubectl describe po/test-pd
Name:         test-pd
Namespace:    default
Node:         docker-for-desktop/192.168.65.3
Start Time:   Sat, 10 Feb 2018 16:22:06 -0500
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"test-pd","namespace":"default"},"spec":{"containers":[{"image":"k8s.gcr.io/test-we...
Status:       Pending
IP:
Containers:
  test-container:
    Container ID:
    Image:          k8s.gcr.io/test-webserver
    Image ID:
    Port:           <none>
    State:          Waiting
      Reason:       ContainerCreating
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /home-volume from home-volume (rw)
      /tmp-volume from tmp-volume (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-prgn9 (ro)
Conditions:
  Type           Status
  Initialized    True
  Ready          False
  PodScheduled   True
Volumes:
  tmp-volume:
    Type:  HostPath (bare host directory volume)
    Path:  /tmp
  home-volume:
    Type:  HostPath (bare host directory volume)
    Path:  /Users/mike
  default-token-prgn9:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-prgn9
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason                 Age                From                         Message
  ----     ------                 ----               ----                         -------
  Normal   Scheduled              52s                default-scheduler            Successfully assigned test-pd to docker-for-desktop
  Normal   SuccessfulMountVolume  52s                kubelet, docker-for-desktop  MountVolume.SetUp succeeded for volume "tmp-volume"
  Normal   SuccessfulMountVolume  51s                kubelet, docker-for-desktop  MountVolume.SetUp succeeded for volume "default-token-prgn9"
  Warning  FailedMount            20s (x7 over 52s)  kubelet, docker-for-desktop  MountVolume.SetUp failed for volume "home-volume" : hostPath type check failed: /Users/mike is not a directory

Do /Users work if you remove the type for that volume? I have it working but I have nothing on the type…

Works perfectly. I definitely tried that but must have had something else wrong at the time. Thanks so much!

1 Like

As cartmanume said above, make sure that the path on host (that needs to be mounted inside the container) is added in the list in Preferences --> File Sharing:

17%20AM