How to copy files to container in kubernetes yaml
Asked Answered
W

2

5

I understand that files / folders can be copied into a container using the command:

kubectl cp /tmp/foo_dir <some-pod>:/tmp/bar_dir

However, I am looking to do this in a yaml file

How would I go about doing this? (Assuming that I am using a deployment for the container)

Windom answered 7/3, 2020 at 21:9 Comment(2)
What kind of files are they? If you can't use a ConfigMap then you need to add them to the image you're deploying, or arrange for them to be in a volume you can mount.Greig
The purpose of this is to copy files into /usr/share/nginx/html for nginx web server, so html, css and javascript files. I initially discounted copying files into the image itself since in my git repo the www files are in a parent directory (and I don't believe you can copy files from a parent directory using COPY in a Dockerfile). This would also complicate continuous delivery since I would have to rebuild each dockerfile manually before deploying to the k8s cluster.Windom
A
10

The way you are going is wrong direction. Kubernetes does this with serveral ways.

first, think about configmap

https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap

You can easily define the configuration files for your application running in container

If you do know the files or folders is exist on worker nodes, you can use hostPath to mount it into container with nominated nodeName: node01 in k8s yaml.

https://kubernetes.io/docs/concepts/storage/volumes/#hostpath

if the files or folders are generated temporarily, you can use emptyDir

https://kubernetes.io/docs/concepts/storage/volumes/#emptydir

Ammann answered 8/3, 2020 at 1:17 Comment(0)
O
-3

You cannot, mapping local files from your workstation is not a feature of Kubernetes.

Oswald answered 7/3, 2020 at 21:16 Comment(4)
This seems strange, since don't pods have their own filesystem which all containers have access to? It seems odd that a feature to copy static files into the filesystem hasn't been created...Windom
Kubernetes is built for heavy automation, not manual workstation stuff. You would have to do something like putting the files in an initContainer or using whatever prefill system your PVC provider offers, if any.Oswald
hostPath is a thing.Manlove
hostPath maps files from the underlying host, not from your workstation.Oswald

© 2022 - 2024 — McMap. All rights reserved.