1

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...

codedread
  • 1,312
  • 11
  • 18
  • When you run the container using plain Docker, is it just plain `docker run gcr.io/...` with no other options? Have you `docker push`ed the latest working version of your image to GCR? – David Maze Nov 13 '22 at 11:49

1 Answers1

1

The error you show about the path not being found should appear when you docker run ... - but it didn't!

So, I assume it is related to the imagePullPolicy. 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 same tag, then don't forget to tell Kubernetes to query the registry download the new digest by changing the imagePullPolicy to Always.

Ryan
  • 36
  • 4