0

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
  • 833
  • 1
  • 18
  • 38
  • 1
    Can you try to change the `command` fields to work like this? https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#run-a-command-in-a-shell The error message indicates that the error is not in the file sharing, and rather in the command specification – Lauri Koskela Oct 15 '20 at 08:12
  • I think your just missing a forward slash at the start (`--from-file=/tmp/file.txt` instead of `--from-file=tmp/file.txt`) – Tom Klino Oct 15 '20 at 08:43
  • Ok I tried that too. Same issue with --------------------------------------------------------------------------------------------------------------------------------------- image: "ubuntu:14.04" command: - touch tmp.txt --------------------------------------------------------------- Error: failed to start container "my-container": Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"touch tmp.txt\": executable file not found in $PATH": – pythonhmmm Oct 15 '20 at 08:58
  • Please try to use in initContainer -> `command: ["bin/sh"] args: ["-c", "echo hello > /pod-data/hello.txt"]` . And in the main container: `command: [/bin/sh] args: ["-c","cat /tmp/hello.txt; sleep infinity"]` – Dawid Kruk Oct 15 '20 at 16:51

1 Answers1

0

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
  • 8,982
  • 2
  • 22
  • 45