Laravel request/response time very slow on Docker Laravel 8 - php

Does anyone have any idea why the request time of my Laravel project is running very slow? I'm running the project on Docker and I use a Dockerfile and docker-compose.yml. I'm also using nginx
I have tried multiple things like Eager loading and artisan optimize but none of those seems to work.
I'm running this on a laptop with an i7-9750H and 16gb RAM.
This what my debugbar says:
Dockerfile in a folder named 'php':
FROM php:8.0-fpm-alpine
RUN apk add shadow && usermod -u 1000 www-data && groupmod -g 1000 www-data
RUN chmod 777 /dev/stdout /dev/stderr
RUN apk --update add wget \
curl \
git \
grep \
build-base \
libmcrypt-dev \
libxml2-dev \
zlib-dev \
autoconf \
cyrus-sasl-dev \
libgsasl-dev \
composer \
supervisor \
libpng-dev \
freetype-dev \
libjpeg-turbo-dev \
libjpeg-turbo-utils \
openssh \
htop \
nano \
ghostscript \
imagemagick-dev \
libzip-dev \
wkhtmltopdf \
jpegoptim \
optipng \
pngquant \
pngcrush \
gifsicle \
bash
RUN apk add --update --no-cache graphviz font-bitstream-type1
RUN echo '* * * * * php /var/www/html/artisan schedule:run' | crontab -u www-data -
RUN rm -rf /var/spool/cron/crontabs/root
RUN docker-php-ext-install mysqli pdo pdo_mysql tokenizer xml zip exif
RUN pecl channel-update pecl.php.net \
&& pecl install redis \
&& rm -rf /tmp/pear \
&& docker-php-ext-enable redis \
&& docker-php-ext-install gd \
&& docker-php-ext-install -j$(nproc) iconv \
&& docker-php-ext-configure gd \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install bcmath
# install imagick
# use github version for now until release from https://pecl.php.net/get/imagick is ready for PHP 8
RUN mkdir -p /usr/src/php/ext/imagick \
&& curl -fsSL https://github.com/Imagick/imagick/archive/06116aa24b76edaf6b1693198f79e6c295eda8a9.tar.gz | tar xvz -C "/usr/src/php/ext/imagick" --strip 1 \
&& docker-php-ext-install imagick
RUN rm /var/cache/apk/* && \
mkdir -p /var/www && \
chown www-data:www-data /var/www -R
CMD ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisord.conf"]
USER www-data
RUN composer global require laravel/envoy
ENV PATH "$PATH:/home/www-data/.composer/vendor/bin"
Dockerfile inside folder named 'nginx':
FROM nginx:alpine
docker-compose.yml
version: "3"
services:
php:
volumes:
- './src/:/var/www/html'
- './.docker/php/supervisor.conf:/etc/supervisord.conf'
tty: true
build:
context: .
dockerfile: .docker/php/Dockerfile
links:
- "db:database"
- "redis:redis"
networks:
internal:
traefik:
nginx:
image: nginx:alpine
volumes:
- './src:/var/www/html'
- './.docker/nginx/nginx/conf.d/:/etc/nginx/conf.d/'
- './.docker/nginx/nginx/nginx.conf:/etc/nginx/nginx.conf'
labels:
traefik.backend: domain.test
traefik.frontend.rule: "Host:library.test"
traefik.docker.network: traefik
traefik.port: 80
networks:
internal:
traefik:
depends_on:
- php
db:
image: mysql/mysql-server:latest
environment:
MYSQL_DATABASE: 'library'
MYSQL_USER: 'amin'
MYSQL_PASSWORD: 'secret'
MYSQL_ROOT_PASSWORD: 'secret'
DEFAULT_AUTHENTICATION_PLUGIN: 'mysql_native_password'
volumes:
- mysql:/var/lib/mysql:rw
- './database:/var/data'
networks:
- internal
ports:
- "3306:3306"
# Cache server
redis:
image: redis:4-alpine
volumes:
- redis:/data:rw
networks:
internal:
volumes:
mysql:
driver: "local"
redis:
driver: "local"
networks:
traefik:
external: true
internal:
external: false

That's not slow.
20MB in 1.7s is about 12MB/s, with overheads that's pretty quick.
The route you're hitting is returning heaps of data, probably more than you expect, and that's why it looks like it's running slow.

Related

How to containerize laravel and postgres using docker?

Please help with code for dockerfile and docker-compose file i need laravel setup and postgres.
This is my docker file
FROM php:7.4
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng-dev \
zlib1g-dev \
libpq-dev \
libxml2-dev \
libzip-dev \
libonig-dev \
graphviz \
&& docker-php-ext-configure gd \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install pdo_mysql \
&& docker-php-ext-install pdo_psql \
&& docker-php-ext-install zip \
&& docker-php-ext-install sockets \
&& docker-php-source delete \
&& curl -sS https://getcomposer.org/installer | php -- \
--install-dir=/usr/local/bin --filename=composer
WORKDIR /var/www/html
COPY . .
RUN composer install
CMD php artisan serve --host=0.0.0.0
EXPOSE 8000
this is docker-compose.yml file
version: '3'
services:
php_4:
build:
context: .
dockerfile: Dockerfile
volumes:
- ./:/var/www/html
ports:
- "8000:8000"
depends_on:
- postgres_4
postgres_4:
image: postgres
restart: unless-stopped
ports:
- "5433:5432"
volumes:
- ./docker/postgres:/var/lib/postgresql/data
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: lara_4
POSTGRES_HOST_AUTH_METHOD: "trust"
Both are not working giving different different errors. SO please anyone give fresh code for both dockerfile and docker-compose file for laravel with postgresql using docker.
You should specify web server, i.e. nginx in docker-compose.yml file
You should take a look at Laravel Sail if you're just starting out with Laravel + Docker.
For your question in general, please edit your question and insert the specific errors.

Persist Symfony upgrade in Docker container

I have a PHP-based Docker container with Composer and Symfony installed inside of it. But each time I start that container, a message from Symfony appears, proposing to download & install the last version (and to activate TLS), which I do (for both). So I think the upgrade doesn't persist, how can I solve this ? (thank you in advance)
Thank you for your answers, everyone. The docker-composer.yaml and php/Dockerfile are made from a French tutorial video, slightly modified due to improvements noted in the Youtube comment section: The video is called "Un environnement de développement Symfony 5 avec Docker et Docker-compose" from Yoandev Co. Here is the docker-compose.yaml :
version: "3.8"
services:
db:
image: mariadb
container_name: db_SF_tuto_2
restart: always
volumes:
- db-data2:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: admin
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
networks:
- dev2
phpmyadmin:
image: phpmyadmin
container_name: phpmyadmin_SF_tuto_2
restart: always
depends_on:
- db
ports:
- 8090:80
environment:
PMA_HOST: db
networks:
- dev2
maildev:
image: maildev/maildev
container_name: maildev_SF_tuto_2
command: bin/maildev --web 80 --smtp 25 --hide-extensions STARTTLS
ports:
- "8091:80"
restart: always
networks:
- dev2
www:
build: php
container_name: www_SF_tuto_2
ports:
- "8742:8000"
volumes:
- ./php/vhosts:/etc/apache2/sites-enabled
- ./:/var/www
restart: always
networks:
- dev2
networks:
dev2:
volumes:
db-data2:
… and php/Dockerfile :
FROM php:7.4-apache
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf \
\
&& apt-get update \
&& apt-get install -y --no-install-recommends \
locales apt-utils git libicu-dev g++ libpng-dev libxml2-dev libzip-dev libonig-dev libxslt-dev unzip \
\
&& echo "en_US.UTF-8 UTF-8" > /etc/locale.gen \
&& echo "fr_FR.UTF-8 UTF-8" >> /etc/locale.gen \
&& locale-gen \
\
&& curl -sS https://getcomposer.org/installer | php -- \
&& mv composer.phar /usr/local/bin/composer \
\
&& curl -sS https://get.symfony.com/cli/installer | bash \
&& mv /root/.symfony/bin/symfony /usr/local/bin \
\
&& docker-php-ext-configure \
intl \
&& docker-php-ext-install \
pdo_mysql gd opcache intl zip calendar xsl \
\
&& pecl install apcu && docker-php-ext-enable apcu
WORKDIR /var/www/
As you have discovered, container storage is temporary and any changes done in it will be discarded when the container is removed. Note that you can stop a running container and the changes will still be there the next time you launch it, but docker-compose down does remove it, so every time you boot up your symfony binary is being reloaded from the image.
If you want for the updates to persist you'll have to create a volume.
However, since your current installation directory already contains binaries from your base image I'd suggest installing to a different directory and mount that.
In your Dockerfile, change the installation target. Since its a new directory you need to add it to the current $PATH so the tools can be executed.
FROM php:7.4-apache
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf \
\
&& apt-get update \
&& apt-get install -y --no-install-recommends \
locales apt-utils git libicu-dev g++ libpng-dev libxml2-dev libzip-dev libonig-dev libxslt-dev unzip \
&& echo "en_US.UTF-8 UTF-8" > /etc/locale.gen \
&& echo "fr_FR.UTF-8 UTF-8" >> /etc/locale.gen \
&& locale-gen \
\
&& mkdir /opt/bin -p \
&& curl -sS https://getcomposer.org/installer | php -- --install-dir /opt/bin --filename composer \
&& curl -sS https://get.symfony.com/cli/installer | bash -s - --install-dir /opt/bin \
\
&& docker-php-ext-configure \
intl \
&& docker-php-ext-install \
pdo_mysql gd opcache intl zip calendar xsl \
\
&& pecl install apcu && docker-php-ext-enable apcu
ENV PATH /opt/bin:$PATH
WORKDIR /var/www/
Now specify where to map the volume in the docker-compose.yml file:
www:
# ...
volumes:
# Specify the mount point.
# If you use short syntax here, a volume will be created every restart.
- phptools:/opt/bin
volumes:
# Declare a named volume so it can be remounted
phptools:
Run docker-compose build and docker-compose up.

Laravel 7 Docker - Unable to Login to Application

I have 2 dockerized laravel apps, which were working fine previously (about 2 months back), now when i try to login to them, App is unable to authenticate.
When i use unauthenticated routes, app works fine.
Database is working fine. Migrations , seeding is working too.
Another laravel app is working absolutely fine with same Dockerfile settings.
Dockerfile
FROM php:7.4-apache
USER root
# WORKDIR /var/www/html
RUN apt-get update && apt-get install -y \
libpng-dev \
zlib1g-dev \
libxml2-dev \
libzip-dev \
libonig-dev \
zip \
curl \
unzip \
&& docker-php-ext-configure gd \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install pdo_mysql \
&& docker-php-ext-install mysqli \
&& docker-php-ext-install zip \
&& docker-php-source delete
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN chown -R www-data:www-data /var/www/html \
&& a2enmod rewrite
docker-compose.yaml
version: '3.7'
services:
app:
build:
context: .
dockerfile: .docker/Dockerfile
image: 'geninv'
container_name: geninv
restart: always
ports:
- "8000:80"
volumes:
- ./:/var/www/html
networks:
default:
networks:
default:
external:
name: mynet
I am using mysql 8 with external volume on mynet network
Thanks in Advance.
A very odd overlook by me. Simple
php artisan config:cache
re created the cache and every thing went smooth after it.

"docker-compose up" Error : can not start service php: OCI runtime create failed: container_linux.go:345

I have installed docker on ubuntu and mac os. "docker-composer up" command works as expected on ubuntu and mac.
But on windows 8, I have installed docker toolbox and run "docker-compose up" command. I'm getting error while creating php-container.
Below is the screenshot of error:
Below is the docker-compose.yml file
version: "3.2"
services:
php:
build: './php/'
container_name: php-container
networks:
- backend
volumes:
- ./code/:/var/www/html/
- ./php/config/php.ini:/usr/local/etc/php/conf.d/php.ini
- ./php/config/mcrypt.so:/usr/local/lib/php/extensions/no-debug-non-zts-20151012/mcrypt.so
links:
- redis:redis
nginx:
container_name: nginx-container
image: nginx:latest
depends_on:
- php
- mysql
ports:
- "80:80"
networks:
- frontend
- backend
volumes:
- ./code/:/var/www/html/
- ./site.conf:/etc/nginx/conf.d/default.conf
links:
- php
mysql:
image: mysql:5.6.40
container_name: mysql-container
networks:
- backend
environment:
- MYSQL_ROOT_PASSWORD=password
volumes:
- /var/lib/postgresql/data
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin-container
networks:
- backend
depends_on:
- mysql
links:
- mysql:mysql
ports:
- 8088:80
environment:
PMA_HOST: mysql
MYSQL_USERNAME: abcxyz
MYSQL_ROOT_PASSWORD: 321654987
networks:
frontend:
backend:
Below is the dockerfile for php
FROM php:7.0.32-fpm
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
&& docker-php-ext-install -j$(nproc) iconv \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd
RUN docker-php-ext-install mysqli
RUN pecl install -o -f redis \
&& rm -rf /tmp/pear \
&& docker-php-ext-enable redis
RUN apt-get -qq update && apt-get -qq -y install \
automake \
cmake \
g++ \
git \
libicu-dev \
libmagickwand-dev \
libpng-dev \
librabbitmq-dev \
libreadline-dev \
pkg-config \
ssh-client \
supervisor \
zlib1g-dev \
&& docker-php-ext-install \
bcmath \
gd \
intl \
opcache \
pdo_mysql \
sockets \
zip \
&& git clone git://github.com/alanxz/rabbitmq-c.git \
&& cd rabbitmq-c \
&& mkdir build && cd build \
&& cmake -DENABLE_SSL_SUPPORT=OFF .. \
&& cmake --build . --target install \
&& pecl install amqp imagick xdebug igbinary \
&& docker-php-ext-enable amqp imagick xdebug igbinary \
&& version=$(php -r "echo PHP_MAJOR_VERSION.PHP_MINOR_VERSION;") \
&& curl -A "Docker" -o /tmp/blackfire-probe.tar.gz -D - -L -s https://blackfire.io/api/v1/releases/probe/php/linux/amd64/$version \
&& mkdir -p /tmp/blackfire \
&& tar zxpf /tmp/blackfire-probe.tar.gz -C /tmp/blackfire \
&& mv /tmp/blackfire/blackfire-*.so $(php -r "echo ini_get('extension_dir');")/blackfire.so \
&& printf "extension=blackfire.so\nblackfire.agent_socket=tcp://blackfire:8707\n" > $PHP_INI_DIR/conf.d/blackfire.ini \
&& curl -A "Docker" -L https://blackfire.io/api/v1/releases/client/linux_static/amd64 | tar zxp -C /tmp/blackfire \
&& mv /tmp/blackfire/blackfire /usr/bin/blackfire \
&& rm -rf /tmp/blackfire /tmp/blackfire-probe.tar.gz \
&& rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y \
libmcrypt-dev \
&& docker-php-ext-install -j$(nproc) iconv mcrypt \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd
RUN apt-get install -y libxslt-dev
RUN apt-get install -y zlibc
RUN docker-php-ext-install xsl
RUN curl -s https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer
Same error here...
In my case in the docker-compose.yml file
apache:
build: '.\apache\'
container_name: apache-container
depends_on:
- php
- mysql
networks:
- frontend
- backend
ports:
- "80:80"
volumes:
- .\public_html\:/var/www/html/
- .\apache\demo.apache.conf:/usr/local/apache2/conf/demo.apache.conf
- .\apache\custom.conf:/usr/local/apache2/conf/test.conf
In the last line - .\apache\custom.conf:/usr/local/apache2/conf/test.conf i'm copying custom conf to test conf in docker. which is working as expected.
But when I change the test.conf to httpd.conf and run docker-compose up , the same error occur as shown in above screenshot in the question.
Error:
Error screenshot
So, If I check files on apache container, I can see test.conf file also there is httpd.conf file, but when I trying to replace httpd.conf file it is showing error.

Error when trying to up docker containers

I'm on this issue since 2 days, trying a lot of solutions found on internet but no one resolved my case... I'm frustrated, I hope someone could help me.
I'm on Linux, and I don't really understand anything with server ports, hosts, etc.
It seems that this project uses API Platform, I don't know if it could help or not.
I can't figure out how to launch this project I just received. I have an issue when launching containers (docker-compose up) :
Error thrown while running command "doctrine:schema:update -f". Message: "An exception occurred in driver: SQLSTATE[08006] [7] could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
php_1 | TCP/IP connections on port 5432?
php_1 | could not connect to server: Address not available
php_1 | Is the server running on host "localhost" (::1) and accepting
php_1 | TCP/IP connections on port 5432?"
It seems that the container can't communicate with the DB container. But I don't know exactly what to do to solve this.
There is several containers in the docker-composer.yml :
version: '3'
services:
php:
build:
context: ./api
depends_on:
- db
env_file:
- ./api/.env
# Comment out these volumes in production
volumes:
- ./api:/srv/api:rw,cached
# If you develop on Linux, comment out the following volumes to just use bind-mounted project directory from host
- /srv/api/var/
- /srv/api/var/cache/
- /srv/api/var/logs/
- /srv/api/var/sessions/
api:
build:
context: ./api
dockerfile: ./Dockerfile.nginx
depends_on:
- php
ports:
- "8080:80"
volumes:
- ./api/public:/srv/api/public:ro
cache-proxy:
build:
context: ./api
dockerfile: ./Dockerfile.varnish
depends_on:
- api
# Comment out this volume in production
volumes:
- ./api/docker/varnish/conf:/etc/varnish:ro
ports:
- "8081:80"
db:
# In production, you may want to use a managed database service
image: postgres:9.6-alpine
environment:
- POSTGRES_DB=api
- POSTGRES_USER=api-platform
# You should definitely change the password in production
- POSTGRES_PASSWORD=!ChangeMe!
volumes:
- db-data:/var/lib/postgresql/data:rw
# You may use a bind-mounted host directory instead, so that it is harder to accidentally remove the volume and lose all your data!
# - ./docker/db/data:/var/lib/postgresql/data:rw
ports:
- "5432:5432"
client:
# Use a static website hosting service in production
# See https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#deployment
build:
context: ./client
dockerfile: ./Dockerfile
env_file:
- ./client/.env
volumes:
- ./client:/usr/src/client:rw,cached
- /usr/src/client/node_modules
ports:
- "80:3000"
h2-proxy:
# Don't use this proxy in prod
build:
context: ./h2-proxy
dockerfile: ./Dockerfile
depends_on:
- client
- api
- cache-proxy
ports:
- "443:443"
- "444:444"
- "8443:8443"
- "8444:8444"
volumes:
db-data: {}
Dockerfile :
ARG PHP_VERSION=7.2
ARG ALPINE_VERSION=3.7
FROM php:${PHP_VERSION}-fpm-alpine${ALPINE_VERSION}
RUN apk add --no-cache \
git
ARG APCU_VERSION=5.1.11
RUN set -xe \
&& apk add --no-cache --virtual .build-deps \
$PHPIZE_DEPS \
icu-dev \
postgresql-dev \
zlib-dev \
&& docker-php-ext-install -j$(nproc) \
intl \
pdo_pgsql \
zip \
&& pecl install \
apcu-${APCU_VERSION} \
&& docker-php-ext-enable --ini-name 20-apcu.ini apcu \
&& docker-php-ext-enable --ini-name 05-opcache.ini opcache \
&& runDeps="$( \
scanelf --needed --nobanner --format '%n#p' --recursive /usr/local/lib/php/extensions \
| tr ',' '\n' \
| sort -u \
| awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
)" \
&& apk add --no-cache --virtual .api-phpexts-rundeps $runDeps \
&& apk del .build-deps
RUN apk add --no-cache freetype libpng libjpeg-turbo freetype-dev libpng-dev libjpeg-turbo-dev && \
docker-php-ext-configure gd \
--with-gd \
--with-freetype-dir=/usr/include/ \
--with-png-dir=/usr/include/ \
--with-jpeg-dir=/usr/include/ && \
NPROC=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1) && \
docker-php-ext-install -j${NPROC} gd exif && \
apk del --no-cache freetype-dev libpng-dev libjpeg-turbo-dev
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
COPY docker/php/php.ini /usr/local/etc/php/php.ini
# https://getcomposer.org/doc/03-cli.md#composer-allow-superuser
ENV COMPOSER_ALLOW_SUPERUSER 1
RUN composer global require "hirak/prestissimo:^0.3" --prefer-dist --no-progress --no-suggest --classmap-authoritative
RUN apk add --no-cache ffmpeg
WORKDIR /srv/api
# Prevent Symfony Flex from generating a project ID at build time
ARG SYMFONY_SKIP_REGISTRATION=1
# Prevent the reinstallation of vendors at every changes in the source code
COPY composer.json composer.lock ./
RUN composer install --prefer-dist --no-dev --no-autoloader --no-scripts --no-progress --no-suggest \
&& composer clear-cache
COPY . ./
RUN mkdir -p var/cache var/logs var/sessions \
&& composer dump-autoload --classmap-authoritative --no-dev \
&& chown -R www-data var
COPY docker/php/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
RUN chmod +x /usr/local/bin/docker-entrypoint
ENTRYPOINT ["docker-entrypoint"]
CMD ["php-fpm"]
.env file :
APP_ENV=dev
APP_SECRET=!ChangeMe!
TRUSTED_PROXIES=10.0.0.0/8,172.16.0.0/12,192.168.0.0/16
TRUSTED_HOSTS=localhost,api
DATABASE_URL=pgsql://api-platform:!ChangeMe!#localhost/api
CORS_ALLOW_ORIGIN=^https?://localhost:?[0-9]*$
VARNISH_URL=http://cache-proxy
JWT_PRIVATE_KEY_PATH=config/jwt/private.pem
JWT_PUBLIC_KEY_PATH=config/jwt/public.pem
JWT_PASSPHRASE=symfony
JWT_TOKEN_TTL=null
MAILER_URL=gmail://email:password#localhost?
MAILER_FROM=user#example.com
CLIENT_MAGIC_LINK_URL=https://localhost/login
FACEBOOK_CLIENT_ID=433898047056774
FACEBOOK_CLIENT_SECRET=90ac80e435405a2ffeaab8a242981152
AUTH_API_KEY=123456789
ADMIN_PWD=$2y$13$vqd7AMd0A5eetDT00qLd2OkGG8T9UJ1gLsD2huOhk3iRwGCBqR3iu
I don't know if you guys needs something more. Just tell me what you need.
Thanks a lot...
You have to use the db as the database host, not localhost in the DATABASE_URL application env.

Categories