<p>I have a deployment of wordpress with a custom docker image with a custom theme copied into the image that gets deployed into a kubernetes cluster with a persistent volume.</p>
<p>The initial deployment works great and the website presents as expected. The problem comes when I update the theme and deploy a new docker image, because of the persistent volume the theme files don't seem to be updated to the new version of the theme in the docker image.</p>
<p>Is there a to clear/reset the wp-content/themes/my-theme directory when I deploy the new image?</p>
<p>Any help is appreciated and code samples below.</p>
<p>Dockerfile:</p>
<pre><code>FROM wordpress:latest
COPY ./my-thtme /usr/src/wordpress/wp-content/themes/my-theme
</code></pre>
<p>Persistent Volume Claim:</p>
<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wordpress
spec:
    accessModes:
        - ReadWriteMany
    resources:
        requests:
            storage: 2Gi
    storageClass: &quot;nfs&quot;
</code></pre>
<p>Deployment:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
spec:
  replicas: 2
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      imagePullSecrets:
        - name: gitlab-auth
      containers:
        - name: wordpress
          image: registry.gitlab.com/user/wordpress:1234
          imagePullPolicy: IfNotPresent
          env:
            - name: WORDPRESS_DB_HOST
              value: wordpress-mysql
            - name: WORDPRESS_DB_USER
              value: mysql_wordpress
            - name: WORDPRESS_DB_NAME
              value: wordpress
            - name: WORDPRESS_DB_TABLE_PREFIX
              value: _wp
            - name: WORDPRESS_DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-pass
                  key: mysql-password
          volumeMounts:
          - name: wordpress-data
            mountPath: /var/www/html/wp-content
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
      volumes:
      - name: wordpress-data
        persistentVolumeClaim:
          claimName: wordpress
</code></pre>

Vaugen Wakeling
<p>There is more than one way to do it. You could add an <a href="https://kubernetes.io/docs/concepts/workloads/pods/init-containers/" rel="nofollow noreferrer"><code>initContainer</code></a> to the <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/" rel="nofollow noreferrer"><code>Deployment</code></a> spec to remove existing files from the <a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/" rel="nofollow noreferrer"><code>Persistent Volume</code></a> before app containers in the <a href="https://kubernetes.io/docs/concepts/workloads/pods/" rel="nofollow noreferrer"><code>Pod</code></a> are started. For example:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
spec:
  replicas: 2
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      imagePullSecrets:
        - name: gitlab-auth
      initContainers:
      - name: init-theme
        image: &quot;alpine:3&quot;
        command: [&quot;sh&quot;, &quot;-c&quot;, &quot;if [ -d /var/www/html/wp-content/my-theme ]; then rm -rf /var/www/html/wp-content/my-theme; fi&quot;]
        volumeMounts:
          - name: workdpress-data
            mountPath: /var/www/html/wp-content
      containers:
      ...
      volumes:
      - name: wordpress-data
        persistentVolumeClaim:
          claimName: wordpress
</code></pre>
