Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run "docker run" from crontab

Tags:

docker

cron

I try to make automated (every night at 4) backups from a postgresql database running inside a docker container.

#!/bin/sh

CONTAINER=`docker ps|grep name_of_container|awk '{print $1}'`
USER='postgre_user'
PASSWORD='changed'
BUDIR='/some/path/backup/'

docker run -it --link $CONTAINER:db -v $BUDIR:/backup/ -e "PGPASSWORD=$PASSWORD" pg_dump -h db -U $USER -Fc -f /backup/$(date +%Y-%m-%d-%H-%M-%S).dump

My crontab looks like this:

0 4 * * * /path/to/script.sh

The script works fine when I execute it manually and it also get executed from cron (I tried * * * * * for debugging).

I can't figure out how to make cron and the script work together. So far I tried:

  • write variables to log file
  • check output from crontab (* * * * * [...] &>cron.log)
  • check output from docker exec [...] > output.log in script

$CONTAINER contains the correct docker id when run from cron, cron.log and output.log are created but empty.

Any ideas?

like image 750
Martin Avatar asked Aug 01 '15 20:08

Martin


2 Answers

Can't use docker run -it --link [...] when running from cron - I use docker run --link [...] now.

like image 118
Martin Avatar answered Oct 21 '22 06:10

Martin


To elaborate on Martin's answer, -it is shorthand for -i -t, ie run interactively on terminal (pseudo TTY), so it's not necessary for running in a cronjob.

If the command is suitable for automation, -it should be not necessary, so removing it should let you run docker from a cron job.

like image 28
mahemoff Avatar answered Oct 21 '22 07:10

mahemoff