3

I have a NodeJS app consisting of a REST API and an overnight maintenance (cron) job. Currently running on Debian Linux.

What is the best practice do Dockerize it?

  • I can use the official "node" Docker image, however that doesn't contain a crontab.
  • I can use the official "alpine" Docker image (and install NodeJS in it) however I lose the possibility of upgrading NodeJS with the easy of pulling a new version of the official image.

What is the best way to achieve this?

1
  • Have you found a solution adding cronjobs with this docker image?
    – Zac
    Nov 23, 2020 at 20:05

2 Answers 2

2

Solution #1

Use a node cron package. I added some code example from a real project. Every hour I scrape a website. You can set time and place your recurring task inside cron.schedule. You can use it a separate project or combine with your api code.

import { Scraper } from './controller/scraper.js';
import cron from 'node-cron'

cron.schedule('0 0 */1 * * *', () => {
  console.log('running a task every hour');
  const scraper = new Scraper()
  const data = scraper.getData()
});

Solution #2

Docker official website doesn't advice running process managers with containers. But you can use if you need. I use pm2 as a process manager. It can be used for running an app for some intervals and it can restart your app if it crashes.

Links that will help you:

My use case:

  • Restart app if it crashes.
  • Run app.js file for each hour.
2
  • 1
    I actually like the idea of node-cron, it will let me use the exact same parent image from my server and scheduler. I give it a try, thanks! May 25, 2021 at 8:42
  • Thanks for sharing about node-cron ;-)
    – intumwa
    Apr 23, 2022 at 2:57
-1

Use official nodejs docker image and build docker image with your code.

You need to run two containers, one for Rest API and other for cron job.

For cron job you dont need conrtab inside the docker image but schedule the image run on the host machine like below:

This will ensure to run docker image at required interval.

crontab -e
* */2 * * * docker run -it app/cron-image:v1
6
  • Thanks, but I think it's better to avoid scheduling a cron job on the host machine because then in case of moving the docker container on another machine someone will need to reconfigure cron on the new host. I'd also like to mention that cron should execute a javascript (maintenance) job from inside of the NodeJS container. Hope this is understandable. Apr 15, 2019 at 15:21
  • Yes, then you can use k8s jobs that will provide more flexibility. Apr 16, 2019 at 4:14
  • Could you describe your suggestion more in detail, maybe with some examples? Apr 16, 2019 at 16:39
  • Create one docker image from node image as base. This new image is self sufficient for rest app and cron job. Now deploy rest app as k8s deployment that can serve the traffic. For cron job, setup k8s cron job with desired scheduling. kubernetes.io/docs/concepts/workloads/controllers/cron-jobs Apr 18, 2019 at 4:53
  • Thanks Aakash but I don't use Kubernetes, I just use Docker + docker-compose. Apr 19, 2019 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.