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 "run this Bash command" type deals.
package.json:
{
...
"scripts": {
"start": "node bin/path/to/index.js",
"compile": "tsc"
}
}
npm run compile && npm run start
works on the command-line. Moving on to the Docker container setup...
Dockerfile:
FROM node:18
WORKDIR /working/dir/
...
RUN npm run compile
CMD [ "npm", "run", "start" ]
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...
my-cron.yaml:
apiVersion: batch/v1
kind: CronJob
metadata:
name: cron-foo
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: job-foo
image: gcr.io/...
imagePullPolicy: IfNotPresent
restartPolicy: OnFailure
When I kubectl apply -f my-cron.yaml
sure enough I get some pods that run, one per-minute, however they all error out:
% kubectl logs cron-foo-27805019-j8gbp
> mdmp@0.0.1 start
> 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
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...
Any help would be appreciated. Most CronJob examples I've seen have a command:
list in the template spec...