Search code examples
phplaraveldockerdocker-composeswoole

Significant Performance Loss with Swoole, Laravel 9, and Docker on Rocky Linux 9 Using Custom Networks


I've been running a Laravel 9 application with Swoole for my microservices. Everything works great until I containerize the application with Docker on Rocky Linux 9. I've noticed a drastic performance degradation when using custom Docker networks.

Setup Details:

Host OS: Rocky Linux 9

Laravel Version: 9

Swoole Version: 4.8.4

Custom Docker Networks: backend, frontend, mongodb

Stack

Backend Microservices: Laravel 9 Frontend: ReactJS Database: MongoDB Cache: Redis Web server for frontend: Nginx Backend server: Swoole Each application have their own container.

Observations:

The application runs smoothly with an API call latency of around 63ms when using network_mode = host.

When using custom Docker networks (backend, frontend, mongodb), the same API call takes more than 6000ms.

Steps Taken:

Checked Docker host resources.

Reviewed volume mounts and tried using Docker's delegated and cached mount options.

Verified Swoole and Laravel configurations.

Experimented with different Docker network configurations.

I'm puzzled by this drastic performance difference, and I am looking for any insights or suggestions to optimize the performance of my setup. Has anyone else experienced similar issues or have any advice on how to troubleshoot this?

Thank you for your time and assistance!

Update

Dockerfile

FROM php:8.0-alpine AS php
WORKDIR /app
RUN apk add --no-cache \
    autoconf \
    build-base \
    libpng-dev \
    oniguruma-dev \
    libxml2-dev \
    imagemagick-dev \
    libzip-dev \
    git \
    curl \
    unzip \
    openssl \
    zip \
    freetype-dev \
    jpeg-dev \
    libwebp-dev \
    && pecl install imagick \
    && docker-php-ext-enable imagick \
    && pecl install mongodb \
    && docker-php-ext-enable mongodb \
    && docker-php-ext-configure gd --with-freetype --with-webp --with-jpeg \
    && docker-php-ext-install pdo_mysql opcache gd zip \
    && pecl install swoole-4.8.4 \
    && docker-php-ext-enable swoole

# Stage - Production Image
FROM php:8.0-alpine
# Install runtime system dependencies for PHP extensions
RUN apk add --no-cache \
    libpng \
    imagemagick-libs \
    libzip\
    libgomp \
    freetype \
    jpeg \ 
    libwebp \
    libstdc++
COPY --from=php /usr/local/etc/php/conf.d/ /usr/local/etc/php/conf.d/
COPY --from=php /usr/local/lib/php/extensions/ /usr/local/lib/php/extensions/
COPY --from=php /usr/lib/ /usr/lib/
COPY . /var/www
WORKDIR /var/www
CMD ["php", "artisan", "octane:start","--port=8000", "--host=0.0.0.0"]

docker-compose.yml

version: "3"

x-shared-configuration: &shared-config

services:
    api:
    build:
        context: .
        dockerfile: Dockerfile
    image: test-api
    # network_mode: host
    restart: unless-stopped
    tty: true
    working_dir: /var/www
    ports:
      - 8000:8000

networks:
    frontend:
        external: true
    backend:
        external: true
    mongodb:
        external: true

  # volumes:
  #  storage:
  #  bootstrap_cache:
             

Solution

  • I made modifications to the .env file in my microservices. Which is responsible for routing requests to other microservices. I was using the service name (api) to forward API requests from one microservice to another. After changing service name to the IPv4 gateway (e.g. 172.20.0.1) of the external network (backend), and this change resolved the performance issue.