getting a No such file or directory error with Kubernetes cronjob and dockerfile

5/5/2021

I have a script that I want to run using a dockerfile that looks like this:

FROM debian:unstable-slim

RUN apt-get update && apt-get install -y netcat-traditional httpie postgresql-client

COPY go.sh /tmp/go.sh

WORKDIR /tmp
CMD ["bash", "-c", "/tmp/go.sh"]

It works fine on local, but I need to deploy it as a kubernetes cronjob, I have a config file with something like this:

script:
- docker build -t {github_enterprise_url/org/repo} .

And the build output seems ok:

...
Step 3/7 : COPY go.sh /tmp/go.sh
 ---> ab8d5227f65e
...
Successfully built e79162d90def

And for the cronjob yaml, among other things, I have something like this:

spec:
  containers:
    - image: >-
        ${ trigger.artifacts.?[type == 'docker'].?[name matches
        'org/repo'].![reference][0] }

the deploy then succeded but with this message:

Cannot determine if job needs to be started: Too many missed start time (> 100). Set or decrease .spec.startingDeadlineSeconds or check clock skew.

So I tried to run it manually like this:

kubectl create job --from=cronjob/name name-manual-123

And when looking at the logs for the pod created, I get this:

kubectl logs name-manual-123-ab1cd
bash: line 1: /tmp/go.sh: No such file or directory

any clue?

-- anfbar
docker
dockerfile
kubernetes
kubernetes-cronjob

1 Answer

5/5/2021

The /tmp directory is generally intended for temporary file storage. It's common to mount a RAM disk (tmpfs) there, and it's possible the Kubernetes setup does this without you noticing it.

You can address this by storing your script in a location that's on the default PATH, like /usr/local/bin:

<!-- language: lang-sh -->
# /usr/local/bin, not /tmp
COPY go.sh /usr/local/bin

CMD ["go.sh"]
# will work when the script is executable; on $PATH (in /usr/local/bin);
# and has a correct shebang line
# #!/bin/sh
-- David Maze
Source: StackOverflow