<p>I'm deploying locally in docker-for-desktop. So that I can migrate to a kubernetes cluster in the future.</p>

<p>However I face a problem. Directories in the docker container/pod are over written, when persistent volumes are used.</p>

<p>I'm pulling the latest SonarQube image. A lot of plugins and quality profiles are pre-installed. Which is exactly what I want. If I don't use persistent volumes. Everything works as expected. When I use a pv all the data in the image is overwritten. I use helm.</p>

<p>In my deployment.yaml I use this:</p>

<pre><code> {{- if (eq .Values.volumes.usePersistent "true") }}
          volumeMounts:
          - mountPath: "/opt/sonarqube/data"
            name: sonarqube-data
          - mountPath: "/opt/sonarqube/extensions"
            name: sonarqube-extensions
      volumes:
      - name: sonarqube-data
        persistentVolumeClaim:
          claimName: sonarqube-data-pv-claim
      - name: sonarqube-extensions
        persistentVolumeClaim:
          claimName: sonarqube-extensions-pv-claim
{{- end }}
</code></pre>

<p>In my storage.yaml I use this: </p>

<pre><code>{{- if (eq .Values.volumes.usePersistent "true") }}
kind: PersistentVolume
apiVersion: v1
metadata:
  name: sonarqube-data-pv-volume
  labels:
    type: local
    app: sonarqube-data
spec:
  storageClassName: manual
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/tmp/toolbox/sonarqube/data"
---
kind: PersistentVolume
apiVersion: v1
metadata:
  name: sonarqube-extensions-pv-volume
  labels:
    type: local
    app: sonarqube-extensions
spec:
  storageClassName: manual
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/tmp/toolbox/sonarqube/extensions"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: sonarqube-data-pv-claim
  labels:
    app: sonarqube-data
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 2Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: sonarqube-extensions-pv-claim
  labels:
    app: sonarqube-extensions
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 2Gi
{{- end }}
</code></pre>

<p>The pvc are bounded and working.
All the data that I need are in de 'data' and 'extensions' folder in the container, coming from the image.
For example in the extensions folder:</p>

<pre><code>sonarqube@sonarqube-deployment-6b8bdfb766-klnwh:/opt/sonarqube/extensions/plugins$ ls
README.txt               sonar-java-plugin-5.11.0.17289.jar      sonar-scala-plugin-1.5.0.315.jar
sonar-csharp-plugin-7.11.0.8083.jar  sonar-javascript-plugin-5.1.1.7506.jar  sonar-scm-git-plugin-1.8.0.1574.jar
sonar-css-plugin-1.0.3.724.jar       sonar-kotlin-plugin-1.5.0.315.jar       sonar-scm-svn-plugin-1.9.0.1295.jar
sonar-flex-plugin-2.4.0.1222.jar     sonar-ldap-plugin-2.2.0.608.jar         sonar-typescript-plugin-1.9.0.3766.jar
sonar-go-plugin-1.1.0.1612.jar       sonar-php-plugin-3.0.0.4537.jar         sonar-vbnet-plugin-7.11.0.8083.jar
sonar-html-plugin-3.1.0.1615.jar     sonar-python-plugin-1.13.0.2922.jar     sonar-xml-plugin-2.0.1.2020.jar
sonar-jacoco-plugin-1.0.1.143.jar    sonar-ruby-plugin-1.5.0.315.jar
</code></pre>

<p>I have made the following directories in my /tmp folder:</p>

<pre><code>- data
- extensions
   - downloads
   - jdbc-driver
   - plugins
</code></pre>

<p>I know I must specify the same folders in my pv as in my container.
I checked, all the folders are there in my /tmp folder. But are empty.
They plugins folder is empty, all the plugin.jar files are gone.</p>

<p>BTW I did not include this in the initial post, but I'm using a PostgresDB also with pvc. pg-deploy.yaml:</p>

<p><code>
{{- if (eq .Values.volumes.usePersistent "true") }}
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: sonarqubedb
      volumes:
        - name: sonarqubedb
          persistentVolumeClaim:
            claimName: postgres-sq-pv-claim
{{- end }}
</code></p>

<p>storage.yaml:</p>

<pre><code>{{- if (eq .Values.volumes.usePersistent "true") }}
kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgres-sq-pv-volume
  labels:
    type: local
    app: postgres-sonarqube
spec:
  storageClassName: manual
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/tmp/toolbox/postgres-sonarqube"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-sq-pv-claim
  labels:
    app: postgres-sonarqube
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 2Gi
{{- end }}
</code></pre>

<p>to avoid overwriting data to the the existing files/content inside the same Directory, you can use <strong>subpath</strong> to mount the data and extensions directory (In the example below) in the existing Container file system. for further detail <a href="https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath" rel="nofollow noreferrer">sub-path</a></p>

<pre><code>      volumeMounts:
      - mountPath: "/opt/sonarqube/data"
        name: sonarqube-data
        subPath: data
      - mountPath: "/opt/sonarqube/extensions"
        name: sonarqube-extensions
        subPath: extensions 
</code></pre>

<p>This works. However it didn't work until I did the same for the database that sonarqube is using:</p>

<pre><code>        volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: sonarqubedb
              subPath: data
</code></pre>
