0

I'm new at Docker and I'm facing a problem.

I've mounted a BE with Laravel 10 in a docker container, without sail, with php 8.1. I've also, in my docker compose, two FE in Vue.

Here the interesting parts:

Dockerfile

FROM php:8.1-apache

RUN docker-php-ext-install opcache pdo_mysql \
    && docker-php-ext-enable opcache

COPY opcache.ini /usr/local/etc/php/conf.d/opcache.ini

RUN a2enmod rewrite \
    && sed -i 's!/var/www/html!/var/www/html/public!g' /etc/apache2/sites-available/000-default.conf

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

COPY . /var/www/html/

WORKDIR /var/www/html

RUN composer install --no-interaction --prefer-dist --optimize-autoloader

RUN chown -R www-data:www-data /var/www/html/storage \
    && chown -R www-data:www-data /var/www/html/bootstrap/cache

EXPOSE 80

docker-compose.yml

...
services:
  api:
    build:
      context: ./api
    container_name: api
    restart: unless-stopped
    ports:
      - "8000:80"
    environment:
      APP_URL: http://localhost:8000
      DB_HOST: db
      DB_PORT: 3306
      DB_USERNAME: ****
      DB_PASSWORD: ****
      DB_DATABASE: ****
      SANCTUM_STATEFUL_DOMAINS: localhost,localhost:8080,localhost:8081
    volumes:
      - backend_data:/var/www/html
    networks:
      - app-network
    depends_on:
      - db
...

networks:
  app-network:
    driver: bridge

volumes:
  db-api:
  backend_data:
    driver: local
    driver_opts:
      type: none
      device: ./api
      o: bind

Everythings works fine but the backend is too slow (about 4/5 second per api call). I've notice that if I don't mount the volume and acceed the file directly in the containers, it is fast as normal (less than 100ms per call).

What am I doing wrong?

I've tried mounting a custom volume for datas, changing the path of the mounted volume, using php-fpm in Dockerfile

Fabev6
  • 31
  • 3
  • 1
    Are you using Docker Desktop? Bind mounts are known to be not so fast in many environments, for example [Docker is extremely slow when running Laravel on Nginx container wsl2](https://stackoverflow.com/questions/63036490/docker-is-extremely-slow-when-running-laravel-on-nginx-container-wsl2) or [Docker in MacOs is very slow](https://stackoverflow.com/questions/55951014/docker-in-macos-is-very-slow). – David Maze May 09 '23 at 10:35
  • @DavidMaze yes I'm using docker desktop, I'm on win11. I've read the links you attached but I can't disable the WSL2 checkbox – Fabev6 May 09 '23 at 10:54
  • 1
    WSL2 is required to run docker on windows because docker requires a linux kernel to run as far as I know. If you disable WSL2 in Docker desktop it will fallback to WSL 1 (which you might not have installed and is not necessarily going to fix any issues) – apokryfos May 09 '23 at 10:57
  • For WSL/WSL2 There is a ginormous difference between having your application on `/mnt/c/projects/laravel` or in the proper Linux part of WSL, just keep that in mind and don't use `/mnt/c`, etc – UnderDog May 09 '23 at 11:16
  • @UnderDog I've currently my project files on windows filesystem, not on wsl, I've read that it doesn't afflict performance significantly and I haven't configured it. Should I? – Fabev6 May 10 '23 at 08:27
  • 1
    Move your files to the Linux part of WSL. I'll assume you chose Ubuntu when you installed WSL, so ... open the new Windiws terminal, it will let you switch to Ubuntu and it will Gove you a Linux prompt. Copy your files from /mnt/c/wherever to /home/user/directory. Then do your docker desktop thing and see if things get faster. with /mnt/c/wherever you've *mounted* that directory to your Linux, then ... you're *mounting* that *mounted* directory to your docket volume. That's the slowness – UnderDog May 10 '23 at 16:22
  • 1
    @UnderDog solved :') – Fabev6 May 11 '23 at 07:30

1 Answers1

1

Got it :') as sad by UnderDog in comments, I've installed WSL, placed my files under /home/user/whatever and runned docker from there.

Fabev6
  • 31
  • 3