Question

Cannot copy files from app directory to worker to setup cron jobs (App Platform)

Hello. I am running a Laravel app on DO App Platform. Love the service but wish cron was easier to implement. I have followed the steps in the below article, How To Setup a Job Scheduler on DigitalOcean App Platform:

https://docs.digitalocean.com/developer-center/how-to-setup-a-job-scheduler-on-digitalocean-app-platform/

I am having a hard time wrapping my mind around the worker, and how it gets access to to the main application component. My understanding is that I need to copy my Laravel app into the worker component, so that the worker can run the required php artisan schedule:work command:

https://www.digitalocean.com/community/questions/laravel-scheduled-tasks

This is the Dockerfile code I have been working with, modified from the examples linked above:

FROM ubuntu:22.04

# 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 php \
    # Remove package lists for smaller image sizes
    && rm -rf /var/lib/apt/lists/* \
    && docker-php-ext-install pdo pdo_mysql \

    # Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

## Set the working directory for the Laravel app
WORKDIR /workspace

# Copy the Laravel app into the container
COPY . /

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

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

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

# Run the entrypoint script
ENTRYPOINT ["/entrypoint.sh"]

# https://manpages.ubuntu.com/manpages/trusty/man8/cron.8.html
# -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"]

According to the build logs the issue arises when the code tries to adjust permissions to the /storage file, and it returns a no such file/directory error. I understand the path would need to be adjusted for cache, etc. as well.

I have never used a Dockerfile before, having been fortunate enough to always have access to a simple cron setup.

I THINK my problem is coming from the working directory I specify. I am guessing the WORKDIR I specify is nonexistent and I am copying nothing and so that is why there is no file to change permissions on. When I pwd in the console for the component the app is hosted on, I see I am in the /workspace directory, and I can see all my laravel application files in there. I do not understand how I can access those files to copy the project over into the worker so that I can run the artisan schedule:work commands. I am really struggling to understand the concepts here - can any one clarify? Thank you.


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
July 9, 2023
Accepted Answer

Hi there,

Indeed, you’re correct that the WORKDIR command in your Dockerfile is similar to the cd command in a shell. It changes the current working directory of the Docker container. The COPY command then copies the file/directory specified in the first argument from the host machine into the Docker container at the path specified in the second argument.

In your Dockerfile, you’re setting the WORKDIR to /workspace, and then you’re trying to copy everything from the host’s current directory into the root directory (/) of the container. Here’s where the problem likely occurs - you’re setting up a working directory but not copying your Laravel application into that working directory.

Here’s how you should modify the Dockerfile:

# ...

# Set the working directory for the Laravel app
WORKDIR /workspace

# Copy the Laravel app into the container
COPY . /workspace

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

# ...

In this updated Dockerfile, I’ve updated the COPY command to copy everything into /workspace, which is your working directory in the container. I’ve also updated the paths in the chown and chmod commands to point to the storage and bootstrap/cache directories in the /workspace directory.

Regarding the workers question, you can think of it as a completely separate instance of your Laravel app. You need to deploy your complete Laravel code in order to allow the worker to actually have access to it. A “worker” in the context of web applications generally refers to a background process that handles tasks that don’t need to be done in real-time, like sending emails, processing images, etc. In your case, your Laravel application is copied into this container so that the worker process can run Laravel’s artisan commands.

Let me know how it goes!

Best,

Bobby

Try DigitalOcean for free

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

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.