run “docker run” from crontab

邮差的信 提交于 2021-01-26 03:58:03

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/31766116/run-docker-run-from-crontab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!