Published on

Setting Up Persistent Local Volumes on Docker Desktop Kubernetes (WSL2 or Mac)

Authors

Docker Desktop’s Kubernetes integration on WSL2 or macOS is great for local testing. Persisting data with local volumes needs a few specifics. This guide walks you through a working setup.

1) Open a shell on the Docker Desktop node

You need to create a host path for your local volume on the Docker Desktop Kubernetes node:

# Get the node name
kubectl get nodes

# Run a privileged container on the node to access the filesystem
kubectl debug node/docker-desktop -it --image=busybox --target= # or use a debug daemonset

Create a directory that will serve as the volume path, e.g. /var/local-path/testdata.

2) Create a PersistentVolume (PV) using hostPath

apiVersion: v1
kind: PersistentVolume
metadata:
  name: local-pv-test
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  storageClassName: local-hostpath
  hostPath:
    path: /var/local-path/testdata

Apply it:

kubectl apply -f pv.yaml

3) Create a PersistentVolumeClaim (PVC)

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: local-pvc-test
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: local-hostpath
kubectl apply -f pvc.yaml

4) Mount the PVC in a Pod/Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-app
spec:
  replicas: 1
  selector:
    matchLabels: { app: test-app }
  template:
    metadata:
      labels: { app: test-app }
    spec:
      containers:
        - name: app
          image: nginx:alpine
          volumeMounts:
            - name: data
              mountPath: /usr/share/nginx/html/data
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: local-pvc-test

Notes

  • On Windows/WSL2, ensure the path is accessible and persists; prefer /var/... inside the node.
  • Docker Desktop sometimes prunes node files on reset; keep backups for critical data.
  • You can also use the local-path-provisioner for dynamic provisioning.

Verify

  • Write a file in the container under the mount path
  • Restart the pod; the file should persist

That’s it — you now have persistent local volumes running on Docker Desktop Kubernetes for WSL2 or Mac.