Nodejs API Cronjob node-schedule not running inside docker container

861 Views Asked by At

Created an API to run some scheduled jobs with Nodejs which is running in a docker container.

exports.createAutoJobs = async (req, res, next) => {
console.log("Request received")
    cron.schedule('* * * * *', () => {
        console.log('Running');
    });
}

The above code is printing "Running" for every minute But for the same code adding a custom time pattern is not working

exports.createAutoJobs = async (req, res, next) => {
console.log("Request received")
    cron.schedule('00 13 15 * *', () => {
        console.log('Running');
    });
}

The custom pattern is working if it is outside the container as a normal Nodejs application, but not inside the docker container. What is the way to get around it and run the cron jobs inside a docker container. Is there an alternative to this approach?

1

There are 1 best solutions below

1
On

In my case the problem was the base image. I used node:18-slim. After switching to node:18-alpine and checking container timezone (mounted a file with a time zone to my container) the problem was solved.