Dockerize Laravel Lumen - laravel

I have Laravel Lumen project
I have a Dockerfile for it.
FROM alpine:3.7
RUN apk --no-cache add \
php7 \
php7-fpm \
php7-pdo \
php7-mbstring \
php7-xml \
php7-openssl \
php7-json \
php7-phar \
php7-zip \
php7-dom \
php7-session \
php7-zlib && \
php7 -r "copy('http://getcomposer.org/installer', 'composer-setup.php');" && \
php7 composer-setup.php --install-dir=/usr/bin --filename=composer && \
php7 -r "unlink('composer-setup.php');" && \
ln -sf /usr/bin/php7 /usr/bin/php && \
ln -s /etc/php7/php.ini /etc/php7/conf.d/php.ini
RUN set -x \
addgroup -g 82 -S www-data \
adduser -u 82 -D -S -G www-data www-data
COPY . /src
ADD .env.example /src/.env
WORKDIR /src
RUN chmod -R 777 storage
CMD php -S localhost:8000 -t public
I also have docker-compose.yml
version: '3'
services:
portalmodules:
build:
context: .
dockerfile: Dockerfile
ports:
- 8000:8000
When I run docker-compose up, everything seem success
Creating network "lumen_default" with the default driver
Building portalmodules
Step 1/9 : FROM alpine:3.7
3.7: Pulling from library/alpine
5d20c808ce19: Downloading [> ] 21.15k5d20c808ce19: Downloading [============================> ] 1.195M5d20c808ce19: Extracting [> ] 32.77kB5d20c808ce19: Extracting [=====================> ] 917.5kB5d20c808ce19: Extracting [==================================================>] 2.107MB5d20c808ce19: Pull complete
Digest: sha256:fea30b82fd63049b797ab37f13bf9772b59c15a36b1eec6b031b6e483fd7f252
Status: Downloaded newer image for alpine:3.7
---> 6d1ef012b567
Step 2/9 : LABEL maintainer "John Doe <john#site.com>"
---> Running in fb4f2dd6d1de
Removing intermediate container fb4f2dd6d1de
---> 8a45db7ddab9
Step 3/9 : RUN apk --no-cache add php7 php7-fpm php7-pdo php7-mbstring php7-xml php7-openssl php7-json php7-phar php7-zip php7-dom php7-session php7-zlib && php7 -r "copy('http://getcomposer.org/installer', 'composer-setup.php');" && php7 composer-setup.php --install-dir=/usr/bin --filename=composer && php7 -r "unlink('composer-setup.php');" && ln -sf /usr/bin/php7 /usr/bin/php && ln -s /etc/php7/php.ini /etc/php7/conf.d/php.ini
---> Running in 3de805731cad
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/community/x86_64/APKINDEX.tar.gz
(1/19) Installing php7-common (7.1.17-r0)
(2/19) Installing ncurses-terminfo-base (6.0_p20171125-r1)
(3/19) Installing ncurses-terminfo (6.0_p20171125-r1)
(4/19) Installing ncurses-libs (6.0_p20171125-r1)
(5/19) Installing libedit (20170329.3.1-r3)
(6/19) Installing pcre (8.41-r1)
(7/19) Installing libxml2 (2.9.8-r1)
(8/19) Installing php7 (7.1.17-r0)
(9/19) Installing php7-dom (7.1.17-r0)
(10/19) Installing php7-fpm (7.1.17-r0)
(11/19) Installing php7-json (7.1.17-r0)
(12/19) Installing php7-mbstring (7.1.17-r0)
(13/19) Installing php7-openssl (7.1.17-r0)
(14/19) Installing php7-pdo (7.1.17-r0)
(15/19) Installing php7-phar (7.1.17-r0)
(16/19) Installing php7-session (7.1.17-r0)
(17/19) Installing php7-xml (7.1.17-r0)
(18/19) Installing libzip (1.3.0-r1)
(19/19) Installing php7-zip (7.1.17-r0)
Executing busybox-1.27.2-r11.trigger
OK: 25 MiB in 32 packages
All settings correct for using Composer
Downloading...
Composer (version 1.8.4) successfully installed to: /usr/bin/composer
Use it: php /usr/bin/composer
Removing intermediate container 3de805731cad
---> ff5d58488a6d
Step 4/9 : RUN set -x addgroup -g 82 -S www-data adduser -u 82 -D -S -G www-data www-data
---> Running in 634b56b930cd
Removing intermediate container 634b56b930cd
---> 395f7f861700
Step 5/9 : COPY . /src
---> 0b4e7b92df73
Step 6/9 : ADD .env.example /src/.env
---> 2848f770f17d
Step 7/9 : WORKDIR /src
---> Running in cbba620f9904
Removing intermediate container cbba620f9904
---> 819ee9222de7
Step 8/9 : RUN chmod -R 777 storage
---> Running in 135b707d5008
Removing intermediate container 135b707d5008
---> 4bf78fca1236
Step 9/9 : CMD php -S localhost:8000 -t public
---> Running in 55d0a643fb43
Removing intermediate container 55d0a643fb43
---> bb0d3d1da9a9
Successfully built bb0d3d1da9a9
Successfully tagged lumen_portalmodules:latest
WARNING: Image for service portalmodules was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating lumen_portalmodules_1 ... done
Attaching to lumen_portalmodules_1
portalmodules_1 | PHP 7.1.17 Development Server started at Fri Mar 29 20:16:45 2019
But I don't what is the URL - I should go to see my site ... 🤦🏻‍♂️
I've tried go to : http://localhost:8000/
I kept getting this
Am I doing anything wrong ? What is my site URL ?

Have you tried : CMD php -S 0.0.0.0:8000 -t public ?
localhost is refering to the container local ip not your host machine's

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

Run existing Laravel project with sail

I have an existing Laravel project which I created with the following command.
curl -s https://laravel.build/example-app | bash
The project was created successfully and when I start it with command ./vendor/bin/sail up it works fine.
I want to clone the project on my second machine and start with sail. I found the following link from documentation which gives you possibility to run composer install even if you don't have composer installed on your OS.
docker run --rm \
-u "$(id -u):$(id -g)" \
-v $(pwd):/var/www/html \
-w /var/www/html \
laravelsail/php81-composer:latest \
composer install --ignore-platform-reqs
This basically starts a docker container which has php and composer installed and runs composer install.
Installing packages work fine, but whenever I run ./vendor/bin/sail up then it throws errors and the application is not started.
It looks like some packages were not installed.
Has anyone had the same issue?
The problem is that you should update laravel/sail package first and then run composer install.
docker run --rm \
-u "$(id -u):$(id -g)" \
-v $(pwd):/var/www/html \
-w /var/www/html \
laravelsail/php81-composer:latest \
composer update laravel/sail
docker run --rm \
-u "$(id -u):$(id -g)" \
-v $(pwd):/var/www/html \
-w /var/www/html \
laravelsail/php81-composer:latest \
composer install --ignore-platform-reqs

Dockerfile does not create files and folders in Laravel

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.

composer install not executed by docker build

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?

Resources