Skip to content

This is simple docker image including docker itself and cron for running docker with cron.

Notifications You must be signed in to change notification settings

zalari/docker-docker-cron

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

docker-cron

This is docker image for running cronjobs via a container. Its main purpose is, to trigger other docker containers via cron.

A possible use case are backups - you want to have regular backups for you dockerized application stack with neither setting up a cronjob inside your database container nor setting up cron on the host.

With this container you can easily achieve this, by exposing the docker socket from the host to this container and setting up cronjobs via an environment variable.

Usage

For setting up cronjobs, you have to pass an environment variable DOCKER_CRONTAB to it - it can be _"multi-line", i.e. contain multiple entries by separating them with \n.

Although you can run arbitrary commands inside this container, you likely want to run some sort of docker command from inside the container communicating with the outside docker daemon, for this you have to pass the docket socket to it.

docker run --rm -v /var/run/docker.sock:/var/run/docker.sock:ro \
       -e DOCKER_CRONTAB=\
       '*/5       *       *       *       *       docker info > /cron-env/docker-info.txt' \
       zalari/docker-cron

with docker-compose

docker-compose is also conveniently provided in this image and this allows for adding cron functionality quite easily to your applications stacks:

  # ...

  #this is a special service for exporting something to stdout
  #to use: docker-compose run --rm -T dump_db_out > db_dump/dump.sql
  dump_db_out:
    image: mysql:5
    # usually you would want some more params for mysqldump
    command: mysqldump
    depends_on:
      - db
   
  
  cron:
    image: zalari/docker-cron
    volumes:
      - ./docker-compose.yml:/cron-env/project/docker-compose.yml
      - ./db_dump:/cron-env/project/db_dump
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - |
        DOCKER_CRONTAB=
        30       22       *       *       * cd /cron-env/project && docker-compose run --rm -T dump_db_out > db_dump/`date +"%m-%d-%y"`-project.sql
        30       22       *       *       * cd /cron-env/project/db_dump && find *.sql -mtime +30 -delete

  

Hints

The image is based on the rather latest alpine image and thus only BusyBox and ash are available for cronjobs; if you need more tooling, simply base your image off this one.

You can also use the default cronjobs from alpine if you either add something or bind-mount to the following dirs in the container:

  • /etc/periodic/15min
  • /etc/periodic/hourly
  • /etc/periodic/daily
  • /etc/periodic/weekly
  • /etc/periodic/monthly

For listing the actual crontab of the running container, simply run: docker exec container-name cat /var/spool/cron/crontabs/root

About

This is simple docker image including docker itself and cron for running docker with cron.

Resources

Stars

Watchers

Forks

Packages

No packages published