问题
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