skip to Main Content

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?

4

Answers


  1. If you want to run scheduled task you might wanna use celery beat: https://docs.celeryq.dev/en/stable/userguide/periodic-tasks.html?highlight=periodic#starting-the-scheduler.

    You can define when each task should run in cron style.

    The decision where to run each task (on which worker) is done by celery router:
    https://docs.celeryq.dev/en/stable/userguide/routing.html

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
  3. Docker is supposed to run a single process on each container so a usual approach when you need cron-like tasks is to run cron on the host machine (or as a managed service like @Omkar Kulkani points) and then that “outside” launch the container running the command that you need.

    something like

    0,20,40 * * * * docker exec [container name] [your command here]

    Login or Signup to reply.
  4. We developed a simple container for scheduling jobs: amplication/scheduler it wraps an HTTP request with CRON.

    Usage:

    services:
      app:
         # ...
      scheduler:
        image: amplication/scheduler
        command: --request.url http://app/example-route --request.method POST --schedule '* * * * *'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search