Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker is extremely slow when running Laravel on Nginx container wsl2

I've updated Windows 10 to 2004 latest version, installed wsl2 and updated it, installed docker, and ubuntu.

When I create a simple index.php file with "Hello World" it's working perfectly ( response: 100-400ms ) but when I added my Laravel project it becomes miserable as it loads for 7sec before performing the request and the response is 4 - 7 seconds😢, even though PHPMyAdmin is running very smoothly ( response: 1 - 2 seconds ).

my docker-compose.yml file:

version: '3.8' networks:   laravel:  services:   nginx:     image: nginx:stable-alpine     container_name: nginx     ports:       - "8080:80"     volumes:       - ./src:/var/www/html       - ./nginx/default.conf:/etc/nginx/conf.d/default.conf     depends_on:       - php       - mysql       - phpmyadmin     networks:       - laravel    mysql:     image: mysql:latest     container_name: mysql     restart: unless-stopped     tty: true     ports:       - "3306:3306"     environment:       MYSQL_ROOT_PASSWORD: secret       SERVICE_TAGS: dev       SERVICE_NAME: mysql     networks:       - laravel    phpmyadmin:     image: phpmyadmin/phpmyadmin     restart: always     depends_on:       - mysql     ports:       - 8081:80     environment:       PMA_HOST: mysql       PMA_ARBITRARY: 1    php:     build:       context: .       dockerfile: Dockerfile     container_name: php     volumes:       - ./src:/var/www/html     ports:       - "9000:9000"     networks:       - laravel    composer:     image: composer:latest     container_name: composer     volumes:       - ./src:/var/www/html     working_dir: /var/www/html     depends_on:       - php     networks:       - laravel    npm:     image: node:latest     container_name: npm     volumes:       - ./src:/var/www/html     working_dir: /var/www/html     entrypoint: ['npm']    artisan:     build:       context: .       dockerfile: Dockerfile     container_name: artisan     volumes:       - ./src:/var/www/html     depends_on:       - mysql     working_dir: /var/www/html     entrypoint: ['php', '/var/www/html/artisan']     networks:       - laravel 

I've been trying to fix this issue for 2 days but couldn't find the answer.

Thanks

like image 531
Wail Hayaly Avatar asked Jul 22 '20 14:07

Wail Hayaly


2 Answers

It looks like you are mounting your Laravel project in your container. This could result in very poor file I/O if you are mounting these files from your Windows environment to WSL 2, since WSL 2 currently has a lot of problems accessing files that are on the Windows environment. This I/O issue exists as of July 2020, you can find the ongoing status of the issue on Github here.

There are three possible solutions I can think of that will resolve this issue for now.


Disable WSL 2 based engine for docker until the issue is resolved

Since this issue only occurs when WSL 2 tries to access the Windows filesystem, you could choose to disable WSL 2 docker integration and run your containers on your Windows environment instead. You can find the option to disable it in the UI of Docker Desktop here: enter image description here


Store your project in the Linux filesystem of WSL 2

Again, since this issue occurs when WSL 2 tries to access the mount points of the Windows filesystem under /mnt, you could choose to store your project onto the Linux filesystem of WSL 2 instead.


Build your own Dockerfiles

You could choose to create your own Dockerfiles and instead of mounting your project, you can COPY the desired directories into the docker images instead. This would result in poor build performance, since WSL 2 will still have to access your Windows filesystem in order to build these docker images, but the runtime performance will be much better, since it won't have to retrieve these files from the Windows environment everytime.

like image 150
octagon_octopus Avatar answered Sep 28 '22 07:09

octagon_octopus


Ok, so i got an interesting fact :))

Running docker on windows without WSL2.

A request has TTFB 5.41s. This is the index.php file. I used die() to check where the time is bigger and i found that if i use die() after terminate, the TTFB becomes ~2.5s.

<?php /**  * Laravel - A PHP Framework For Web Artisans  *  * @package  Laravel  * @author   Taylor Otwell <[email protected]>  */  define('LARAVEL_START', microtime(true));  require __DIR__.'/../../application/vendor/autoload.php';  $app = require_once __DIR__.'/../../application/bootstrap/app.php';  #die(); <-- TTFB 1.72s $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);  $response = $kernel->handle(     $request = Illuminate\Http\Request::capture() );  $response->send();  #die(); <-- TTFB 2.67s  $kernel->terminate($request, $response);  #die(); <-- TTFB 2.74s  #if there is no die in the file then TTFB is ~6s 
like image 45
radu Avatar answered Sep 28 '22 07:09

radu