composer install not executed by docker build - laravel

I'm trying to build a laravel image with docker build. Here is my dockerfile
FROM php:7.1.14-fpm
WORKDIR /app
COPY . /app
COPY ./entrypoint.sh /tmp
RUN touch ./resources/assets/less/_main_full/main.less \
&& mv ./.env.local ./.env \
&& mv ./.dockerignore-local ./.dockerignore \
&& apt-get update -y && apt-get install -y openssl zip unzip git npm \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-dev \
libmagickwand-dev vim --no-install-recommends \
&& apt-get purge --auto-remove -y g++ \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install pdo pdo_mysql mbstring zip -j$(nproc) iconv mcrypt -j$(nproc) gd
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash \
&& export NVM_DIR="$HOME/.nvm" \
&& [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm \
&& nvm install node \
&& npm cache clean -f && npm install -g n && n stable && npm install cross-env && npm install && npm run dev
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
&& composer install --no-interaction
RUN chown -R www-data:www-data \
/app/storage \
/app/bootstrap/cache \
&& chmod 755 /tmp/entrypoint.sh
CMD ["/tmp/entrypoint.sh"]
but when I execute it with:
docker build -f laravel-local.dockerfile . -t xoco/kendozone:local-1.0.0 --no-cache
Composer install is not doing anything:
Step 7/9 : RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && composer install --no-interaction
---> Running in 90d05a566269
All settings correct for using Composer
Downloading...
Composer (version 1.6.3) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Package phpoffice/phpexcel is abandoned, you should avoid using it. Use phpoffice/phpspreadsheet instead.
Generating autoload files
> Illuminate\Foundation\ComposerScripts::postInstall
> php artisan optimize
Removing intermediate container 90d05a566269
---> 7665c4fefb57
I don't understand this behaviour. I have a .dockerignore that has vendor folder inside, so I don't know why shouldn't it install composer dependecies:
vendor/
node_modules/
# Laravel 5 & Lumen specific
public/storage
storage/*.key
storage/framework/cache/**
storage/framework/sessions/**
storage/framework/views/**
.env
.idea
Any ideas?

Related

PhpStorm doesn't recognize laravel namespace after building docker image

I am setting up a new Laravel project and automating the installation process using docker.
Everything works, but the problem here is that the PhpStorm doesn't recognize some namespaces because the vendor folder is a volume. I will show the files to understand them better.
docker-compose.yml
version: '3'
services:
laravel.test:
build:
dockerfile: Dockerfile
ports:
- '${APP_PORT:-8080}:80'
volumes:
- '.:/var/www/html'
- /var/www/html/vendor #HERE IS THE VOLUME FOR VENDOR
Dockerfile
FROM php:8.1.2-fpm
#INSTALL DEPENDENCIES
RUN apt-get update && apt-get install -y \
openssl \
build-essential \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
locales \
apt-transport-https \
wget \
lsb-release \
libxml2-dev \
ca-certificates \
git \
nano \
libonig-dev \
zip
#PGSQL
RUN apt-get install -y libpq-dev \
&& docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
&& docker-php-ext-install pdo pdo_pgsql pgsql
#Libraries required for Laravel
RUN docker-php-ext-install \
pdo_mysql \
bcmath \
ctype \
fileinfo \
iconv \
gd \
mbstring \
pdo_mysql \
xml \
pcntl
# Install composer
RUN php -r "readfile('https://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer
COPY ./ /var/www/html
# Copy code to $path
RUN composer install
EXPOSE 9000
CMD ["php-fpm"]
.dockerignore
vendor
So after building the image and running composer install, the vendor folder is created in the container with all dependencies and in the host machine but without subfolders, as we can see in the following image:
And when I try to import any namespace, I receive an error.
Nothing wrong in PHPStorm. Your vendor folder is empty.
I used to run docker-composer without mapping volume for vendor.
Remove the mapping and log in to service with bash and run the composer install
Steps
Remove the - /var/www/html/vendor volume mapping
Log in to service using docker exec -it laravel.test bash (FYI: It's better if you use container_name: search it.)
run the command composer install or composer update
Note: Before running remove the existing vendor folder.(delete or rename it _vendor for temp)
Wild guess: check you volums whether you defined volume.

File's permission not changed on Debian Buster docker-compose project while in Ubuntu22.04 does

Hi I wanted to ask about something strange happened to me....
I was dockerizing a laravel app in a Lamp stack trough a digitalocean tutorial.
https://www.digitalocean.com/community/tutorials/how-to-set-up-laravel-nginx-and-mysql-with-docker-compose
Basically when I do all the passages on a debian buster desktop, I got permission denied error, while if I do it on ubuntu 22.04 desktop I got no problems (tried on my dual boot laptop too and got absolutely no problem).
Here is the error:
The stream or file "/var/www/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied
I still don't understand why, because the error it's not in docker-compose yml or Dockerfile, probably how debian manage the permissions of the files.
Thanks for any suggestions.
In case anyone wondering this is the Dockerfile:
FROM php:8.1.0-fpm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Set working directory
WORKDIR /var/www
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
RUN chmod +x /usr/local/bin/install-php-extensions && sync && install-php-extensions mbstring pdo_mysql zip exif pcntl gd
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
libonig-dev \
locales \
zip \
libzip-dev \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
#RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
#RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
#RUN docker-php-ext-install gd
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
#RUN php artisan key:generate
# Copy existing application directory contents
COPY --chown=1000:1000 . /var/www
# Change current user to www
USER www
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
This must be added to dockerfile. web server access to root folder and 755 to storage folder
chown -R www-data:www-data /var/www
chmod -R 755 /var/www/storage

How to set laravel file permissions inside a docker image

I have the following dockerfile to build my Laravel web application. After I run it I get permission errors when trying to acccess the vendor and storage directories. My question is what is the correct way of setting permissions for Laravel for a php:7.2-fpm base image. The error I receive is:
fopen(/var/www/app/vendor/dompdf/dompdf/lib/fonts/glyphicons-halflings-normal_4ced20531a4f462a8c5c535d4debd2eb.ufm):
failed to open stream: Permission denied
Dockerfile
FROM php:7.2-fpm
WORKDIR /var/www/app
RUN apt-get update && apt-get install -y \
build-essential \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl \
npm
RUN curl -sL https://deb.nodesource.com/setup_9.x | bash
RUN apt-get install -y nodejs
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd
RUN docker-php-ext-install bcmath
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Copy existing application directory contents
COPY . /var/www/app
RUN chmod -R 775 /var/www/app
RUN chmod -R 775 /var/www/app/vendor
RUN chmod -R 777 /var/www/app/storage
# COPY --from=nodeBuild ./ /var/www
RUN composer install
RUN npm install
RUN npm update
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
I removed these commands:
RUN chmod -R 775 /var/www/app
RUN chmod -R 775 /var/www/app/vendor
RUN chmod -R 777 /var/www/app/storage
I got the webserver to own the directory recursively:
chown -R www-data:www-data /var/www
This resolved the problem

How to reinstall the libzip distribution when build lumen in docker

I use docker version Version 17.12.0-ce-mac55 (23011) on osx version 10.11.6. I have a problem when I build lumen on docker, but when the build process is finished there is an error like this :
configure: error: Please reinstall the libzip distribution ERROR:
Service 'app' failed to build: The command '/bin/sh -c apt-get update
&& apt-get install -y libpng-dev libjpeg-dev libpq-dev && rm -rf
/var/lib/apt/lists/* && docker-php-ext-configure gd
--with-png-dir=/usr --with-jpeg-dir=/usr && docker-php-ext-install gd mbstring pdo pdo_mysql pdo_pgsql zip' returned a non-zero code: 1
This is my dockerfile settings:
So the build process fails. Has anyone ever had a case like me? I hope someone can provide a solution.
Try to configure zip with libzip and install libzip-dev
#install some base extensions
RUN apt-get install -y zip libzip-dev \
&& docker-php-ext-configure zip --with-libzip \
&& docker-php-ext-install zip
With Alpine version :
RUN apk add --no-cache libpng-dev zlib-dev libzip-dev \
&& docker-php-ext-configure zip --with-libzip \
&& docker-php-ext-install zip
try this
RUN rm composer.lock && composer install
--optimize-autoloader
--no-interaction
--no-progress
--ignore-platform-reqs
RUN apk add --no-cache php
php7-common
php7-fpm
php7-pdo
php7-opcache
php7-zip \

Laravel, Nginx and Docker Container, Permission Denied

I have created a laravel project and put it in a docker container and it all seems to load fine, however at some point I get an error message saying:
There is no existing directory at "/Users/john/Documents/work/dashboard/src/storage/logs" and its not buildable: Permission denied
It is strange that Laravel is trying to write into a file or directory that is part of my local environment, instead of the docker container environment which I would expect to be something like:
/var/www/storage/logs
This is my docker-compose.yml:
version: '2'
services:
# The Web Server
web:
build:
context: ./
dockerfile: web.signup.dockerfile
working_dir: /var/www
volumes:
- ./src:/var/www
- /var/www/storage
env_file: 'src/.env'
ports:
- 80:80
volumes:
dbdata:
And this is my Dockerfile
FROM centos:latest
RUN set -ex \
&& yum install -y epel-release \
&& yum update -y mysql-client libmagickwand-dev \
&& yum install -y libmcrypt-devel \
&& yum install -y python-pip \
&& pip install --upgrade pip \
&& yum install -y zip unzip \
&& yum install -y java-1.8.0-openjdk \
&& yum clean all
RUN pip install --upgrade --ignore-installed six awscli
RUN yum install -y supervisor
RUN yum install -y php-pear php-devel
RUN pecl install imagick
# Add the Ngix
ADD nginx.repo /etc/yum.repos.d/nginx.repo
# Add the Centos PHP dependent repository
RUN rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
RUN yum update -y
# Installing Nginx
RUN yum -y install nginx
# Installing PHP
RUN yum -y --enablerepo=remi,remi-php72 install php-fpm php-common php-mcrypt php-mbstring
WORKDIR /var/www
ADD vhost.prod.conf /etc/nginx/conf.d/default.conf
COPY src/. /var/www
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
&& php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \
&& php composer-setup.php \
&& php -r "unlink('composer-setup.php');" \
&& php composer.phar install --no-dev --no-scripts \
&& rm composer.phar
RUN chown -R apache:apache \
/var/www/storage \
/var/www/bootstrap/cache
EXPOSE 80
EXPOSE 9000
RUN mkdir -p /run/php-fpm
COPY supervisord.conf /supervisord.conf
CMD ["/usr/bin/supervisord", "-c", "/supervisord.conf"]
Any ideas at all?
Works as designed,
you are mounting "src" from your current directory into the container.
volumes:
- ./src:/var/www
If you want to access your code inside the container you could add the files while building.
To fix your "bug",
chmod -R 777 /Users/john/Documents/work/dashboard/src/storage/logs
Would be ok a for local development environment
From my end, results are when no matter what folder permission we changed in the physical folder.
Delete everything in bootstrap/cache/* solved the issue.
In my case, I got this error when I ran out of HDD space and I cleared it
Only this helped:
rm bootstrap/cache/config.php
php artisan cache:clear
composer dump-autoload

Resources