How to run a cron job inside a docker container?

前端 未结 20 1653
無奈伤痛
無奈伤痛 2020-11-22 05:36

I am trying to run a cronjob inside a docker container that invokes a shell script.

Yesterday I have been searching all over the web and stack overflow, but I could

20条回答
  •  借酒劲吻你
    2020-11-22 06:24

    There is another way to do it, is to use Tasker, a task runner that has cron (a scheduler) support.

    Why ? Sometimes to run a cron job, you have to mix, your base image (python, java, nodejs, ruby) with the crond. That means another image to maintain. Tasker avoid that by decoupling the crond and you container. You can just focus on the image that you want to execute your commands, and configure Tasker to use it.

    Here an docker-compose.yml file, that will run some tasks for you

    version: "2"
    
    services:
        tasker:
            image: strm/tasker
            volumes:
                - "/var/run/docker.sock:/var/run/docker.sock"
            environment:
                configuration: |
                    logging:
                        level:
                            ROOT: WARN
                            org.springframework.web: WARN
                            sh.strm: DEBUG
                    schedule:
                        - every: minute
                          task: hello
                        - every: minute
                          task: helloFromPython
                        - every: minute
                          task: helloFromNode
                    tasks:
                        docker:
                            - name: hello
                              image: debian:jessie
                              script:
                                  - echo Hello world from Tasker
                            - name: helloFromPython
                              image: python:3-slim
                              script:
                                  - python -c 'print("Hello world from python")'
                            - name: helloFromNode
                              image: node:8
                              script:
                                  - node -e 'console.log("Hello from node")'
    

    There are 3 tasks there, all of them will run every minute (every: minute), and each of them will execute the script code, inside the image defined in image section.

    Just run docker-compose up, and see it working. Here is the Tasker repo with the full documentation:

    http://github.com/opsxcq/tasker

提交回复
热议问题