Where to put the php artisan migrate command - php

Trying to deploy the laravel application on docker stack .What I am
confused or not able to figure out is where can I run this php artisan
migrate:fresh to generate the tables required in mysql.
The services and the task are running well
docker-compose.yml
version: '3.3'
networks:
smstake:
ipam:
config:
- subnet: 10.0.10.0/24
services:
db:
image: mysql:5.7
networks:
- smstake
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: smstake
MYSQL_USER: root
MYSQL_PASSWORD: password
deploy:
mode: replicated
placement:
constraints:
- node.role == manager
app:
image: smstake:latest
ports:
- 8000:80
networks:
- smstake
command: docker-compose exec app php artisan migrate --seed
deploy:
mode: replicated
replicas: 1
placement:
constraints:
- node.role == manager
volumes:
db_data:
Here is the dockerfile with which the image is generated
FROM alpine
ENV \
APP_DIR="/app" \
APP_PORT="80"
# the "app" directory (relative to Dockerfile) containers your Laravel app...
COPY app/ $APP_DIR
# or we can make the volume in compose to say use this directory
RUN apk update && \
apk add curl \
php7 \
php7-opcache \
php7-openssl \
php7-pdo \
php7-json \
php7-phar \
php7-dom \
php7-curl \
php7-mbstring \
php7-tokenizer \
php7-xml \
php7-xmlwriter \
php7-session \
php7-ctype \
php7-mysqli \
php7-pdo \
php7-pdo_mysql\
&& rm -rf /var/cache/apk/*
RUN curl -sS https://getcomposer.org/installer | php -- \
--install-dir=/usr/bin --filename=composer
RUN cd $APP_DIR && composer install
WORKDIR $APP_DIR
RUN chmod -R 775 storage
RUN chmod -R 775 bootstrap
#CMD php artisan migrate:fresh
CMD php artisan serve --host=0.0.0.0 --port=$APP_PORT
Tried adding to the Dockerfile as is commented but didn't solve the problem
Tried adding on docker-compose as command: php artisan migrate:fresh too
Previously was doing this in jenkins to make it work Now dont want it via jenkins
docker-compose up -d --force-recreate --build
#Running commands on already running service
docker-compose exec -T app php artisan migrate:fresh --seed --force

This is how I solved it .Created a bash script called run.sh and added the php artisan migrations commands followed by the php serve command.
run.sh
#!/bin/sh
cd /app
php artisan migrate:fresh --seed
php artisan serve --host=0.0.0.0 --port=$APP_PORT
Added entrypoint to the Dockerfile removing the CMD in the end which will run the commands desired.
copy ./run.sh /tmp
ENTRYPOINT ["/tmp/run.sh"]
Remove the command from the docker-compose.yml

To my opinion, automate migrate is not a good way when creating container. You can do this after container is up with this one line code manually;
docker exec your_container_name php artisan migrate

Yes, special script.
I try to build deploy and testing throw docker-compose, so i run migrations in script before starting supervisor in docker-service "jobs":
#!/bin/sh
cd /var/www
php artisan migrate --seed
/usr/bin/supervisord -n -c /etc/supervisord.conf
And piece from my deploy-docker-compose.yml:
services:
nginx:
depends_on:
- phpfpm ## NOT START BEFORE PHPFPM
phpfpm:
depends_on:
- jobs ## NOT START BEFORE MIGRATION
jobs:
# ....
This schema is not started in production yet;
UPD1
i had to create simple laravel command wait_db_alive:
public function handle()
{
$i = 1;
$ret = 1;
while($i <= 10){
echo 'connecting to host:'.config('database.connections.'.config('database.default').'.host').' try '.$i.'..';
try {
DB::connection()->getPdo();
echo 'ok'.PHP_EOL;
$ret = 0;
break;
} catch (\Exception $e) {
echo 'error:' . $e->getMessage() .PHP_EOL;
sleep(1);
$i++;
}
}
return $ret;
}
and edit init.sh to
#!/bin/sh
php artisan wait_db_alive && php artisan migrate --seed && /usr/bin/supervisord -n -c /etc/supervisord.conf
so, log for jobs:
jobs_1 | connecting to host:db try 1..error:SQLSTATE[HY000] [2002] Connection refused
jobs_1 | connecting to host:db try 2..error:SQLSTATE[HY000] [2002] Connection refused
jobs_1 | connecting to host:db try 3..ok
jobs_1 | Nothing to migrate.
jobs_1 | Seeding: ServicesTableSeeder
...
jobs_1 | Database seeding completed successfully.
jobs_1 | 2020-11-22 05:33:43,653 CRIT Supervisor is running as root.
UPD2
In some cases we need to start container without .env-file, then better init.sh for this:
#!/bin/sh
php artisan wait_db_alive && php artisan migrate --seed
/usr/bin/supervisord -n -c /etc/supervisord.conf

One quick way is to create temporary route to run migration.
Route::get('/migrate', function () {
\Artisan::call('migrate');
return \Artisan::output();
});

To run all migrate you need to be inside your container, for that you need to run your containers with docker-compose up, here u need to be with terminal in the directory where your docker-compose.yml file is.
after that, and with the same place of docker-compose.yml file, run this command to be inside your container:
docker exec -it name_of_container bash
and when you will be inside your container, go to your shared app inside the container, I think here is the app so cd app.
after all of this run:
php artisan migrate

Related

How do I build dockerized laravel app in azure pipeline?

I am trying to build and use a dockerized laravel app in an azure pipeline, but I get the error
Verifying lock file contents can be installed on current platform.
Package operations: 163 installs, 0 updates, 0 removals
In Filesystem.php line 268:
/var/www/app/vendor does not exist and could not be created.
when trying to install composer dependencies after building the image. The setup is pretty standard, I've tried so many different things but to no avail.
Dockerfile
FROM php:8.1-apache
RUN apt update
RUN apt-get install -y --no-install-recommends \
g++ \
libicu-dev \
libpq-dev \
libzip-dev \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
git \
lsb-release \
gnupg \
zip
RUN docker-php-ext-install \
intl \
opcache \
pdo \
pdo_mysql \
zip \
exif \
gd
RUN pecl install \
pcov \
xdebug
WORKDIR /var/www/app
RUN usermod -a -G www-data www-data
RUN chown root:root /var/www
RUN chown -R www-data:www-data /var/www/app
RUN chmod -R 755 /var/www/app
RUN mkdir /var/www/app/vendor
RUN chmod -R 775 /var/www/app/vendor
RUN chown -R www-data:www-data /var/www/app/vendor
RUN a2enmod rewrite
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
USER www-data
docker-compose
version: '3.8'
services:
database:
image: mysql:8.0
container_name: database
environment:
- MYSQL_DATABASE=app
- MYSQL_ROOT_PASSWORD=root
volumes:
- ./infrastructure/mysql-data:/var/lib/mysql
ports:
- '3306:3306'
api:
container_name: api
build:
context: ./infrastructure/php
ports:
- '8080:80'
volumes:
- ./server:/var/www/app
- ./infrastructure/apache/default.conf:/etc/apache2/sites-enabled/000-default.conf
depends_on:
- database
mailhog:
image: mailhog/mailhog:latest
container_name: mailhog
ports:
- '1025:1025'
- '8025:8025'
azure-pipelines.yml
trigger:
- develop
pool:
vmImage: ubuntu-latest
steps:
- script: make up
displayName: 'Build'
- script: |
docker-compose exec -T api composer install
make db_reset
displayName: 'Install'
- script: |
make analyse
displayName: 'Analyse backend'
Makefile
DOCKER_COMPOSE = docker-compose
COMPOSER ?= composer
PHP_CMD = php
PHP_SERVICE = api
up:
#echo "\n==> Docker container building and starting ..."
$(DOCKER_COMPOSE) up --build -d
db_reset:
$(DOCKER_COMPOSE) exec -T api php artisan migrate:fresh
$(DOCKER_COMPOSE) exec -T api php artisan passport:install
$(DOCKER_COMPOSE) exec -T api php artisan db:seed
analyse:
$(DOCKER_COMPOSE) exec -T -u root api php artisan ide-helper:models -W
$(DOCKER_COMPOSE) exec -T -u root api ./vendor/bin/php-cs-fixer fix app
$(DOCKER_COMPOSE) exec -T -u root api ./vendor/bin/phpstan analyse --memory-limit=2G -c phpstan.neon
I really don't know what I am doing wrong. Any help is appreciated

How do I run commands in a Docker container inside an Azure pipeline?

I have a PHP application that is containerized. I am trying to set up a pipeline in Azure, but I am struggling to run commands inside the built container (which is essential to fully install & scaffold the application so that I can run code analysis and tests).
I have make commands set up, so the way to install the application is simply
make up
make install
These are effectively running:
docker-compose up --build -d
docker-compose exec -T {containerName} composer install
docker-compose exec -T {containerName} php artisan migrate:fresh
docker-compose exec -T {containerName} php artisan passport:install
docker-compose exec -T {containerName} php artisan db:seed
...
to build the image, install dependencies (laravel), scaffold the app backend.
My test azure-pipelines.yml is set up as follows:
# Docker
# Build a Docker image
# https://learn.microsoft.com/azure/devops/pipelines/languages/docker
trigger:
- develop
resources:
- repo: self
variables:
tag: '$(Build.BuildId)'
stages:
- stage: Build
displayName: Build image
jobs:
- job: Build
displayName: Build
pool:
vmImage: ubuntu-latest
steps:
- task: Docker#2
displayName: Build an image
inputs:
command: build
dockerfile: '$(Build.SourcesDirectory)/infrastructure/php/Dockerfile'
tags: |
$(tag)
- job: Install
displayName: Install app
dependsOn: Build
steps:
- script: |
docker-compose exec -T api composer install
- job: Database
displayName: Scaffold Database
dependsOn: Install
steps:
- script: |
make db_reset
- job: Analyse
displayName: Analyse backend
dependsOn: Database
steps:
- script: |
make analyse
- job: Test
displayName: Run tests
dependsOn: Analyse
steps:
- script: |
php artisan test
but this fails on the Install job, the error log is as follows:
Starting: CmdLine
==============================================================================
Task : Command line
Description : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
Version : 2.201.1
Author : Microsoft Corporation
Help : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/command-line
==============================================================================
Generating script.
Script contents:
docker-compose exec -T api composer install
========================== Starting Command Output ===========================
/usr/bin/bash --noprofile --norc /home/vsts/work/_temp/a440fb6a-e03b-4da2-b958-1fa3aefa2084.sh
##[error]Bash exited with code '1'.
Finishing: CmdLine
Verifying lock file contents can be installed on current platform.
Package operations: 163 installs, 0 updates, 0 removals
In Filesystem.php line 268:
/var/www/app/vendor does not exist and could not be created.
when trying to install composer dependencies after building the image. The setup is pretty standard.
Dockerfile
FROM php:8.1-apache
RUN apt update
RUN apt-get install -y --no-install-recommends \
g++ \
libicu-dev \
libpq-dev \
libzip-dev \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
git \
lsb-release \
gnupg \
zip
RUN docker-php-ext-install \
intl \
opcache \
pdo \
pdo_mysql \
zip \
exif \
gd
RUN pecl install \
pcov \
xdebug
WORKDIR /var/www/app
RUN usermod -a -G www-data www-data
RUN chown root:root /var/www
RUN chown -R www-data:www-data /var/www/app
RUN chmod -R 755 /var/www/app
RUN mkdir /var/www/app/vendor
RUN chmod -R 775 /var/www/app/vendor
RUN chown -R www-data:www-data /var/www/app/vendor
RUN a2enmod rewrite
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
USER www-data
docker-compose
version: '3.8'
services:
database:
image: mysql:8.0
container_name: database
environment:
- MYSQL_DATABASE=app
- MYSQL_ROOT_PASSWORD=root
volumes:
- ./infrastructure/mysql-data:/var/lib/mysql
ports:
- '3306:3306'
api:
container_name: api
build:
context: ./infrastructure/php
ports:
- '8080:80'
volumes:
- ./server:/var/www/app
- ./infrastructure/apache/default.conf:/etc/apache2/sites-enabled/000-default.conf
depends_on:
- database
mailhog:
image: mailhog/mailhog:latest
container_name: mailhog
ports:
- '1025:1025'
- '8025:8025'
Makefile
DOCKER_COMPOSE = docker-compose
COMPOSER ?= composer
PHP_CMD = php
PHP_SERVICE = api
up:
#echo "\n==> Docker container building and starting ..."
$(DOCKER_COMPOSE) up --build -d
db_reset:
$(DOCKER_COMPOSE) exec -T api php artisan migrate:fresh
$(DOCKER_COMPOSE) exec -T api php artisan passport:install
$(DOCKER_COMPOSE) exec -T api php artisan db:seed
analyse:
$(DOCKER_COMPOSE) exec -T -u root api php artisan ide-helper:models -W
$(DOCKER_COMPOSE) exec -T -u root api ./vendor/bin/php-cs-fixer fix app
$(DOCKER_COMPOSE) exec -T -u root api ./vendor/bin/phpstan analyse --memory-limit=2G -c phpstan.neon
I am assuming the issue is with permissions when trying to run composer install through the container, but then, how do I actually use the container? There's no permission issues locally. The building obviously succeeds, here's the last snippet of the build job:
---> Running in b83b9f8e8010
All settings correct for using Composer
Downloading...
Composer (version 2.3.5) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer
Removing intermediate container b83b9f8e8010
---> bc104ff13e89
Step 11/22 : USER www-data
---> Running in 200f01ed5b5f
Removing intermediate container 200f01ed5b5f
---> 2431fb9a77ae
Step 12/22 : LABEL com.azure.dev.image.build.buildnumber=20220509.10
---> Running in 8183d45902d5
Removing intermediate container 8183d45902d5
---> d1a9ed6d2dc6
Step 13/22 : LABEL com.azure.dev.image.build.builduri=vstfs:///Build/Build/14
---> Running in 316a03a492e7
Removing intermediate container 316a03a492e7
---> 8bd620fc9792
Step 14/22 : LABEL com.azure.dev.image.build.definitionname=REDACTED
---> Running in 75f66a32856a
Removing intermediate container 75f66a32856a
---> 4daf41f08c6c
Step 15/22 : LABEL com.azure.dev.image.build.repository.name=REDACTED
---> Running in 707ba3523e83
Removing intermediate container 707ba3523e83
---> 7084ad9947a7
Step 16/22 : LABEL com.azure.dev.image.build.repository.uri=REDACTED
---> Running in b08c87ff8818
Removing intermediate container b08c87ff8818
---> 83a6531aef80
Step 17/22 : LABEL com.azure.dev.image.build.sourcebranchname=develop
---> Running in 0d69670ee481
Removing intermediate container 0d69670ee481
---> 6389f09560dc
Step 18/22 : LABEL com.azure.dev.image.build.sourceversion=dab0a85595268c16a05c5292adc1e0340c13818f
---> Running in 3b58e7fe5640
Removing intermediate container 3b58e7fe5640
---> e495c2eeab89
Step 19/22 : LABEL com.azure.dev.image.system.teamfoundationcollectionuri=REDACTED
---> Running in 2e04a85c91b9
Removing intermediate container 2e04a85c91b9
---> 4570213d4e47
Step 20/22 : LABEL com.azure.dev.image.system.teamproject=REDACTED
---> Running in f4c914d2522c
Removing intermediate container f4c914d2522c
---> 0ee813e09dbf
Step 21/22 : LABEL image.base.digest=sha256:2891049f5a33bd4ac61ea28b4d4f9144468e50369b7bc29ff61e2dd3089f5bb6
---> Running in 564f498fb0c4
Removing intermediate container 564f498fb0c4
---> 039be559c0d4
Step 22/22 : LABEL image.base.ref.name=php:8.1-apache
---> Running in 60c373b879be
Removing intermediate container 60c373b879be
---> c10cb44c17d6
Successfully built c10cb44c17d6
Finishing: Build an image
... or, am I doing this completely wrong? Any help would be greatly appreciated.
The permission issue happens on your ./server directory, you should check owner and permissions there.
It looks like you want to assign permissions on the mounted volume.
Over here you can find quite nice explanation how to deal with it.
Set appropriate permissions on the host machine or pass appropriate user and group to the container.
Docker-compose set user and group on mounted volume

could not translate host name "database" to address: Name or service not known [duplicate]

Trying to deploy the laravel application on docker stack .What I am
confused or not able to figure out is where can I run this php artisan
migrate:fresh to generate the tables required in mysql.
The services and the task are running well
docker-compose.yml
version: '3.3'
networks:
smstake:
ipam:
config:
- subnet: 10.0.10.0/24
services:
db:
image: mysql:5.7
networks:
- smstake
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: smstake
MYSQL_USER: root
MYSQL_PASSWORD: password
deploy:
mode: replicated
placement:
constraints:
- node.role == manager
app:
image: smstake:latest
ports:
- 8000:80
networks:
- smstake
command: docker-compose exec app php artisan migrate --seed
deploy:
mode: replicated
replicas: 1
placement:
constraints:
- node.role == manager
volumes:
db_data:
Here is the dockerfile with which the image is generated
FROM alpine
ENV \
APP_DIR="/app" \
APP_PORT="80"
# the "app" directory (relative to Dockerfile) containers your Laravel app...
COPY app/ $APP_DIR
# or we can make the volume in compose to say use this directory
RUN apk update && \
apk add curl \
php7 \
php7-opcache \
php7-openssl \
php7-pdo \
php7-json \
php7-phar \
php7-dom \
php7-curl \
php7-mbstring \
php7-tokenizer \
php7-xml \
php7-xmlwriter \
php7-session \
php7-ctype \
php7-mysqli \
php7-pdo \
php7-pdo_mysql\
&& rm -rf /var/cache/apk/*
RUN curl -sS https://getcomposer.org/installer | php -- \
--install-dir=/usr/bin --filename=composer
RUN cd $APP_DIR && composer install
WORKDIR $APP_DIR
RUN chmod -R 775 storage
RUN chmod -R 775 bootstrap
#CMD php artisan migrate:fresh
CMD php artisan serve --host=0.0.0.0 --port=$APP_PORT
Tried adding to the Dockerfile as is commented but didn't solve the problem
Tried adding on docker-compose as command: php artisan migrate:fresh too
Previously was doing this in jenkins to make it work Now dont want it via jenkins
docker-compose up -d --force-recreate --build
#Running commands on already running service
docker-compose exec -T app php artisan migrate:fresh --seed --force
This is how I solved it .Created a bash script called run.sh and added the php artisan migrations commands followed by the php serve command.
run.sh
#!/bin/sh
cd /app
php artisan migrate:fresh --seed
php artisan serve --host=0.0.0.0 --port=$APP_PORT
Added entrypoint to the Dockerfile removing the CMD in the end which will run the commands desired.
copy ./run.sh /tmp
ENTRYPOINT ["/tmp/run.sh"]
Remove the command from the docker-compose.yml
To my opinion, automate migrate is not a good way when creating container. You can do this after container is up with this one line code manually;
docker exec your_container_name php artisan migrate
Yes, special script.
I try to build deploy and testing throw docker-compose, so i run migrations in script before starting supervisor in docker-service "jobs":
#!/bin/sh
cd /var/www
php artisan migrate --seed
/usr/bin/supervisord -n -c /etc/supervisord.conf
And piece from my deploy-docker-compose.yml:
services:
nginx:
depends_on:
- phpfpm ## NOT START BEFORE PHPFPM
phpfpm:
depends_on:
- jobs ## NOT START BEFORE MIGRATION
jobs:
# ....
This schema is not started in production yet;
UPD1
i had to create simple laravel command wait_db_alive:
public function handle()
{
$i = 1;
$ret = 1;
while($i <= 10){
echo 'connecting to host:'.config('database.connections.'.config('database.default').'.host').' try '.$i.'..';
try {
DB::connection()->getPdo();
echo 'ok'.PHP_EOL;
$ret = 0;
break;
} catch (\Exception $e) {
echo 'error:' . $e->getMessage() .PHP_EOL;
sleep(1);
$i++;
}
}
return $ret;
}
and edit init.sh to
#!/bin/sh
php artisan wait_db_alive && php artisan migrate --seed && /usr/bin/supervisord -n -c /etc/supervisord.conf
so, log for jobs:
jobs_1 | connecting to host:db try 1..error:SQLSTATE[HY000] [2002] Connection refused
jobs_1 | connecting to host:db try 2..error:SQLSTATE[HY000] [2002] Connection refused
jobs_1 | connecting to host:db try 3..ok
jobs_1 | Nothing to migrate.
jobs_1 | Seeding: ServicesTableSeeder
...
jobs_1 | Database seeding completed successfully.
jobs_1 | 2020-11-22 05:33:43,653 CRIT Supervisor is running as root.
UPD2
In some cases we need to start container without .env-file, then better init.sh for this:
#!/bin/sh
php artisan wait_db_alive && php artisan migrate --seed
/usr/bin/supervisord -n -c /etc/supervisord.conf
One quick way is to create temporary route to run migration.
Route::get('/migrate', function () {
\Artisan::call('migrate');
return \Artisan::output();
});
To run all migrate you need to be inside your container, for that you need to run your containers with docker-compose up, here u need to be with terminal in the directory where your docker-compose.yml file is.
after that, and with the same place of docker-compose.yml file, run this command to be inside your container:
docker exec -it name_of_container bash
and when you will be inside your container, go to your shared app inside the container, I think here is the app so cd app.
after all of this run:
php artisan migrate

laravel docker php:7.4-fpm-alpine3.12 500 error

I have a problem for docker-compose up, when I git clone from the code repository on aws.
Files become root from www
I think it is because of the volumes
But I don't know how to fix this.
Any helps?
And start_script.sh part not working as well.
php artisan config:clear
php artisan cache:clear
php artisan key:generate
php artisan migrate
php artisan db:seed
php-fpm dockerfile
FROM php:7.4-fpm-alpine3.12
USER root
WORKDIR /var/www
RUN apk update && apk add --no-cache $PHPIZE_DEPS \
build-base shadow nano curl gcc git bash vim \
php7 \
php7-fpm \
php7-common \
php7-pdo \
php7-pdo_mysql \
php7-mysqli \
php7-mcrypt \
php7-mbstring \
php7-xml \
php7-openssl \
php7-json \
php7-phar \
php7-zip \
php7-gd \
php7-dom \
php7-session \
php7-zlib \
haveged
# # Install extensions
RUN docker-php-ext-install pdo pdo_mysql
RUN docker-php-ext-enable pdo_mysql
# Remove Cache
RUN rm -rf /var/cache/apk/*
RUN mkdir -p /usr/src/php/ext/redis \
&& curl -L https://github.com/phpredis/phpredis/archive/5.3.4.tar.gz | tar xvz -C
/usr/src/php/ext/redis --strip 1 \
&& echo 'redis' >> /usr/src/php-available-exts \
&& docker-php-ext-install redis
# Copy config
COPY ./config/php/local.ini /usr/local/etc/php/conf.d/local.ini
# verify that the binary w
RUN addgroup -g 1000 -S www && \
adduser -u 1000 -S www -G www -s /bin/sh -D www
COPY --chown=www:www . /var/www/
RUN ["chmod", "+x", "./start_script.sh"]
EXPOSE 9000
RUN chmod -R 775 /var/www/storage
RUN ls -al
USER www
# Run php-fpm
CMD ["./start_script.sh"]
start_script.sh
#!/bin/bash
set -m
#turn on bash's job control
#Start the primary process and put it in the background
php-fpm -y /usr/local/etc/php-fpm.conf -R &
#Start the helper process
php artisan config:clear
php artisan cache:clear
php artisan key:generate
php artisan migrate
php artisan db:seed
#the my_helper_process might need to know how to wait on the
#primary process to start before it does its work and returns
ls -al
#now we bring the primary process back into the foreground
# and leave it there
fg %1
docker-compose.yml
version: "3"
services:
app:
build:
context: .
dockerfile: ./docker/php-fpm/Dockerfile
image: docker/laravel
container_name: app
tty: true
restart: unless-stopped
environment:
DB_HOST: db
DB_PASSWORD: password
SESSION_DRIVER: redis
REDIS_HOST: redis
volumes:
- ./:/var/www
- ./config/php/local.ini:/usr/local/etc/php/conf.d/local.ini
depends_on:
- db
webserver:
build:
context: .
dockerfile: ./docker/nginx/Dockerfile
image: docker/nginx
container_name: webserver
restart: unless-stopped
ports:
- "8080:80"
volumes:
- ./:/var/www
- ./config/nginx/conf.d/:/etc/nginx/conf.d/
depends_on:
- app
db:
image: mysql:5.7
container_name: db
environment:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: password
tty: true
ports:
- "3306:3306"
volumes:
- dbdata:/var/lib/mysql
redis:
image: redis:latest
container_name: redis
volumes:
dbdata:
driver: local
when I build php:7.4-fpm-alpine3.12, ls -al
After I docker-compose up ls -al get
when I write dd(); on app\Excepions\Handler.php
public function register()
{
$this->reportable(function (Throwable $e) {
dd($e);
});
}
And I also did
add this line in config object of composer.json file
"config": {
"platform-check": false
},
docker exec app php artisan config:cache
docker run --rm -v ${PWD}:/app composer dump-autoload
How to fix this?

Laravel application performance issue on Docker for Windows

I am trying to run my laravel application inside docker containers on my laptop (during development) and finding that the speed of the application is drastically slow when compared to running it using XAMPP for example.
My laptop is running Windows 10 Pro (64-Bit) with i7-6700HQ CPU, 16 GB RAM and SSD.
When I run my app in docker for windows, average page load time is approx 3.5 Seconds.
Running it on local XAMPP, average page load time is approx 350 Milliseconds (0.35 Second).
For my docker setup, I use the following image/Dockerfile:
FROM alpine:3.8
MAINTAINER Latheesan Kanesamoorthy
RUN apk add \
--no-cache \
--update \
apache2 \
composer \
curl \
php7 \
php7-apache2 \
php7-curl \
php7-bcmath \
php7-dom \
php7-mbstring \
php7-pdo_mysql \
php7-session \
php7-sockets \
php7-tokenizer \
php7-xml \
php7-xmlwriter \
php7-fileinfo \
&& mkdir -p /run/apache2 \
&& ln -sf /dev/stdout /var/log/apache2/access.log \
&& ln -sf /dev/stderr /var/log/apache2/error.log
COPY ./image/*.conf /etc/apache2/conf.d/
COPY ./image/php.ini /etc/php7/conf.d/99_custom.ini
RUN mkdir -p /storage/framework/testing
RUN mkdir -p /storage/framework/views
RUN mkdir -p /storage/framework/sessions
RUN mkdir -p /storage/framework/cache/data
RUN chown -R apache:apache /storage
WORKDIR /app
COPY ./src/composer.* ./
RUN composer install -n --no-autoloader --no-scripts --no-progress --no-suggest
COPY src .
RUN composer dump-autoload -o -n
EXPOSE 80
and docker-compose.yml:
version: '2.1'
services:
mysql:
container_name: myapp-mysql
mem_limit: 512M
image: mysql:5.7
restart: always
environment:
MYSQL_ROOT_PASSWORD: myapp
MYSQL_DATABASE: myapp
MYSQL_USER: myapp
MYSQL_PASSWORD: myapp
ports:
- "35000:3306"
redis:
container_name: myapp-redis
image: redis:latest
redis-commander:
container_name: myapp-redis-commander
image: rediscommander/redis-commander:latest
hostname: redis-commander
restart: always
environment:
- REDIS_HOSTS=local:redis:6379
ports:
- "7050:8081"
links:
- redis
app:
container_name: myapp-app
mem_limit: 512M
build:
context: ""
dockerfile: image/Dockerfile
env_file:
- image/env/development
volumes:
- ./src:/app:cached
ports:
- "25000:80"
entrypoint: httpd -DFOREGROUND
links:
- mysql
- redis
and I use the following commands to boot it up:
docker-compose down --remove-orphans
docker-compose up -d --build
docker exec myapp-app composer install --prefer-dist --no-suggest
docker exec myapp-app php artisan cache:clear
docker exec myapp-app php artisan migrate:fresh --seed
As you can see, the docker version uses redis as the driver for: cache, eloquent model cache, queue and session.
Locally for XAMPP, I am simply using file driver for all.
Any idea why the performance is so slow on docker?
P.S. The reason why I want to try developing using the docker environment is so that I can keep my development and production environment identical.

Categories