Search code examples
crondocker

run "docker run" from crontab


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?


Solution

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