<p>I have an image which contains data inside /usr/data/webroot. This data should be moved on container init to /var/www/html.</p>

<p>Now I stumbeled upon InitContainers. As I understand it, it can be used to execute tasks on container initialization. </p>

<p>But I don't know if the task is beeing excuted after the amo-magento pods are created, or if the init task runs, and after that the magento pods are created.</p>

<p>I suppose that the container with the magento image is not available when the initContainers task runs, so no content is available to move to the new directory.</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: amo-magento
  labels:
    app: amo-magento
spec:
  replicas: 1
  selector:
    matchLabels:
      app: amo-magento
  template:
    metadata:
      labels:
        app: amo-magento
        tier: frontend
    spec:
      initContainers:
        - name: setup-magento
          image: busybox:1.28
          command: ["sh", "-c", "mv -r /magento/* /www"]

          volumeMounts:
            - mountPath: /www
              name: pvc-www

            - mountPath: /magento
              name: magento-src

      containers:
        - name: amo-magento
          image: amo-magento:0.7 # add google gcr.io path after upload
          imagePullPolicy: Never

          volumeMounts:
            - name: install-sh
              mountPath: /tmp/install.sh
              subPath: install.sh

            - name: mage-autoinstall
              mountPath: /tmp/mage-autoinstall.sh
              subPath: mage-autoinstall.sh

            - name: pvc-www
              mountPath: /var/www/html

            - name: magento-src
              mountPath: /usr/data/webroot

            # maybe as secret - can be used as configMap because it has not to be writable
            - name: auth-json
              mountPath: /var/www/html/auth.json
              subPath: auth.json

            - name: php-ini-prod
              mountPath: /usr/local/etc/php/php.ini
              subPath: php.ini

#            - name: php-memory-limit
#              mountPath: /usr/local/etc/php/conf.d/memory-limit.ini
#              subPath: memory-limit.ini

      volumes:
        - name: magento-src
          emptyDir: {}

        - name: pvc-www
          persistentVolumeClaim:
            claimName: magento2-volumeclaim

        - name: install-sh
          configMap:
            name: install-sh

        # kubectl create configmap mage-autoinstall --from-file=build/docker/mage-autoinstall.sh
        - name: mage-autoinstall
          configMap:
            name: mage-autoinstall

        - name: auth-json
          configMap:
            name: auth-json

        - name: php-ini-prod
          configMap:
            name: php-ini-prod

#        - name: php-memory-limit
#          configMap:
#            name: php-memory-limit

</code></pre>

John Jameson
<blockquote>
  <p>But I don't know if the task is beeing excuted after the amo-magento pods are created, or if the init task runs, and after that the magento pods are created.</p>
</blockquote>

<p>For sure the latter, that's why you are able to specify an <strong>entirely different</strong> <code>image:</code> for your <code>initContainers:</code> task -- they are related to one another only in that they run on the same Node and, as you have seen, share volumes. Well, I said "for sure" but you have a slight misnomer: after that the <code>magneto</code> <strong>containers</strong> are created -- the Pod is the collection of <em>every</em> colocated container, <code>initContainers:</code> and <code>container:</code> containers</p>

<p>If I understand your question, the fix to your <code>Deployment</code> is just to update the <code>image:</code> in your <code>initContainer:</code> to be the one which contains the magic <code>/usr/data/webroot</code> and then update your shell command to reference the correct path inside that image:</p>

<pre><code>  initContainers:
    - name: setup-magento
      image: your-magic-image:its-magic-tag
      command: ["sh", "-c", "mv -r /usr/data/webroot/* /www"]
      volumeMounts:
        - mountPath: /www
          name: pvc-www
        # but **removing** the reference to the emptyDir volume
</code></pre>

<p>and then by the time the <code>container[0]</code> starts up, the PVC will contain the data you expect</p>

<hr>

<p>That said, I am actually <em>pretty sure</em> that you want to remove the PVC from this story, since -- by definition -- it is persistent across Pod reboots and thus will only accumulate files over time (since your <code>sh</code> command does not currently clean up <code>/www</code> before moving files there). If you replaced all those <code>pvc</code> references to <code>emptyDir: {}</code> references, then those directories would always be "fresh" and would always contain just the content from the tagged image declared in your <code>initContainer:</code></p>
