<p>I have a small NodeJS script that I want to run inside a container inside a kubernetes cluster as a CronJob. I'm having a bit of a hard time figuring out how to do that, given most examples are simple &quot;run this Bash command&quot; type deals.</p>
<p>package.json:</p>
<pre><code>{
  ...
  &quot;scripts&quot;: {
    &quot;start&quot;: &quot;node bin/path/to/index.js&quot;,
    &quot;compile&quot;: &quot;tsc&quot;
  }
}
</code></pre>
<p><code>npm run compile &amp;&amp; npm run start</code> works on the command-line. Moving on to the Docker container setup...</p>
<p>Dockerfile:</p>
<pre><code>FROM node:18
WORKDIR /working/dir/
...
RUN npm run compile

CMD [ &quot;npm&quot;, &quot;run&quot;, &quot;start&quot; ]
</code></pre>
<p>When I build and then docker run this container on the command-line, the script runs successfully. This gives me confidence that most things above are correct and it must be a problem with my CronJob...</p>
<p>my-cron.yaml:</p>
<pre><code>apiVersion: batch/v1
kind: CronJob
metadata:
  name: cron-foo
spec:
  schedule: &quot;* * * * *&quot;
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: job-foo
            image: gcr.io/...
            imagePullPolicy: IfNotPresent
          restartPolicy: OnFailure
</code></pre>
<p>When I <code>kubectl apply -f my-cron.yaml</code> sure enough I get some pods that run, one per-minute, however they all error out:</p>
<pre><code>% kubectl logs cron-foo-27805019-j8gbp

&gt; mdmp@0.0.1 start
&gt; node bin/path/to/index.js

node:internal/modules/cjs/loader:998
  throw err;
  ^

Error: Cannot find module '/working/dir/bin/path/to/index.js'
    at Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
    at Module._load (node:internal/modules/cjs/loader:841:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Node.js v18.11.0
</code></pre>
<p>The fact that it's trying to run the correct command means the correct Docker container is being pulled successfully, but I don't know why the script is not being found...</p>
<p>Any help would be appreciated. Most CronJob examples I've seen have a <code>command:</code> list in the template spec...</p>

codedread
<p>The error you show about the path not being found should appear when you <code>docker run ...</code> - but it didn't!</p>
<p>So, I assume it is related to the <code>imagePullPolicy</code>. Something is fixed, checked locally and then re-pushed to the given registry for your Kubernetes workloads to use. If it was re-pushed with the <strong>same</strong> tag, then don't forget to tell Kubernetes to query the registry download the new digest by changing the <a href="https://kubernetes.io/docs/concepts/containers/images/#image-pull-policy" rel="nofollow noreferrer"><code>imagePullPolicy</code></a> to <code>Always</code>.</p>
