<p>Say we have a simple deployment.yml file:</p>
<pre><code>apiVersion: apps/v1 
kind: Deployment
metadata:
  namespace: ikg-api-demo
  name: ikg-api-demo
spec:
  selector:
    matchLabels:
      app: ikg-api-demo
  replicas: 3 
  template:
    metadata:
      labels:
        app: ikg-api-demo
    spec:
      containers:
        - name: ikg-api-demo
          imagePullPolicy: Always
          image: example.com/main_api:private_key
          ports:
            - containerPort: 80
</code></pre>
<p>the problem is that this image/container depends on another image/container - it needs to cp data from the other image, or use some shared volume.</p>
<p>How can I tell kubernetes to download another image, run it as a container, and then copy data from it to the container declared in the above file?</p>
<p>It looks like <a href="https://kubernetes.io/docs/tasks/access-application-cluster/communicate-containers-same-pod-shared-volume/" rel="nofollow noreferrer">this article</a> explains how.</p>
<p>but it's not 100% clear how it works. It looks like you create some shared volume, launch the two containers, using that shared volume?</p>
<p>so I according to that link, I added this to my deployment.yml:</p>
<pre><code>spec:

  volumes:
    - name: shared-data
      emptyDir: {}

  containers:

    - name: ikg-api-demo
      imagePullPolicy: Always
      volumeMounts:
        - name: shared-data
          mountPath: /nltk_data
      image: example.com/nltk_data:latest

    - name: ikg-api-demo
      imagePullPolicy: Always
      volumeMounts:
        - name: shared-data
          mountPath: /nltk_data
      image: example.com/main_api:private_key
      ports:
        - containerPort: 80
</code></pre>
<p>my primary hesitation is that mounting /nltk_data as a shared volume will overwrite what might be there already.</p>
<p>So I assume what I need to do is mount it at some other location, and then make the ENTRYPOINT for the source data container:</p>
<pre><code>ENTRYPOINT ['cp', '-r', '/nltk_data_source', '/nltk_data']
</code></pre>
<p>so that will write it to the shared volume, once the container is launched.</p>
<p><strong>So I have two questions:</strong></p>
<ol>
<li><p>How to run one container and finish a job, before another container starts using kubernetes?</p>
</li>
<li><p>How to write to a shared volume without having that shared volume overwrite what's in your image? In other words, if I have /xyz in the image/container, I don't want to have to copy <code>/xyz</code> to <code>/shared_volume_mount_location</code> if I don't have to.</p>
</li>
</ol>

<h1>How to run one container and finish a job, before another container starts using kubernetes?</h1>
<p>Use initContainers - updated your deployment.yml, assuming <code>example.com/nltk_data:latest</code> is your data image</p>
<h1>How to write to a shared volume without having that shared volume overwrite?</h1>
<p>As you know what is there in your image, you need to select an appropriate mount path. I would use <code>/mnt/nltk_data</code></p>
<h1>Updated deployment.yml with init containers</h1>
<pre><code>spec:
  volumes:
    - name: shared-data
      emptyDir: {}
  initContainers:
    - name: init-ikg-api-demo
      imagePullPolicy: Always
      # You can use command, if you don't want to change the ENTRYPOINT
      command: ['sh', '-c', 'cp -r /nltk_data_source /mnt/nltk_data']
      volumeMounts:
        - name: shared-data
          mountPath: /mnt/nltk_data
      image: example.com/nltk_data:latest
  containers:
    - name: ikg-api-demo
      imagePullPolicy: Always
      volumeMounts:
        - name: shared-data
          mountPath: /nltk_data
      image: example.com/main_api:private_key
      ports:
        - containerPort: 80
</code></pre>
