Answers: <p>If it's a small(&lt;1MB) file, you can create a configmap with "--from-file" and mount it into volume.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-10-11T02:52:50.187Z
Answers: 2019-10-11T02:52:50.187Z
Answers: Leon
Answers: <p>Ok. Maybe you're not gonna like it but there are some things you can do.</p>
  
  <p>One and most simple is to use a volume mount, eventually on a persistent storage but I think an <a href="https://kubernetes.io/docs/concepts/storage/volumes/#emptydir" rel="noreferrer">emptyDir</a> can work as well (or even better depending on the nature of the file you want to copy) and use an <a href="https://kubernetes.io/docs/concepts/workloads/pods/init-containers/" rel="noreferrer">init container </a> to copy the file to the volume before the app container start. I don't know how you generate that file and I can't speculate about the security implications but the file needs to be in the docker image used for the init container. So, for your init container, you will have a Docker file  like </p>
  
  <pre><code>FROM ubuntu
  COPY localfile /container/file
  CMD cp /container/file /mnt/kube-volume
  </code></pre>
  
  <p>replacing localfile, container/file and /mnt/kube-volume with appropriate values; you build the container image locally -on your machine, so it can contain the file-  and upload it to the (secure) docker repo along with the app container image.</p>
  
  <p>Another method is to upload the file to a (secure) location and have an init container or a bootstrap process that download the file when you start your main application container  in the pod.</p>
  
  <p>The third option is to create a docker image that contains that file and use it for the app container in your pod.</p>
  
Answers: CC BY-SA 3.0
Answers: 2018-04-27T22:09:08.647Z
Answers: 2018-04-27T22:09:08.647Z
Answers: boca
Body: <pre><code>  # Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace
  &lt;some-namespace&gt;
    kubectl cp /tmp/foo &lt;some-namespace&gt;/&lt;some-pod&gt;:/tmp/bar
  </code></pre>
  
  <p>works, but then a pod should be running. The problem is that the file has to be copied before the pod starts. The jar file has to be loaded by an Application server. Destroying the pod after the copy will not work as the docker image is deployed again and the content will be wiped out.</p>
  
  <p>A configMap was misused to copy the jar before the pod starts, but that only seems to work for configuration files. This was confirmed by comparing the checksum, e.g. local vs. remote.</p>
  
Comments: CC BY-SA 3.0
Comments: 2018-03-01T22:57:50.327Z
Comments: You're more or less asking to load memory into a process namespaces before the process exists, I.e placing a couch in a house before the house is built... I  don't see a solution there
Comments: Tensibai
Comments: CC BY-SA 3.0
Comments: 2018-03-01T23:07:37.863Z
Comments: Copying config using configMap works, but if a larger file, like a jar needs to be copied then it is better to bake it using a Dockerfile?
Comments: 030
Comments: CC BY-SA 3.0
Comments: 2018-03-02T06:38:47.640Z
Comments: In brief Config Maps are files rendered on the target node for the pod from etcd and then the container is launched with this mount. I don't think it's a viable way  for large files like a jar.
Comments: Tensibai
Tags: <kubernetes>
Title: How to copy a file locally to remote pod before it starts?
UserDisplayName: 030
ThreadText: # How to copy a file locally to remote pod before it starts?
  
  (Asked by: username_0 on 2018-03-01)
  
  ```
    # Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace
  <some-namespace>
    kubectl cp /tmp/foo <some-namespace>/<some-pod>:/tmp/bar
  
  ```
  
  works, but then a pod should be running. The problem is that the file has to be copied before the pod starts. The jar file has to be loaded by an Application server. Destroying the pod after the copy will not work as the docker image is deployed again and the content will be wiped out.
  
  A configMap was misused to copy the jar before the pod starts, but that only seems to work for configuration files. This was confirmed by comparing the checksum, e.g. local vs. remote.
  
  **Tags:** kubernetes
  
  > **Comments:**
  > **username_1**: You're more or less asking to load memory into a process namespaces before the process exists, I.e placing a couch in a house before the house is built... I  don't see a solution there  
  > **username_0**: Copying config using configMap works, but if a larger file, like a jar needs to be copied then it is better to bake it using a Dockerfile?  
  > **username_1**: In brief Config Maps are files rendered on the target node for the pod from etcd and then the container is launched with this mount. I don't think it's a viable way  for large files like a jar.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2018-04-27. Score: 5 | Accepted answer
  
  Ok. Maybe you're not gonna like it but there are some things you can do.
  
  One and most simple is to use a volume mount, eventually on a persistent storage but I think an [emptyDir](https://kubernetes.io/docs/concepts/storage/volumes/#emptydir) can work as well (or even better depending on the nature of the file you want to copy) and use an [init container](https://kubernetes.io/docs/concepts/workloads/pods/init-containers/)  to copy the file to the volume before the app container start. I don't know how you generate that file and I can't speculate about the security implications but the file needs to be in the docker image used for the init container. So, for your init container, you will have a Docker file like
  
  ```
  FROM ubuntu
  COPY localfile /container/file
  CMD cp /container/file /mnt/kube-volume
  
  ```
  
  replacing localfile, container/file and /mnt/kube-volume with appropriate values; you build the container image locally -on your machine, so it can contain the file- and upload it to the (secure) docker repo along with the app container image.
  
  Another method is to upload the file to a (secure) location and have an init container or a bootstrap process that download the file when you start your main application container in the pod.
  
  The third option is to create a docker image that contains that file and use it for the app container in your pod.
  
  
  ---
  
  ### Answer by username_2 on 2019-10-11. Score: 0
  
  If it's a small(<1MB) file, you can create a configmap with "--from-file" and mount it into volume.
  
  
  ---
  
  