How to run a cron job inside a docker container?

前端 未结 20 1307
無奈伤痛
無奈伤痛 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:20

    Cron jobs are stored in /var/spool/cron/crontabs (Common place in all distros I Know). BTW, You can create a cron tab in bash using something like that:

    crontab -l > cronexample
    echo "00 09 * * 1-5 echo hello" >> cronexample
    crontab cronexample
    rm cronexample
    

    This will create a temporary file with cron task, then program it using crontab. Last line remove temporary file.

    0 讨论(0)
  • 2020-11-22 06:22

    For those who wants to use a simple and lightweight image:

    FROM alpine:3.6
    
    # copy crontabs for root user
    COPY config/cronjobs /etc/crontabs/root
    
    # start crond with log level 8 in foreground, output to stderr
    CMD ["crond", "-f", "-d", "8"]
    

    Where cronjobs is the file that contains your cronjobs, in this form:

    * * * * * echo "hello stackoverflow" >> /test_file 2>&1
    # remember to end this file with an empty new line
    
    0 讨论(0)
  • 2020-11-22 06:22

    What @VonC has suggested is nice but I prefer doing all cron job configuration in one line. This would avoid cross platform issues like cronjob location and you don't need a separate cron file.

    FROM ubuntu:latest
    
    # Install cron
    RUN apt-get -y install cron
    
    # Create the log file to be able to run tail
    RUN touch /var/log/cron.log
    
    # Setup cron job
    RUN (crontab -l ; echo "* * * * * echo "Hello world" >> /var/log/cron.log") | crontab
    
    # Run the command on container startup
    CMD cron && tail -f /var/log/cron.log
    

    After running your docker container, you can make sure if cron service is working by:

    # To check if the job is scheduled
    docker exec -ti <your-container-id> bash -c "crontab -l"
    # To check if the cron service is running
    docker exec -ti <your-container-id> bash -c "pgrep cron"
    

    If you prefer to have ENTRYPOINT instead of CMD, then you can substitute the CMD above with

    ENTRYPOINT cron start && tail -f /var/log/cron.log
    
    0 讨论(0)
  • 2020-11-22 06:22

    Though this aims to run jobs beside a running process in a container via Docker's exec interface, this may be of interest for you.

    I've written a daemon that observes containers and schedules jobs, defined in their metadata, on them. Example:

    version: '2'
    
    services:
      wordpress:
        image: wordpress
      mysql:
        image: mariadb
        volumes:
          - ./database_dumps:/dumps
        labels:
          deck-chores.dump.command: sh -c "mysqldump --all-databases > /dumps/dump-$$(date -Idate)"
          deck-chores.dump.interval: daily
    

    'Classic', cron-like configuration is also possible.

    Here are the docs, here's the image repository.

    0 讨论(0)
  • 2020-11-22 06:23

    So, my problem was the same. The fix was to change the command section in the docker-compose.yml.

    From

    command: crontab /etc/crontab && tail -f /etc/crontab

    To

    command: crontab /etc/crontab

    command: tail -f /etc/crontab

    The problem was the '&&' between the commands. After deleting this, it was all fine.

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题