2

I was developing an app locally using Laravel. For testing I was using Pest and tests were running fast, really fast. That was until I decided to Dockerize my app, now tests are running pretty slow.

What it used to run in 3 seconds now it's running in over a minute.

Here's my Dockerfile:

FROM php:8.1.12-fpm

ARG uid=1000
ARG user=macgiver

RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    zip \
    unzip

# Install and enable xDebug
RUN pecl install xdebug \
    && docker-php-ext-enable xdebug

RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install php modules required by laravel.
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd

# Create system user to run Composer and Artisan commands.
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
    chown -R $user:$user /home/$user

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

# Set working directory
WORKDIR /var/www

USER $user

and this is my docker-compose:

version: "3.9"
services:
  app:
    build:
      context: ./
      dockerfile: Dockerfile
    image: dmc
    container_name: dmc-app
    restart: unless-stopped
    working_dir: /var/www/
    depends_on:
      - db
      - nginx
    volumes:
      - ./:/var/www/
      - ./docker/php/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
      - ./docker/php/conf.d/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini
    expose:
      - "9003"
    networks:
      - dmc-net

  nginx:
    image: nginx:1.23.2-alpine
    container_name: dmc-nginx
    restart: unless-stopped
    ports:
      - "8000:80"
    volumes:
      - ./:/var/www
      - ./docker-compose/nginx:/etc/nginx/conf.d
    networks:
      - dmc-net

  db:
    image: mysql:8.0.31
    container_name: dmc-db
    restart: unless-stopped
    # using 3307 on the host machine to avoid collisions in case there's a local MySQL instance installed already.
    ports:
      - "3307:3306"
    # use the variables declared in .env file
    environment:
      MYSQL_HOST: ${DB_HOST}
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_ROOT_PASSWORD: abcd1234
      MYSQL_USER: ${DB_USERNAME}
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - ./docker-compose/mysql:/docker-entrypoint-initdb.d
      - mysql-data:/var/lib/mysql
    networks:
      - dmc-net

networks:
  dmc-net:
    driver: bridge

volumes:
  mysql-data:

Any ideas?

Code4R7
  • 2,600
  • 1
  • 19
  • 42
MrCujo
  • 1,218
  • 3
  • 31
  • 56
  • 2
    Are you working on a Mac or on Windows? wsl2? – UnderDog Dec 24 '22 at 05:28
  • Mac. I've seen some post where wsl2 is mention, but it's my understanding that is for windows. – MrCujo Dec 24 '22 at 15:20
  • Correct, wsl2 is for Windows. Mac & docker is sometimes slow. In 1 of my docker projects, i had this comment: `# If you are using Docker Sync. For `osx` use 'native_osx' DOCKER_SYNC_STRATEGY=native_osx` I'll get back to that docker sync part. Maybe you're not using it. Focus on `mount volume on Mac is slow`, maybe stackoverflow has some topics on that Looks like you're going to need docker-sync: https://dev.to/kovah/cut-your-docker-for-mac-response-times-in-half-with-docker-sync-1e8j – UnderDog Dec 24 '22 at 15:47
  • 1
    Unfortunately docker on mac is slow because of the way how it's implemented. [Here](https://www.docker.com/blog/speed-boost-achievement-unlocked-on-docker-desktop-4-6-for-mac/) is a blog about speed boost updates in docker desktop and you can try to see if that helps. If that doesn't help you can google [mac docker slow](https://www.google.com/search?q=mac+docker+slow&oq=mac+docker+slow) and check some of the results. You can try using `cached` on your volume. You can try projects like `mutagen`. Or you can also try `native_osx` as the @UnderDog suggested – ljubadr Dec 30 '22 at 04:21
  • 1
    What makes docker slow on mac is using bind mount - mapping your local folder to folder in docker container. You could avoid mounting local files by creating your app with empty volume and then `git clone` your code into the volume or copy your code from your mac to the volume. With `vscode` you can use [VS Code Remote Development](https://code.visualstudio.com/docs/remote/remote-overview). With this setup your code is inside the volume and your vscode connects to your container which makes docker on mac fast. I've used this approach multiple times and it works like a charm – ljubadr Dec 30 '22 at 05:24
  • But you will need to do some configuration for your container: copy ssh keys so that you can access repository, maybe install `zsh`, aliases, setup extensions that are installed inside the container, etc. It takes some time to configure, but it's worth the effort – ljubadr Dec 30 '22 at 05:26
  • [Dotfiles](https://dotfiles.github.io/) can be useful to setup the container – ljubadr Dec 30 '22 at 05:26

0 Answers0