Not able to access file shared with init container

10/15/2020

I am creating a file in init container and wanted to consume this file in main container.

  containers:
  - name: test1
    imagePullPolicy: Always
    image: newbusybox
    command:
    - "some command  --from-file=tmp/file.txt"
    volumeMounts:
    - name: workdir
      mountPath: /tmp
  initContainers:
  - name: install
    image: busybox
    command: ["/bin/sh", "-c"]
    args: ["echo test  > /pod-data/file.txt]
    volumeMounts:
    - name: workdir
      mountPath: /pod-data/
  volumes:
  - name: workdir
    emptyDir: {}

Pods goes into crashlookBack off with following error

Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"some command  --from-file=tmp/file.txt\": stat some comamnd  --from-file=tmp/file.txt: no such file or directory": unknown

I have verified file is mounted at tmp location.

-- pythonhmmm
google-kubernetes-engine
kubernetes

1 Answer

10/19/2020

I'm not exactly sure what you are trying to accomplish but I need to agree with a comment provided by user @Lauri Koskela:

Can you try to change the command fields to work like this? kubernetes.io/docs/tasks/inject-data-application/… The error message indicates that the error is not in the file sharing, and rather in the command specification

The link that @Lauri Koskela posted:

Could shed some light on ways to parse the commands into the Pods.

Also you have a typo in:

  • args: ["echo test > /pod-data/file.txt]
  • args: ["echo test > /pod-data/file.txt"] # <-- missing closing (")

Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"some command  --from-file=tmp/file.txt\": stat some comamnd  --from-file=tmp/file.txt: no such file or directory": unknown

Error above is connected with the fact that you tried to run a command without specifying a shell (it's also connected with a comment you made). You can read more about it here:

If you were to:

    command: ["/bin/sh", "-c"] 
    args: 
    - "some command  --from-file=/tmp/file.txt"

You should be able to run some command

Following this path, here is additional example:

apiVersion: v1
kind: Pod
metadata:
  name: busybox-pod
  labels:
    app: busybox
spec:
  containers:
  - name: busybox
    imagePullPolicy: Always
    image: busybox
    command: ["/bin/sh", "-c"]
    args: 
    - "cat /tmp/file.txt > /working.txt && sleep 3600"
    # - "cat /tmp/file.txt > /working.txt; sleep 3600"
    volumeMounts:
    - name: workdir
      mountPath: /tmp
  initContainers:
  - name: install
    image: busybox
    command: ["/bin/sh", "-c"]
    args: ["echo test > /pod-data/file.txt"]
    volumeMounts:
    - name: workdir
      mountPath: /pod-data/
  volumes:
  - name: workdir
    emptyDir: {}

This example will:

  • Run an initContainer with busybox image and will save "test" text to a file named file.txt in /pod-data/ directory
  • Run main container and will cat the content of earlier created file now being in /tmp to a working.txt file. It will also sleep for 3600 seconds.

After running this container you can check if the file was read correctly by:

  • $ kubectl exec -t busybox-pod -- cat /working.txt
hello
-- Dawid Kruk
Source: StackOverflow