Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best approach for adding cron jobs (scheduled tasks) for a particular service in docker-compose

I'm using a docker-compose. I have a web and a worker service.

version: '3'
services:
  web:
    build: .
    environment:
      - "*"
    links:
      - redis
      - memcached
    ports:
      - "80:8001"
      - "443:8001"

  worker:
    build: .
    command: ["/bin/bash", "/home/django/start_celery.sh"]
    environment:
      - "*"
    links:
      - redis
      - memcached

  memcached:
    image: memcached
    ports:
      - "11211:11211"

  redis:
    image: redis
    ports:
      - "6379:6379"

I need to run crons (scheduled tasks) on worker service.

And I dont want to hardcode the crontab in Dockerfile as I'm using same dockerfile for both the services.

So what is the best approach for this?

like image 678
Roshan007 Avatar asked Nov 29 '18 10:11

Roshan007


1 Answers

You can try the following Opensource tool for scheduling crons in the docker-compose.

https://github.com/mcuadros/ofelia

eg:

 [job-service-run "service-executed-on-new-container"]
 schedule = 0,20,40 * * * *
 image = ubuntu
 network = swarm_network
 command =  touch /tmp/example

In case you are planning to utilize the image in any of the cloud platforms.

For Eg.

AWS: You can also have a look at ECS scheduler

https://docs.aws.amazon.com/AmazonECS/latest/developerguide/scheduled_tasks.html

GCP: Kubernetes Engine CronScheduler

https://cloud.google.com/kubernetes-engine/docs/how-to/cronjobs

like image 150
Omkar Kulkarni Avatar answered Sep 24 '22 16:09

Omkar Kulkarni