为什么 Laravel 应用在 Docker 上运行非常慢?

huangapple go评论85阅读模式
英文:

Why is Laravel application working very slow on Docker?

问题

我正在开发一个测验应用。我已经在 Docker 上安装了 Laravel 应用,但当我开始开发应用时,我注意到应用加载非常慢。加载一个包含 5 个输入框的表单页面大约需要 7 到 8 秒的时间。为什么 Laravel 应用在 Docker 上运行非常慢?

我知道这个等待时间很长,因为几周前我在 Docker 上安装了一个 Laravel 应用,那个应用加载速度很快。但现在我无法再使用那个应用了。我觉得问题可能出在 .yml 文件和 Dockerfile 上,但我不清楚安装问题出在哪。

由于我使用的是 W10 家庭版,无法通过 Docker UI 禁用“使用基于 WSL 2 的引擎(Windows Home 只能运行 WSL 2 后端)”。

docker-compose.yml

  1. version: '3'
  2. services:
  3. mariadb:
  4. image: mariadb
  5. restart: always
  6. ports:
  7. - 3375:3306
  8. environment:
  9. TZ: "Europe/Bucharest"
  10. MARIADB_ALLOW_EMPTY_PASSWORD: "no"
  11. MARIADB_ROOT_PASSWORD: "user@pass"
  12. MARIADB_ROOT_HOST: "%"
  13. MARIADB_USER: 'user'
  14. MARIADB_PASSWORD: 'pass'
  15. MARIADB_DATABASE: 'db'
  16. networks:
  17. - sail
  18. volumes:
  19. - ./docker-config/server_bd:/var/lib/mysql
  20. app:
  21. build: ./docker-config
  22. container_name: app
  23. ports:
  24. - 8000:80
  25. - 4430:443
  26. volumes:
  27. - "./:/var/www/html"
  28. tty: true
  29. networks:
  30. - sail
  31. links:
  32. - mariadb
  33. depends_on:
  34. - mariadb
  35. phpmyadmin:
  36. image: phpmyadmin/phpmyadmin
  37. restart: always
  38. networks:
  39. - sail
  40. links:
  41. - "mariadb:db"
  42. depends_on:
  43. - mariadb
  44. environment:
  45. UPLOAD_LIMIT: 1024M
  46. ports:
  47. - 3971:80
  48. networks:
  49. sail:
  50. driver: bridge
  51. volumes:
  52. data:
  53. driver: local

Dockerfile

  1. FROM ubuntu:20.04
  2. EXPOSE 80
  3. WORKDIR /var/www/html
  4. ENV DEBIAN_FRONTEND noninteractive
  5. ENV TZ=Europe/Bucharest
  6. RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
  7. RUN apt-get update -y
  8. RUN apt-get upgrade -y
  9. RUN apt install -y lsb-release
  10. RUN apt-get clean all
  11. RUN apt-get install ca-certificates apt-transport-https -y
  12. RUN apt-get install software-properties-common -y
  13. # 其余部分内容省略

数据库工作正常,速度很快。

笔记本电脑的性能不是问题,因为上一个应用一切正常,没有问题。

英文:

I am working on a quizz app. I have installed the Laravel app on docker and when I started working on the app I noticed that it takes very long for the app to load. It takes ~7-8s to load a page with a form which have 5 inputs.为什么 Laravel 应用在 Docker 上运行非常慢?

I know that this waiting time is very long because a few weeks ago I installed a laravel application on docker that moves very quickly. But I can no longer use that one. I think it is possible that it is due to the .yml file and the Dockerfile, but I don't know what the installation problems would be.

I can not disable the Use the WSL 2 based engine (Windows Home can only run the WSL 2 backend) from Docker UI because I run W10 Home.

docker-compose.yml

  1. version: '3'
  2. services:
  3. mariadb:
  4. image: mariadb
  5. restart: always
  6. ports:
  7. - 3375:3306
  8. environment:
  9. TZ: "Europe/Bucharest"
  10. MARIADB_ALLOW_EMPTY_PASSWORD: "no"
  11. MARIADB_ROOT_PASSWORD: "user@pass"
  12. MARIADB_ROOT_HOST: "%"
  13. MARIADB_USER: 'user'
  14. MARIADB_PASSWORD: 'pass'
  15. MARIADB_DATABASE: 'db'
  16. networks:
  17. - sail
  18. volumes:
  19. - ./docker-config/server_bd:/var/lib/mysql
  20. app:
  21. build: ./docker-config
  22. container_name: app
  23. ports:
  24. - 8000:80
  25. - 4430:443
  26. volumes:
  27. - "./:/var/www/html"
  28. tty: true
  29. networks:
  30. - sail
  31. links:
  32. - mariadb
  33. depends_on:
  34. - mariadb
  35. phpmyadmin:
  36. image: phpmyadmin/phpmyadmin
  37. restart: always
  38. networks:
  39. - sail
  40. links:
  41. - "mariadb:db"
  42. depends_on:
  43. - mariadb
  44. environment:
  45. UPLOAD_LIMIT: 1024M
  46. ports:
  47. - 3971:80
  48. networks:
  49. sail:
  50. driver: bridge
  51. volumes:
  52. data:
  53. driver: local

