Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

crontab via a docker container

Hello,

I have a Laravel / VueJS application in a docker container.

I need to run cron jobs, so I have created another container with Alpine.

FROM alpine:3.20.1

COPY cron/crontab /etc/crontabs/root

RUN crontab /etc/crontab
RUN mkdir -p /var/log/cron

CMD ["/usr/sbin/crond", "-f"]
cron:
  build:
    context: .
    dockerfile: cron.dockerfile
  container_name: my-cron-container
  volumes:
    - ./src:/var/www/html
  depends_on:
    php:
      condition: service_started
    soketi:
      condition: service_healthy
    vendor:
      condition: service_started
  restart: always

// And here is my crontab content

* * * * * cd /var/www/html && php /var/www/html/artisan schedule:run >> /dev/null 2>&1

When I do docker-compose run cron sh, I see the crond in action.

crond[11]: USER root pid  16 cmd cd /var/www/html && php /var/www/html/artisan schedule:run >> /dev/null 2>&1
crond[11]: USER root pid  17 cmd cd /var/www/html && php /var/www/html/artisan schedule:run >> /dev/null 2>&1
crond[11]: USER root pid  18 cmd cd /var/www/html && php /var/www/html/artisan schedule:run >> /dev/null 2>&1
crond[11]: USER root pid  19 cmd cd /var/www/html && php /var/www/html/artisan schedule:run >> /dev/null 2>&1
crond[11]: USER root pid  20 cmd cd /var/www/html && php /var/www/html/artisan schedule:run >> /dev/null 2>&1

But sure it doesn't execute php /var/www/html/artisan schedule:run. Why ?

If I run php -v from the shell, php is not found. php is in another container, but it should work, no ?

Why does my code not work ?

Can you help me please ?

Thanks a lot.

Vincent

0 likes
3 replies
vincent15000's avatar
vincent15000
OP
Best Answer
Level 63

I have found the solution, in fact it was very easy.

The docker image php:8.3-fpm-alpine has already cron installed by default.

So I had just to use this image and copy the cron configuration.

Arsentiy Zhunussov's avatar

Common UNIX utils, such as cron, already installed in busybox (Docker's library), which included all distro images on Docker Hub.

1 like

Please or to participate in this conversation.