<p>I am trying to setup a persistent volume for K8s that is running in Docker Desktop for Windows. The end goal being I want to run Jenkins and not lose any work if docker/K8s spins down.</p>

<p>I have tried a couple of things but I'm either misunderstanding the ability to do this or I am setting something up wrong. Currently I have the environment setup like so:</p>

<p>I have setup a volume in docker for Jenkins. All I did was create the volume, not sure if I need more configuration here.</p>

<pre><code>docker volume inspect jenkins-pv
[
    {
        "CreatedAt": "2020-05-20T16:02:42Z",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/jenkins-pv/_data",
        "Name": "jenkins-pv",
        "Options": {},
        "Scope": "local"
    }
]
</code></pre>

<p>I have also created a persistent volume in K8s pointing to the mount point in the Docker volume and deployed it.</p>

<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: jenkins-pv-volume
  labels:
    type: hostPath
spec:
  storageClassName: manual
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  hostPath:
    path: "/var/lib/docker/volumes/jenkins-pv/_data"
</code></pre>

<p>I have also created a pv claim and deployed that.</p>

<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: jenkins-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
</code></pre>

<p>Lastly I have created a deployment for Jenkins. I have confirmed it works and I am able to access the app.</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins-app
  template:
    metadata:
      labels:
        app: jenkins-app
    spec:
      containers:
      - name: jenkins-pod
        image: jenkins/jenkins:2.237-alpine
        ports:
        - containerPort: 50000
        - containerPort: 8080
        volumeMounts:
        - name: jenkins-pv-volume
          mountPath: /var/lib/docker/volumes/jenkins-pv/_data
      volumes:
      - name: jenkins-pv-volume
        persistentVolumeClaim:
          claimName: jenkins-pv-claim
</code></pre>

<p>However, the data does not persist quitting Docker and I have to reconfigure Jenkins every time I start. Did I miss something or how/what I am trying to do not possible? Is there a better or easier way to do this?</p>

<p>Thanks!</p>

Nick Orlowski
<p>I figured out my issue, it was two fold. </p>

<ol>
<li>I was trying to save data from the wrong location within the pod that was running Jenkins.</li>
<li>I was never writing the data back to docker shared folder.</li>
</ol>

<p>To get this working I created a shared folder in Docker (C:\DockerShare).
Then I updated the host path in my Persistent Volume.
The format is <em>/host_mnt/path_to_docker_shared_folder_location</em>
Since I used C:\DockerShare my path is: <em>/host_mnt/c/DockerShare</em></p>

<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: jenkins
  labels:
    type: hostPath
spec:
  storageClassName: manual
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /host_mnt/c/DockerShare/jenkins
</code></pre>

<p>I also had to update the Jenkins deployment because I was not actually saving any of the config.
I should have been saving data from <em>/var/jenkins_home</em>.</p>

<p>Deployment looks like this: </p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins-app
  template:
    metadata:
      labels:
        app: jenkins-app
    spec:
      containers:
      - name: jenkins-pod
        image: jenkins/jenkins:2.237-alpine
        ports:
        - containerPort: 50000
        - containerPort: 8080
        volumeMounts:
        - name: jenkins
          mountPath: /var/jenkins_home
      volumes:
      - name: jenkins
        persistentVolumeClaim:
          claimName: jenkins
</code></pre>

<p>Anyways its working now and I hope this helps someone else when it comes to setting up a PV.</p>
