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

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

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

version: '3'
services:
  mariadb:
      image: mariadb
      restart: always
      ports:
        - 3375:3306
      environment:
        TZ: "Europe/Bucharest"
        MARIADB_ALLOW_EMPTY_PASSWORD: "no"
        MARIADB_ROOT_PASSWORD: "user@pass"
        MARIADB_ROOT_HOST: "%"
        MARIADB_USER: 'user'
        MARIADB_PASSWORD: 'pass'
        MARIADB_DATABASE: 'db'
      networks:
        - sail
      volumes:
      - ./docker-config/server_bd:/var/lib/mysql

  app:
    build: ./docker-config
    container_name: app
    ports:
        - 8000:80        
        - 4430:443
    volumes:
        - "./:/var/www/html"
    tty: true
    networks:
       - sail
    links:
      - mariadb
    depends_on:
      - mariadb

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    restart: always
    networks:
      - sail
    links:
      - "mariadb:db"
    depends_on:
      - mariadb
    environment:
      UPLOAD_LIMIT: 1024M
    ports:
      - 3971:80    

networks:
    sail:
      driver: bridge

volumes:
  data:
    driver: local

Dockerfile

FROM ubuntu:20.04

EXPOSE 80

WORKDIR /var/www/html

ENV DEBIAN_FRONTEND noninteractive
ENV TZ=Europe/Bucharest
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN apt-get update -y
RUN apt-get upgrade -y

RUN apt install -y lsb-release
RUN apt-get clean all

RUN apt-get install ca-certificates apt-transport-https -y
RUN apt-get install software-properties-common -y

# 其余部分内容省略

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

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

英文:

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

version: '3'
services:
  mariadb:
      image: mariadb
      restart: always
      ports:
        - 3375:3306
      environment:
        TZ: "Europe/Bucharest"
        MARIADB_ALLOW_EMPTY_PASSWORD: "no"
        MARIADB_ROOT_PASSWORD: "user@pass"
        MARIADB_ROOT_HOST: "%"
        MARIADB_USER: 'user'
        MARIADB_PASSWORD: 'pass'
        MARIADB_DATABASE: 'db'
      networks:
        - sail
      volumes:
      - ./docker-config/server_bd:/var/lib/mysql

  app:
    build: ./docker-config
    container_name: app
    ports:
        - 8000:80        
        - 4430:443
    volumes:
        - "./:/var/www/html"
    tty: true
    networks:
       - sail
    links:
      - mariadb
    depends_on:
      - mariadb

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    restart: always
    networks:
      - sail
    links:
      - "mariadb:db"
    depends_on:
      - mariadb
    environment:
      UPLOAD_LIMIT: 1024M
    ports:
      - 3971:80    
  
networks:
    sail:
      driver: bridge

volumes:
  data:
    driver: local

Dockerfile

FROM ubuntu:20.04

EXPOSE 80

WORKDIR /var/www/html

ENV DEBIAN_FRONTEND noninteractive
ENV TZ=Europe/Bucharest
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN apt-get update -y
RUN apt-get upgrade -y

RUN apt install -y lsb-release
RUN apt-get clean all

RUN apt-get install ca-certificates apt-transport-https -y
RUN apt-get install software-properties-common -y

# Apache Server
RUN apt-get -y install apache2
RUN a2enmod rewrite
RUN service apache2 restart

# SETUP SSL
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf &&\
    a2enmod rewrite &&\
    a2enmod ssl

COPY cert/certificate.crt /etc/ssl/certificate.crt
COPY cert/private.key /etc/ssl/private.key
COPY cert/ca_bundle.crt /etc/ssl/ca_bundle.crt

COPY 000-default.conf /etc/apache2/sites-available/000-default.conf
RUN service apache2 restart

RUN apt-get install -y wget

RUN apt-get install nano

RUN apt-get update -y

RUN apt-get install -y apt-transport-https

# PHP8
RUN add-apt-repository ppa:ondrej/php
RUN apt-get install -y php8.1
RUN apt-get install -y php8.1-fpm
RUN apt-get install -y libapache2-mod-php
RUN apt-get install -y libapache2-mod-fcgid
RUN apt-get install -y curl
RUN apt-get install -y php-curl
RUN apt-get install -y php-dev
RUN apt-get install -y php-gd
RUN apt-get install -y php-mbstring
RUN apt-get install -y php-zip
RUN apt-get install -y php-xml
RUN apt-get install -y php-soap
RUN apt-get install -y php-common
RUN apt-get install -y php-tokenizer
RUN apt-get install -y unzip
RUN apt-get install -y php-bcmath
RUN apt-get install -y php-mysql

# Install npm
RUN apt-get install -y npm
RUN npm cache clean -f
RUN npm install -g n
RUN n stable

RUN service apache2 restart

# COMPOSER
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN echo " extension = php_mysqli.so"  >> /etc/php/8.1/apache2/php.ini

RUN service apache2 restart

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文件夹中,如

C:\Projects\FooBar

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

/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

C:\Projects\FooBar

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

/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:

确定