Compose: Crontab can't execute docker-compose commands

Created on 30 Oct 2015  路  13Comments  路  Source: docker/compose

This shell script (as example) is never executed by crontab unless the docker-compose commands are commented. If is executed standalone it runs.

#!/bin/sh
# arg0 start-channel
# arg1 channel name

cd $HOME/$1
docker-compose ps
docker-compose up -d
docker-compose ps
exit 0
kinquestion

Most helpful comment

Exactly, using absolute paths (/usr/local/bin/docker-compose) inside shell scripts it runs.
Thanks!

All 13 comments

Had the same problem when adding docker-compose commands to crontab. Worked around by launching individual docker counterparts.

This is unlikely to be an issue with docker-compose. It's good practice to use absolute paths in crontabs because your $PATH may not be the same as an interactive shell. Maybe add set -x to see why it's failing?

Exactly, using absolute paths (/usr/local/bin/docker-compose) inside shell scripts it runs.
Thanks!

I Faced the same problem, simplest solution is to add path at beginning of crontab (note that last path is where docker-compose is) :
PATH=/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin

For anyone facing this issue, it's probably a small mistake. Try the following:

  • make sure that you cd to your (absolute) path to docker-compose.yml in your shell script before docker-compose
  • just use absolute paths everywhere

馃槂

Thank you for this post. I was struggling until I stumbled upon this post. I am using absolute path /usr/local/bin.

Thanks for this post guys this problem was killing me. This is what I had to do:

  1. Add /usr/local/bin to my crontab path (my path to docker-compose)
  2. I am using Centos. Had to set my shebang to #!/usr/bin/env bash in the sh script
  3. In the sh script I still had to cd to my script location where the docker-compose.yml file was, then run docker-compose pull
    cd /opt/mycompany/docker-compose/
    docker-compose pull

Exactly, using absolute paths (/usr/local/bin/docker-compose) inside shell scripts it runs.
Thanks!

thanks! works too when add absolute path like above !

for me a docker command was not working error: /bin/sh: 1: docker: not found.
so i ran which docker and got /snap/bin/docker. replaced docker with /snap/bin/docker in crontab

DC_NOT_DEFINED=1
if hash docker-compose 2>/dev/null; then
    DC_NOT_DEFINED=0
fi;

if [[ ${DC_NOT_DEFINED} > 0 ]]; then
    echo "* docker-compose not defined, make alias"
    docker-compose() {
        /usr/local/bin/docker-compose "$@"
    }
    export -f docker-compose

    docker-compose -v
fi;

If anyone is having issues with docker-compose run or docker-compose exec the -T flag did it for me:
/usr/local/bin/docker-compose exec -T web sh

For anyone facing this issue, it's probably a small mistake. Try the following:

  • make sure that you cd to your (absolute) path to docker-compose.yml in your shell script before docker-compose
  • just use absolute paths everywhere

馃槂

Thanks! 2 days debugging and this is works like a charm!

/usr/local/bin/docker-compose -f /your/docker-compose.yml/file/path exec -T web python3 script.py

Was this page helpful?
0 / 5 - 0 ratings