Need to run copy script that will copy some files to mounted path [init-container]

3/27/2019

I have tomcat as docker image. I have 3 xmls/property files to bring up the war in my tomcat I need to write init container which will

  1. have a script [shell or python]
  2. create a volume and mount it to the main container
  3. copy the property files from my local system to the mounted volume.
  4. then init-container finishes app container starts after this.

for example: on my local I have the following:

/work-dir tree
├── bootstrap.properties
├── index.html
├── indexing_configuration.xml
├── repository.xml
└── wrapper.sh

init container should run a script wrapper.sh to copy these
files into the mounted volume on app container which is /usr/share/jack-configs/

-- Tuhin Subhra Mandal
kubernetes

1 Answer

3/27/2019

You have to create a volume and mount on both containers. On Init container you run the script to copy the files to the mounted volume.

Instead of using a local file, I would suggest you use a blob storage to copy you files over, will make it much more simple.

This docs shows how to do what you want.

An example YAML is the following:

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
    - "-O"
    - "/work-dir/index.html"
    - http://kubernetes.io
    volumeMounts:
    - name: workdir
      mountPath: "/work-dir"
  dnsPolicy: Default
  volumes:
  - name: workdir
    emptyDir: {}

To accomplish what you want, you have to change the command in the init container to execute your script, this bit I leave you try.

PS: If you really want to copy from a local(node) filesystem, you need to mount another volume to the init container and copy from one volume to another

-- Diego Mendes
Source: StackOverflow