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.
Related
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
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
I have a Laravel Project and I created a docker-compose and dockerfile.
I do docker-compose build and my problem is that:
It does not create the folder vendor (composer install).
Do not copy .env.local to .env
Do not create the node_modules folder
docker-compose.yml
version: "3"
services:
api:
container_name: nadal_api
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/var/www/html/app
ports:
- 5002:80
DockerFile
FROM composer:latest AS composer
#Generamos el primer build desde un container de nodejs
FROM node:latest as nodebuild
COPY --chown=root:www-data . /var/www/html/app
WORKDIR /var/www/html/app
RUN npm install
RUN npm run production
#Tomamos la imagen de php fpm para utilizar las librerias compiladas
FROM php:7.2-fpm-stretch
#Instalamos nginx y otras dependencias del framework
RUN apt-get update && apt-get install -y \
apt-transport-https \
wget \
lsb-release \
libxml2-dev \
nginx \
ca-certificates \
git \
zip
#Compilamos 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
#Compilamos las librerias requeridas por laravel
RUN docker-php-ext-install pdo pdo_mysql
RUN docker-php-ext-install xml
RUN docker-php-ext-install mbstring
#Install supervisor porque debemos tener dos procesos, nginx y fpm
RUN wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
RUN echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/php.list
RUN apt-get update && apt-get install -y \
supervisor \
curl \
libssl-dev \
libmcrypt-dev
#Copiamos las configuraciones de nginx, el host y supervisor
ADD docker/nginx.conf /etc/nginx/nginx.conf
ADD docker/virtualhost.conf /etc/nginx/conf.d/default.conf
ADD docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
#Copiamos el projecto con los modulos de node y los assets compilados
COPY --from=nodebuild --chown=root:www-data /var/www/html/app /var/www/html/app
COPY --chown=root:www-data ./.env.local /var/www/html/app/.env
#Aplicamos los permisos que corresponden
RUN chmod -R g+w /var/www/html/app/storage
RUN chmod -R g+w /var/www/html/app/bootstrap
#Copiamos composer
COPY --from=composer /usr/bin/composer /usr/bin/composer
##Download composer packages
WORKDIR /var/www/html/app
RUN composer install
RUN chmod -R g+w /var/www/html/app/vendor
RUN php artisan key:generate
EXPOSE 80
CMD ["/usr/bin/supervisord"]
Can it be the permissions?
Step 21/30 : COPY --chown=root:www-data ./.env.${environment:-local}
/var/www/html/app/.env ---> 7d1038fe2615
Step 26/30 : RUN composer install ---> Running in 8e3265277308
Removing intermediate container 8e3265277308 ---> 429553e893a4 Step
27/30 :
RUN chmod -R g+w /var/www/html/app/vendor ---> Running in
9e276059478b Removing intermediate container 9e276059478b --->
d52ee5ad4ec4
When building the image, you COPY your whole current directory to /var/www/html/app, then stuff happens with the content there. On docker-compose up, you mount your whole folder again at this destination, which overwrites everything that happened on docker-compose build. To avoid this, just remove the volumes part from your docker-compose.yml.
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 \
This question has been asked here in many ways, but none of them seemed to be exactly what I'm looking for. Note: I'm totally new to Docker.
I've currently got a Docker image ("padcrawler") on my Mac, which is an image built off of scrapinghub/splash.
I'm basically just trying to make it so that any changes I make on my host machine (Mac), are automatically updated within the container. I'm using docker-machine and NOT boot2docker.
This is my Dockerfile:
FROM scrapinghub/splash:latest
# Update UBUNTU base image and install deps
RUN apt-get update \
&& apt-get upgrade -y \
&& apt-get install -y \
build-essential \
ca-certificates \
gcc \
git \
libpq-dev \
make \
libxml2-dev \
libxslt-dev \
libssl-dev \
libffi-dev \
zlib1g-dev \
python-pip \
python \
python-dev \
ssh \
&& apt-get autoremove \
&& apt-get clean
RUN mkdir -p /usr/src/pad
WORKDIR /usr/src/pad
# Copy over everything to container directory
COPY . /usr/src/pad
# Install Python (2.7) deps
RUN /usr/bin/pip install -r /usr/src/pad/requirements.txt
# Launch pad-crawler upon docker container bootup
ENTRYPOINT /usr/src/pad/start
I'm also using docker-compose and this is my yml file:
version: '2'
services:
crawler:
build: .
volumes:
- ".:/usr/src/pad"
image: padcrawler
However, I'm still unable to get any host file changes to automatically sync with the mounted volume in the container. Obviously it's annoying and frustrating to have to manually copy over the changes every-time. Is there a bigger issue here? I'm using Docker totally wrong?