Reputation: 161
I recently have a problem when I want to execute docker-compose command in crontab.
I have a docker-compose YAML file that defined all the services I need, say “docker-compose.yml". And I also have a Makefile in which I had written some command to do something.
My makefile is:
.PHONY operate
operate:
/usr/local/bin/docker-compose -p /project -f ~/docker-compose-production.yml run rails env
This make script worked fine when it executed in shell. It listed all the environment var I defined in docker-compose.yml. But when I putted it in crontab.
The result became strange, it listed nothing but only the $PATH
.
My crontab file is:
57 21 * * * make -f ~/Makefile operate >~/temp 2>&1
I guess there must be some environment var that docker-compose must have but I don’t know. Do you have any idea about this problem?
Upvotes: 15
Views: 42280
Reputation: 6926
With newer versions of docker, docker-compose
got merged with main docker
command, so I do this:
0 3 * * * /usr/bin/docker compose -f /home/nuc/docker/docker-compose.yaml exec containername command >/dev/null 2>&1
Upvotes: 0
Reputation: 1001
my solution in crontab:
*/1 * * * * /usr/local/bin/docker-compose -f /home/mypath/docker-compose.yml exec php ls >/dev/null 2>&1
my command: ls
my service container: php
Upvotes: 4
Reputation: 169
I had an issue with a bash script with docker-compose exec command inside the script and this could be related to the problem.
The script was running fine when it was launched from the terminal but it was returning an error code when the script was launched by CRON.
I solved the issue by adding -T after docker-compose exec in the bash script :
docker-compose exec -T
–T Disable pseudo-tty allocation. By default docker-compose exec allocates a TTY.
Upvotes: 8
Reputation: 1087
you can use
10 3 * * 0 /usr/local/bin/docker-compose -f /www/ilanni.com/docker-compose.yml start > /dev/null
Upvotes: 15
Reputation: 251
for laravel you can try:
docker exec -i php php /var/www/html/artisan schedule:run
Upvotes: 0
Reputation: 546
I am running docker-compose on Windows 10 with WSL(Windows subsystem for linux) crontab. Note: Got Docker Desktop for Windows and WSK working flawlesly using https://nickjanetakis.com/blog/setting-up-docker-for-windows-and-wsl-to-work-flawlessly
Documenting how I got docker-compose commands to run, for anyone else who may run into the same issue.
# Setting environment for Docker
DOCKER_HOST=tcp://localhost:2375
# m h dom mon dow command
01 01 * * * cd /d/my_docker_proj_dir && /home/my_user/.local/bin/docker-compose exec -d container bash -c "full-path-to-command args" 1> /tmp/cron.log 2> /tmp/cron.log
Upvotes: 1
Reputation: 11
For me the problem was not having the path set correctly. Check that your PATH environment var in crontab is the same to that of your current user.
Upvotes: 1
Reputation: 13604
You won't need any environment variables to drive Docker Compost that you aren't already using for Docker. Most of those are unneeded unless you are connecting to a remote Docker host.
What I suspect is that cron is executing as a different user with a different set of permissions that doesn't have access to the Docker socket. Maybe you can add a line to your Makefile in order to debug this. You can use the 'whoami' command to output the name of the current user.
Upvotes: 2