-1

Im trying to prevent PHP Composer to install it's dependencies everytime I edit my src dir files like this in my Dockerfile.

# Change working directory.
WORKDIR /var/www
# Install composer.
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Copy composer files here to prevent running composer install again if source files have been edited.
COPY ./src/composer.json ./src/composer.lock ./
# run composer install but oh no the "post-autoload-dump" script gets called 
# and it needs some laravel files (artisan for example) that haven't been copied yet (happends in next step)
RUN composer install --no-dev --optimize-autoloader --no-interaction --prefer-dist
# Copy Laravel source files.
COPY ./src .

Now this will give me the following error I was talking about in my Dockerfile code comments

Could not open input file: artisan

This happends because "php artisan package:discover" is called after Composer is done installing.

I tried to fix it like this:

COPY ./src/composer.json ./src/composer.lock ./
# Copy files that are needed to run the post-autoload-dump script after composer isntall.
COPY ./src/routes ./routes
COPY ./src/artisan .
COPY ./src/bootstrap ./bootstrap
RUN composer install --no-dev --optimize-autoloader --no-interaction --prefer-dist

This fixes the error but obviously it will break the Docker caching layers because if I change something in routes dir composer install will run again.

I also tried the following by removing the "post-autoload-dump" script in composer and instead add it after I copy my source files in the Dockerfile

RUN php artisan package:discover

But as you can guess, this wil also run everytime something in my src dir changes.

Is there a clean way that will only install Composer dependencies if I need it to, but skip the install step and post-autoload-dump script if I am just working in my src dir?

yivi
48.2k18 gold badges133 silver badges158 bronze badges
asked May 15, 2025 at 21:43
1
  • My personal take would be to NOT run composer on the building phase. I am not sure if you are using Docker Compose or just plain Docker, but if anything fails on composer (PHP) or your PHP project (using composer), then the build fails, so it makes no sense to have it. And you can achieve faster and better caching related to installing composer by just using the docker image, instead of having RUN curl ...., do COPY --from=composer:latest /usr/bin/composer /usr/bin/composer Commented May 16, 2025 at 12:03

1 Answer 1

-1

To properly cache Docker layers in your Laravel app, you want to avoid re-running composer install every time a file changes. The trick is to separate the steps so Docker can cache the dependency install.

Something like this:

WORKDIR /var/www
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Copy only composer files first
COPY ./src/composer.json ./src/composer.lock ./
# Install dependencies without running scripts (avoids errors)
RUN composer install --no-dev --optimize-autoloader --no-interaction --prefer-dist --no-scripts
# Now copy the rest of the app
COPY ./src .
# Manually run the necessary scripts after all files are there
RUN php artisan package:discover --ansi
answered May 15, 2025 at 21:47
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.