How to copy files to container in kubernetes yaml

10,144

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

Share:
10,144

Related videos on Youtube

Kyle Blue
Author by

Kyle Blue

Updated on June 04, 2022

Comments

  • Kyle Blue
    Kyle Blue almost 2 years

    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)

    • David Maze
      David Maze about 4 years
      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.
    • Kyle Blue
      Kyle Blue about 4 years
      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.
  • Kyle Blue
    Kyle Blue about 4 years
    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...
  • opricnik
    opricnik about 4 years
    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.
  • Константин Ван
    Константин Ван almost 3 years
    hostPath is a thing.
  • opricnik
    opricnik almost 3 years
    hostPath maps files from the underlying host, not from your workstation.