Laravel scheduled tasks


I’m trying to setup scheduled tasks for Laravel. I did follow this guide: but it seems I have no access to my Laraval app code so I don’t understand how I’m supposed to run the php artisan schedule:run command. On your documentation, you mention a curl http://sample-nodejs:8080 example. Does it mean that the app can only be accessed via a private URL?


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
April 20, 2023

Hi there,

The documentation that you’ve shared gives a Node.js sample app as an example. In order for this to work, you need to get the files from the docker-cron repo and use the Dockerfile from the example repo as a blueprint and adjust it for your Laravel project.

That way, rather than running the example curl command from the demo Node app, the Worker component will clone your Laravel project and will have access to all of the necessary files.

Here is an example of an extended Dockerfile which might need some adjustments based on your app:

# Use the official PHP image as the base image
FROM php:8.0-fpm

# Install necessary packages for Laravel, including cron and unzip
RUN apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get -y --no-install-recommends install -y cron curl unzip libpq-dev \
    # Remove package lists for smaller image sizes
    && rm -rf /var/lib/apt/lists/* \
    && docker-php-ext-install pdo pdo_mysql \
    # Install Composer
    && curl -sS | php -- --install-dir=/usr/local/bin --filename=composer

# Set the working directory for the Laravel app
WORKDIR /var/www

# Copy the Laravel app into the container
COPY . /var/www

# Install the Laravel app dependencies
RUN composer install

# Set proper permissions for the storage and bootstrap/cache directories
RUN chown -R www-data:www-data /var/www/storage /var/www/bootstrap/cache \
    && chmod -R 755 /var/www/storage /var/www/bootstrap/cache

# Copy the crontab file and entrypoint script into the container
COPY crontab /hello-cron

# Install the crontab and make the entrypoint script executable
RUN crontab /hello-cron \
    && chmod +x /

# Run the entrypoint script

# -f | Stay in foreground mode, don't daemonize.
# -L loglevel | Tell  cron  what to log about jobs (errors are logged regardless of this value) as the sum of the following values:
CMD ["cron", "-f", "-L", "2"]

An alternative option is to use the suggestion here with a standard worker and a while loop:

Let me know how it goes!



Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up