Home > Back-end >  Run a docker container at a specific time on the cloud
Run a docker container at a specific time on the cloud

Time:12-15

The title again. The problem is that the I fetch the date from an API and once the container is run on that date, I fetch the next date on which I want to run the container on. Is there any cloud service on any cloud platform which provides this service?

CodePudding user response:

if you want to run a specific logic(piece of code) at specific time then go with server-less application, almost all cloud provider provide sever-less implementation with different name.

like AWS, they have lambda in place to provide server-less capability. great way to save a cost and you don't have to maintain your resources.

now coming to your question if you choose aws platform then write your piece of code in lambda function and then trigger/run that function with cloudWatch event.(trigger at specific time).

best of luck.

CodePudding user response:

You are looking for container orchestration. One option is to use ECS on AWS, but a simpler option would be Lambda (with Docker) Eventbrdige. You could also optionally use Cloudwatch instead of event bridge, but afaik EventBrdige is the improved way of doing pretty much what you could do with Cloudwatch, however, there are some differences - https://aws.amazon.com/blogs/compute/upgrading-to-amazon-eventbridge-from-amazon-cloudwatch-events/

You could go through the ECS route, but it will be a bit more complex and time-consuming and if Lambda serves your purpose, then that would be much more convenient for you.

Lambda also supports Docker images, or you could code in the language of your choice.

Also, these services are specific to AWS, but pretty much all cloud hosting providers, and a lot of third-party SaaS applications would provide similar functionalities if you are interested.

CodePudding user response:

maybe you wanna give "Kubernetes CronJobs" a try.

A daily job for your container could look like this :

apiVersion: batch/v1beta1            ## The version of the Kubernetes API
kind: CronJob                        ## The type of object for Cron jobs
metadata:
  name: cron-test
spec:
  schedule: "0 12 */1 * *"            ## Defined schedule using the *nix style cron syntax
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: cron-test
            image: YOUR_IMAGE            ## Image used
            args:
           - /bin/sh
            - -c
            - date; echo Hello this is Cron test
          restartPolicy: Never

Full details can be found on Kubernetes CronJob Webpage

CodePudding user response:

One way to go about this is:

  1. Have a container in ECS.
  2. Create EventBridge Rule.
  3. As you fetch the date from API, update the EventBridge Rule schedule.
  4. EventBridge Rule is triggered at scheduled time. Container is run.

Programmatically repeat 3-4.

For eg., updating the EventBridge Rule through

  1. AWS CLI: aws events put-rule --name <RULE_NAME> --schedule-expression <CRON_VALUE> --role-arn <IAM_ROLE_ARN>

You can have the cron value easily this way:

#!/bin/bash
_DATESTR='2018-05-15 17:30:00'
echo "date format: ${_DATESTR}"
echo "cron format: ${_DATESTR:14:2} ${_DATESTR:11:2} ${_DATESTR:8:2} ${_DATESTR:5:2} *"

# output:
# $ date format: 2018-05-15 17:30:00
# $ cron format: 30 17 15 05 *

### Code credit to Leslie: https://stackoverflow.com/questions/50349770/date-format-to-cron-dormat-conversion

NOTE: This is not a good practice as EventBridge will be a serious limiting factor - only invoking the execution of container at a desired time, one at a time. This limits running to a single container invocation.

If you need a better concurrent scheduling method you can have Docker setup in EC2 and use at command to schedule. See example: https://www.redhat.com/sysadmin/single-use-cron

  • Related