<p>I need to copy a file inside my pod during the time of creation. I don't want to use <code>ConfigMap</code> and <code>Secrets</code>. I am trying to create a <code>volumeMounts</code> and copy the source file using the <code>kubectl cp</code> command—my manifest looks like this.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Pod
metadata:
    name: copy
    labels:
      app: hello
spec:
  containers:
  - name: init-myservice
    image: bitnami/kubectl
    command: ['kubectl','cp','./test.json','init-myservice:./data']
    volumeMounts:
    - name: my-storage
      mountPath: data
  - name: init-myservices
    image: nginx
    volumeMounts:
    - name: my-storage
      mountPath: data
  volumes:
  - name: my-storage
    emptyDir: {}
</code></pre>
<p>But I am getting a <code>CrashLoopBackOff</code> error. Any help or suggestion is highly appreciated.</p>

<p>I do agree with an answer provided by H.R. Emon, it explains why you can't just run <code>kubectl cp</code> inside of the container. I do also think there are some resources that could be added to show you how you can tackle this particular setup.</p>
<p>For this particular use case it is recommended to use an <code>initContainer</code>.</p>
<blockquote>
<p><code>initContainers</code> - specialized containers that run before app containers in a Pod. Init containers can contain utilities or setup scripts not present in an app image.</p>
<p><em><a href="https://kubernetes.io/docs/concepts/workloads/pods/init-containers/" rel="nofollow noreferrer">Kubernetes.io: Docs: Concepts: Workloads: Pods: Init-containers</a></em></p>
</blockquote>
<p>You could use the example from the official Kubernetes documentation (assuming that downloading your <code>test.json</code> is feasible):</p>
<blockquote>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Pod
metadata:
  name: init-demo
spec:
 containers:
 - name: nginx
   image: nginx
   ports:
   - containerPort: 80
   volumeMounts:
   - name: workdir
     mountPath: /usr/share/nginx/html
 # These containers are run during pod initialization
 initContainers:
 - name: install
   image: busybox
   command:
   - wget
   - &quot;-O&quot;
   - &quot;/work-dir/index.html&quot;
   - http://info.cern.ch
   volumeMounts:
   - name: workdir
     mountPath: &quot;/work-dir&quot;
 dnsPolicy: Default
 volumes:
 - name: workdir
   emptyDir: {}
</code></pre>
<p>-- <em><a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-initialization/#create-a-pod-that-has-an-init-container" rel="nofollow noreferrer">Kubernetes.io: Docs: Tasks: Configure Pod Initalization: Create a pod that has an initContainer</a></em></p>
</blockquote>
<p>You can also modify above example to your specific needs.</p>
<hr />
<p><strong>Also, referring to your particular example, there are some things that you will need to be aware of:</strong></p>
<ul>
<li>To use <code>kubectl</code> inside of a <code>Pod</code> you will need to have required permissions to access the Kubernetes API. You can do it by using <code>serviceAccount</code> with some permissions. More can be found in this links:
<ul>
<li><em><a href="https://kubernetes.io/docs/reference/access-authn-authz/authentication/#service-account-tokens" rel="nofollow noreferrer">Kubernetes.io: Docs: Reference: Access authn authz: Authentication: Service account tokens</a></em></li>
<li><em><a href="https://kubernetes.io/docs/reference/access-authn-authz/rbac/" rel="nofollow noreferrer">Kubernetes.io: Docs: Reference: Access authn authz: RBAC</a></em></li>
</ul>
</li>
<li>Your <code>bitnami/kubectl</code> container will run into <code>CrashLoopBackOff</code> errors because of the fact that you're passing a single command that will run to completion. After that <code>Pod</code> would report status <code>Completed</code> and it would be restarted due to this fact resulting in before mentioned <code>CrashLoopBackOff</code>. To avoid that you would need to use <code>initContainer</code>.</li>
<li>You can read more about what is happening in your setup by following this answer (connected with previous point):
<ul>
<li><em><a href="https://stackoverflow.com/a/62589092/12257134">Stackoverflow.com: Questions: What happens one of the container process crashes in multiple container POD?</a></em></li>
</ul>
</li>
</ul>
<hr />
<p>Additional resources:</p>
<ul>
<li><em><a href="https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/" rel="nofollow noreferrer">Kubernetes.io: Pod lifecycle</a></em></li>
</ul>
<blockquote>
<p>A side note!</p>
<p>I also do consider including the reason why <code>Secrets</code> and <code>ConfigMaps</code> cannot be used to be important in this particular setup.</p>
</blockquote>
