Search code examples
kubernetesdocker-volume

Unable to access volume content using initContainers


I have a simple image (mdw:1.0.0) with some content in it:

FROM alpine:3.9
COPY /role /mdw
WORKDIR /mdw

I was expecting that my container 'nginx' would see the content of /mdw folder, but there is no file.

apiVersion: v1
kind: Pod
metadata:
 name: init-demo
spec:
 initContainers:
 - name: install
   image: mdw:1.0.0
   imagePullPolicy: Never
   volumeMounts:
   - name: workdir
     mountPath: "/mdw"
 containers:
 - name: nginx
   image: nginx
   volumeMounts:
   - name: workdir
     mountPath: /mdw
   command: ["ls", "-l", "/mdw"]
 volumes:
 - name: workdir
   emptyDir: {}

Do you know what is the reason and how to fix it ?

Thank you very much


Solution

  • When mounting volume if directory already exists will get wiped. It's intentional and no fix really.

    Only way would be to populate the directory after mounting is done.