<pre><code>kubectl -n magento logs magento-install-jssk6
</code></pre>
<p>I am getting <code>Database found In ConfigModel.php line 166:Missing write permissions to the following paths: /var/www/html/pub/media</code> in install job:</p>
<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: magento-install
  namespace: magento
spec:
  template:
    metadata:
      name: install
      labels:
        app: magento-install
        k8s-app: magento
    spec:
      containers:
      - name: magento-setup
        image: kiweeteam/magento2:vanilla-2.3.4-php7.3-fpm
        command: [&quot;/bin/sh&quot;]
        args:
        - -c
        - |
          /bin/bash &lt;&lt;'EOF'
          bin/install.sh
          php bin/magento setup:perf:generate-fixtures setup/performance-toolkit/profiles/ce/small.xml
          magerun index:list | awk '{print $2}' | tail -n+4 | xargs -I{} magerun index:set-mode schedule {}
          magerun cache:flush
          EOF
        envFrom:
        - configMapRef:
            name: config
        volumeMounts:
        - mountPath: /var/www/html/pub/media
          name: media
      volumes:
      - name: media
        persistentVolumeClaim:
          claimName: media
      restartPolicy: OnFailure
</code></pre>

<blockquote>
<p>and when I try to change permissions I am getting chown: changing
ownership of '/var/www/html/pub/media': Operation not permitted</p>
</blockquote>
<p>It happens because you run <code>chown</code> as <code>www-data</code> user and the current owner of this directory is <code>root</code>.</p>
<p>You can resolve your issue by using the <a href="https://kubernetes.io/docs/concepts/workloads/pods/init-containers/" rel="nofollow noreferrer">init container</a> run as <code>root</code> (user with id <code>0</code>). Below you can see a modified version of your <code>magento-install</code> <code>Job</code> with the <strong>init cotntainer</strong> already added:</p>
<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: magento-install
  namespace: magento
spec:
  template:
    metadata:
      name: install
      labels:
        app: magento-install
        k8s-app: magento
    spec:
      initContainers:
      - name: magento-chown
        securityContext:
          runAsUser: 0
        image: kiweeteam/magento2:vanilla-2.3.4-php7.3-fpm
        command: ['sh', '-c', 'chown -R www-data:www-data /var/www/html/pub/media']
        volumeMounts:
        - name: media
          mountPath: &quot;/var/www/html/pub/media&quot;
      containers:
      - name: magento-setup
        image: kiweeteam/magento2:vanilla-2.3.4-php7.3-fpm
        command: [&quot;/bin/sh&quot;]
        args:
        - -c
        - |
          /bin/bash &lt;&lt;'EOF'
          bin/install.sh
          php bin/magento setup:perf:generate-fixtures setup/performance-toolkit/profiles/ce/small.xml
          magerun index:list | awk '{print $2}' | tail -n+4 | xargs -I{} magerun index:set-mode schedule {}
          magerun cache:flush
          EOF
        envFrom:
        - configMapRef:
            name: config
        volumeMounts:
        - mountPath: /var/www/html/pub/media
          name: media
      volumes:
      - name: media
        persistentVolumeClaim:
          claimName: media
      restartPolicy: OnFailure
</code></pre>
<p>Once you attach to your newly created <code>Pod</code> by using:</p>
<pre><code>kubectl exec -ti -n magento magento-install-z66qg -- /bin/bash
</code></pre>
<p>You'll see that the current owner of the <code>/var/www/html/pub/media</code> directory isn't any more <code>root</code> but <code>www-data</code> user:</p>
<pre><code>www-data@magento-install-z66qg:~/html$ ls -ld /var/www/html/pub/media
drwxr-xr-x 3 www-data www-data 4096 Jul 27 18:45 /var/www/html/pub/media
</code></pre>
<p>We can simplify it even more. The <strong>init container</strong> doesn't even need to use the <code>kiweeteam/magento2:vanilla-2.3.4-php7.3-fpm</code> image. It might as well be a simple container based on <code>busybox</code>, which runs as <code>root</code> by default so you can omit the security context from the previous example and your <code>initContainers</code> section will look as follows:</p>
<pre><code>initContainers:
- name: magento-chown
  image: busybox
  command: ['sh', '-c', 'chown -R www-data:www-data /var/www/html/pub/media']
  volumeMounts:
  - name: media
</code></pre>
<p>The final effect will be exactly the same.</p>