Dockerfile

  1. FROM ubuntu:20.04
  2. EXPOSE 80
  3. WORKDIR /var/www/html
  4. ENV DEBIAN_FRONTEND noninteractive
  5. ENV TZ=Europe/Bucharest
  6. RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
  7. RUN apt-get update -y
  8. RUN apt-get upgrade -y
  9. RUN apt install -y lsb-release
  10. RUN apt-get clean all
  11. RUN apt-get install ca-certificates apt-transport-https -y
  12. RUN apt-get install software-properties-common -y
  13. # Apache Server
  14. RUN apt-get -y install apache2
  15. RUN a2enmod rewrite
  16. RUN service apache2 restart
  17. # SETUP SSL
  18. RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf &&\
  19. a2enmod rewrite &&\
  20. a2enmod ssl
  21. COPY cert/certificate.crt /etc/ssl/certificate.crt
  22. COPY cert/private.key /etc/ssl/private.key
  23. COPY cert/ca_bundle.crt /etc/ssl/ca_bundle.crt
  24. COPY 000-default.conf /etc/apache2/sites-available/000-default.conf
  25. RUN service apache2 restart
  26. RUN apt-get install -y wget
  27. RUN apt-get install nano
  28. RUN apt-get update -y
  29. RUN apt-get install -y apt-transport-https
  30. # PHP8
  31. RUN add-apt-repository ppa:ondrej/php
  32. RUN apt-get install -y php8.1
  33. RUN apt-get install -y php8.1-fpm
  34. RUN apt-get install -y libapache2-mod-php
  35. RUN apt-get install -y libapache2-mod-fcgid
  36. RUN apt-get install -y curl
  37. RUN apt-get install -y php-curl
  38. RUN apt-get install -y php-dev
  39. RUN apt-get install -y php-gd
  40. RUN apt-get install -y php-mbstring
  41. RUN apt-get install -y php-zip
  42. RUN apt-get install -y php-xml
  43. RUN apt-get install -y php-soap
  44. RUN apt-get install -y php-common
  45. RUN apt-get install -y php-tokenizer
  46. RUN apt-get install -y unzip
  47. RUN apt-get install -y php-bcmath
  48. RUN apt-get install -y php-mysql
  49. # Install npm
  50. RUN apt-get install -y npm
  51. RUN npm cache clean -f
  52. RUN npm install -g n
  53. RUN n stable
  54. RUN service apache2 restart
  55. # COMPOSER
  56. RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
  57. RUN echo " extension = php_mysqli.so" >> /etc/php/8.1/apache2/php.ini
  58. RUN service apache2 restart
  59. CMD ( cron -f -l 8 & ) && apachectl -D FOREGROUND

The DB is working fine, at a good speed.

The laptop power is not an issue because on the last app everything was running smooth and with no problems.

答案1

得分: 2

我注意到在WSL2中,文件系统是一个问题。如果你的项目在Windows文件系统的文件夹中,它会比将整个项目放入Linux文件系统中运行慢得多。尝试将你的项目移动到Linux系统中,看看是否会加快速度。

例如,如果你的项目位于Windows文件夹中,如

  1. C:\Projects\FooBar

将它移动到WSL2下的Linux文件系统中,类似这样:

  1. /home/YourName/projects/FooBar

在你完成这个操作后,从新的位置重新构建容器。


更新

如果你的项目在文件夹 D:\Quizzology-App 中,你需要将它复制到WSL2的Linux文件系统中。

尝试以下步骤:

  1. 打开WSL2终端。
  2. 输入 cd ~ 到达Linux文件系统的主目录。
  3. 使用命令 mkdir Projects 创建一个名为 Projects 的文件夹。
  4. 进入这个文件夹,使用命令 cd Projects
  5. 使用复制命令将你的项目复制到这个文件夹,例如 cp -R /mnt/d/Quizzology-App ./Quizzology-App/
  6. 进入应用程序的Linux文件夹,输入 cd Quizzology-App,然后从这里重新构建容器。
英文:

So I noticed that in WSL2 it is the filesystem which is an issue. If your project is on a folder in the Windows file system it will run much more slower than if you put the entire project into the Linux file system. Try moving your project into the linux system and see if it speeds it up.

For example, if your project is located in a windows folder like

  1. C:\Projects\FooBar

Move it to the linux filesystem under WSL2 to something like:

  1. /home/YouName/projects/FooBar

After you have done this - rebuild the container from the new location.


Update

In case your project is in the folder D:\Quizzology-App then you need to copy it to the WSL2 Linux file system.

To do this try the following:

  1. Open WSL2 terminal
  2. Type cd ~ to go to the home directory of the linux file system.
  3. Create a folder called projects using command mkdir Projects will make the folder Projects
  4. Go into the folder using command cd Projects
  5. Use a copy command to copy your project to this folder like cp -R /mnt/d/Quizzology-App ./Quizzology-App/
  6. Enter the linux folder of the app by typing cd Quizzology-App and then rebuild the container from here.

huangapple
  • 本文由 发表于 2023年2月14日 00:09:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定