<p>Before posting this question I followed this answer <a href="https://stackoverflow.com/questions/30538210/how-to-mimic-volumes-from-in-kubernetes">How to mimic &#39;--volumes-from&#39; in Kubernetes</a> but it didn't work for me.</p>

<p>I have 2 containers:</p>

<ul>
<li><strong>node</strong>: its image contains all the files related to the app ( inside <code>/var/www</code> ) </li>
<li><strong>nginx</strong>: it needs to access the files inside the <strong>node</strong> image (especially the <code>/clientBuild</code> folder where I have all the assets)</li>
</ul>

<p>What is inside the <strong>node</strong> image:</p>

<pre><code>$ docker run node ls -l
&gt; clientBuild/
&gt; package.json
&gt; ...
</code></pre>

<p>A part of the <code>nginx.prod.conf</code>:</p>

<pre><code>location ~* \.(jpeg|jpg|gif|png|ico|css|js|gz|map|json)$ {
  include /etc/nginx/mime.types;
  root /usr/local/nginx/html/clientBuild/;
}
</code></pre>

<p>And the the deployment setup:</p>

<pre><code>kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: pwa-app-production
  labels:
    app: MyApp
spec:
  replicas: 1
  template:
    metadata:
      name: app
      labels:
        app: MyApp
        env: production
    spec:
      containers:
      - name: nginx
        image: nginx
        command: [nginx, -c, /nginx.prod.conf, -g, 'daemon off;']
        resources:
          limits:
            memory: "500Mi"
            cpu: "100m"
        imagePullPolicy: Always
        volumeMounts:
          - mountPath: /usr/local/nginx/html
            name: pwa-disk
            readOnly: true
        ports:
        - name: nginx
          containerPort: 80
      initContainers:
      - name: node
        image: node
        command: [npm, start]
        resources:
          limits:
            memory: "500Mi"
            cpu: "100m"
        imagePullPolicy: Always
        volumeMounts:
          - mountPath: /var/www
            name: pwa-disk
        ports:
        - name: app
          containerPort: 3000
        - name: api
          containerPort: 3001
      volumes:
        - name: pwa-disk
          emptyDir: {}
</code></pre>

<p>I first attempt to put both images in the same <code>containers</code> key, but i got: <code>/var/www/package.json not found</code> on <code>npm start</code></p>

<p>Then I moved it inside the <code>initContainers</code> but now I only have a notice that it failed, but it does not tell me why. View logs does not show any details too.</p>

<p>Notice that when I remove volume part, the <code>npm start</code> works.</p>

<p><a href="https://i.stack.imgur.com/Susmc.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Susmc.png" alt="enter image description here"></a></p>

<p>I assume your assets are already packaged inside the image at <code>/var/www</code>. If you mount an <code>emptyDir</code> volume at that path, then everything there gets overriden with the content of the <code>emptyDir</code> volume - which initially is nothing. That means all your assets are deleted through that mount - which is why your node server is most likely failing. </p>

<p>What you want to do is mount the <code>emptyDir</code> volume at some other path, say <code>/data</code>. Then you override your node containers cmd with <code>cp -r /var/www/* /data</code> to copy the assets into your<code>pwa-disk</code> volume. Now, you can mount this volume into your nginx container. </p>

<p>I think there is a misunderstanding on how <code>initContainers</code> work. They are meant to terminate. They run <strong>BEFORE</strong> any other container is started - no other container inside your pod is started until your <code>initContainers</code> have successfully terminated. So most likely you do not want to start your node server as an <code>initContainer</code>. I guess your node server is not supposed to terminate, in which case your nginx container will never start up. Instead, you might want to declare your node server together with your nginx inside the <code>containers</code> section. Additionally, you also add your node container with an overridden cmd (<code>cp -r /var/www/* /data</code>) to the <code>initContainers</code> section, to copy the assets to a volume. The whole thing might look sth like that: </p>

<pre><code>kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: pwa-app-production
  labels:
    app: MyApp
spec:
  replicas: 1
  template:
    metadata:
      name: app
      labels:
        app: MyApp
        env: production
    spec:
      containers:
      - name: nginx
        image: nginx
        command: [nginx, -c, /nginx.prod.conf, -g, 'daemon off;']
        resources:
          limits:
            memory: "500Mi"
            cpu: "100m"
        imagePullPolicy: Always
        volumeMounts:
          - mountPath: /usr/local/nginx/html
            name: pwa-disk
            readOnly: true
        ports:
        - name: nginx
          containerPort: 80
      - name: node
        image: node
        command: [npm, start]
        resources:
          limits:
            memory: "500Mi"
            cpu: "100m"
        imagePullPolicy: Always
        ports:
        - name: app
          containerPort: 3000
        - name: api
          containerPort: 3001

      initContainers:
      - name: assets
        image: node
        command: [bash, -c]
        args: ["cp -r /var/www/* /data"]
        imagePullPolicy: Always
        volumeMounts:
          - mountPath: /data
            name: pwa-disk
      volumes:
        - name: pwa-disk
          emptyDir: {}
</code></pre>
