[k8s] How to mount local directory (persistent volume) to Kubernetes pods of Docker Desktop for Mac?

  1. Check and add the directories you want to share into your Docker Desktop preferences.
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 1
minReadySeconds: 10
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:latest
...
volumeMounts:
- name: my-volume
mountPath: /data
volumes:
- name: my-volume
hostPath:
path: /tmp
type: DirectoryOrCreate

...
  1. Add the shared folder into Docker Desktop preferences.
#
# PersistentVolume
#
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-persistent-volume
labels:
type: local
spec:
storageClassName: hostpath
capacity:
storage: 256Mi
accessModes:
- ReadWriteMany
hostPath:
path: /tmp

persistentVolumeReclaimPolicy: Retain
---#
# PersistentVolumeClaim
#
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-persistent-volume-claim
spec:
storageClassName: hostpath
accessModes:
- ReadWriteMany
resources:
requests:
storage: 256Mi
apiVersion: v1
kind: StorageClass
apiVersion: storage.k8s.io/v1beta1
metadata:
name: my-storage-class
provisioner: docker.io/hostpath
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 1
minReadySeconds: 10
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:latest
...
volumeMounts:
- name: my-volume
mountPath: /data
volumes:
- name: my-volume
persistentVolumeClaim:
claimName: my-persistent-volume-claim

...

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store