Docker Community Forums

Share and learn in the Docker community.

Cron service for Docker Cloud

(Leif Gensert) #1

Hello there,

I’m looking for a simple solution to backup my database to S3.

I already have a container that runs all the necessary commands (leifg/postgres-s3-backup).

The only thing missing is a container that schedules the redeploy of that container periodically.

I looked into these 2 projects but neither of them work for me (various errors when running the services). Furthermore they both seem very outdated:

My question is: does anyone have a different solution to this problem? Or is there any project I’m not aware of?

(Adiekmann) #2

I found it easiest to just keep backup containers running and performing backups periodically by runnig cron inside those containers. The corresponding Dockerfile looks somewhat like:

FROM debian

RUN apt-get install -y -qq cron && \
echo "30 2 * * 1-6 /usr/local/bin/ daily > /proc/1/fd/1 2>&1" > /var/spool/cron/crontabs/root && \
echo "30 2 * * 0 /usr/local/bin/ weekly > /proc/1/fd/1 2>&1" >> /var/spool/cron/crontabs/root && \
echo "" >> /var/spool/cron/crontabs/root && \
crontab /var/spool/cron/crontabs/root

This makes your backup strategy independant of the actual container orchestration used as an added bonus :wink:

(Sunrunner) #3

Cron stinks outside of single servers that are meant to run all the time. Its only really got one set of semantics.
run-at time . Cron not running at time and the job doesn’t run. There are other options that have different semantics too. What will help is to think of the backup as something with an API (easy to test and to make the job itself resilient against loss of an instance). Then think about how its going to be triggered on a schedule. Dirty option is cron that is told to call the API all day say once an hour and leave it up to the backup API itself to decide if it should be run. (This avoids the 'happens to be not-running when the job is scheduled to run issue). Or better look for a more container friendly scheduler that recovers correctly on start.

(Steffen Bleul) #4

Try a companion container that cron’s you container: blacklabelops/cronium

Firstly, run your container and keep it running:

docker run -d \
    --name databaseBackup \
    leifg/postgres-s3-backup \
    sh -c "while sleep 3600; do :; done"

Secondly, start cronium to cron trigger your scripts:

  docker run -d \
       -v /var/run/docker.sock:/var/run/docker.sock \
       -e "JOB1NAME=Job1" \
       -e "JOB1CONTAINER_NAME=databaseBackup" \
       -e "JOB1CRON=0 4 * * *" \
       -e "" \
       -e "JOB1SHELL_COMMAND=/bin/bash -c" \