John Davidson

laravel - composer install not working php-alpine docker

0 comments
Message:


I am using php FPM alpine container with nginx alpine container for my laravel application.


Here is my Dockerfile,


FROM php:7.4-fpm-alpine
WORKDIR /usr/src/app

# install composer
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
RUN php composer-setup.php --install-dir=/usr/local/bin --filename=composer
RUN php -r "unlink('composer-setup.php');"

# copy files
COPY ./ /usr/src/app/

# install packages
ENV COMPOSER_ALLOW_SUPERUSER 1
RUN composer install

Here is my docker-compose file,


version: "3.7"
services:
web:
image: nginx:1.19-alpine
ports:
- 80:80
volumes:
- ../api:/usr/src/app
- ./site.conf:/etc/nginx/conf.d/site.conf
depends_on:
- api

api:
build: ../api
volumes:
- ../api:/usr/src/app

When I run docker-compose up --build the container build process works as you can see in the attached screenshot but the vendor directory was never created by the composer!?



Is it a known issue while using PHP FPM Alpine containers?


Also, when I access into the container using this command


docker exec -it docker_api_1 /bin/ash

and run composer install manually then everything works. Any suggestions are appreciated.


This is the error message when i visit the site. It can't find the composer autoload.php file,



My Answer:
It seems like the issue might be related to the order in which the files are being copied and the composer install command is being run in your Dockerfile.

One possible solution is to move the `composer install` command after the `COPY` command in your Dockerfile. This way, the composer install command will be run after all the files are copied into the container.

Here is an updated version of your Dockerfile:

Dockerfile
FROM php:7.4-fpm-alpine
WORKDIR /usr/src/app

# install composer
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
RUN php composer-setup.php --install-dir=/usr/local/bin --filename=composer
RUN php -r "unlink('composer-setup.php');"

# copy files
COPY ./ /usr/src/app/

# install packages
ENV COMPOSER_ALLOW_SUPERUSER 1

# Run composer install after copying files
RUN composer install



Try rebuilding your containers with this updated Dockerfile and see if it resolves the issue. This should ensure that the composer install command is run after all the files are copied into the container.

If the issue persists, you can also try running `composer install` manually inside the container after it has started, as you mentioned that it works when done manually.

I hope this helps! Let me know if you have any other questions.

Rate this post

5 of 5 based on 3705 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us