11# Dockerfile for PHP Swoole CRUD Microservice
22#
33# This Dockerfile builds a PHP 8.4 CLI image with Swoole, Redis, Xdebug, Opcache, and other required extensions.
4- # It uses a multi-stage build to compile Swoole and extensions, then creates a lightweight runtime image.
4+ # It uses a multi-stage build to compile Swoole and extensions, then creates a runtime image with all extensions included .
55#
6- # Stages:
7- # 1. build: Compiles Swoole and PHP extensions, installs Composer.
8- # 2. runtime: Copies compiled extensions and Composer, installs runtime dependencies, and sets up the app.
6+ # Supervisor is used to run the PHP process and manage restarts.
97#
108# Exposes ports for Swoole HTTP servers.
119#
1210# Usage:
1311# docker build -t php-swoole-crud-microservice .
1412# docker run -p 9501:9501 php-swoole-crud-microservice
1513
16- # ================= Build Stage =================
17- FROM php:8.4-cli AS build
14+ # ================= Base Stage =================
15+ # Contains common runtime dependencies
16+ FROM php:8.4-cli AS base
1817
19- # --- System dependencies ---
20- # Installs build tools and libraries required for compiling Swoole and PHP extensions.
21- # Includes nghttp2 and zlib to ensure HTTP/2 and compression support are built in automatically.
18+ # --- Runtime system dependencies ---
2219RUN apt-get update && apt-get install -y \
23- autoconf \
24- build-essential \
2520 git \
2621 libbrotli-dev \
2722 libc-ares-dev \
@@ -31,104 +26,81 @@ RUN apt-get update && apt-get install -y \
3126 libsqlite3-dev \
3227 libssl-dev \
3328 libzip-dev \
34- pkg-config \
29+ supervisor \
3530 unzip \
36- zlib1g-dev \
3731 && rm -rf /var/lib/apt/lists/*
3832
39- RUN docker-php-ext-install sockets opcache
33+ # Create supervisor log directory
34+ RUN mkdir -p /var/log/supervisor
35+
36+ # ================= Build Stage =================
37+ FROM base AS build
4038
41- # --- Build Swoole from source with required features ---
42- # Deprecated options removed; HTTP/2, zlib, mysqlnd auto-enabled if dependencies are present.
39+ # --- Build-only dependencies ---
40+ RUN apt-get update && apt-get install -y \
41+ autoconf \
42+ build-essential \
43+ pkg-config \
44+ && rm -rf /var/lib/apt/lists/*
45+
46+ # --- PHP extensions ---
47+ RUN docker-php-ext-install sockets opcache pdo pdo_mysql pdo_sqlite
48+
49+ # --- Build Swoole from source with caching ---
4350ARG SWOOLE_VERSION=6.0.0
44- RUN git clone -b v${SWOOLE_VERSION} https://github.com/swoole/swoole-src.git /usr/src/swoole \
51+ RUN --mount=type=cache,target=/tmp/swoole-build \
52+ git clone -b v${SWOOLE_VERSION} https://github.com/swoole/swoole-src.git /usr/src/swoole \
4553 && cd /usr/src/swoole \
4654 && phpize \
47- && ./configure \
48- --enable-openssl \
49- --enable-sockets \
50- --enable-swoole-curl \
51- --enable-cares \
52- --enable-mysqlnd \
55+ && ./configure --enable-openssl --enable-sockets --enable-swoole-curl --enable-cares --enable-mysqlnd \
5356 && make -j$(nproc) && make install \
5457 && docker-php-ext-enable swoole opcache
5558
56- # --- PHP extensions ---
57- # Installs PDO extensions for MySQL and SQLite.
58- RUN docker-php-ext-install pdo pdo_mysql pdo_sqlite
59-
60- # In build stage
61- # RUN git clone https://github.com/OpenSwoole/mysql.git /usr/src/openswoole-mysql \
62- # && cd /usr/src/openswoole-mysql \
63- # && phpize \
64- # && ./configure \
65- # && make -j$(nproc) \
66- # && make install \
67- # && docker-php-ext-enable openswoole_mysql
68-
69- # --- phpredis extension ---
70- # Installs and enables phpredis via PECL.
71- RUN pecl install redis \
72- && docker-php-ext-enable redis
73-
74- # --- Xdebug extension ---
75- # Installs and enables Xdebug for debugging.
76- RUN pecl install xdebug \
77- && docker-php-ext-enable xdebug
59+ # --- PECL extensions with caching ---
60+ RUN --mount=type=cache,target=/tmp/pecl \
61+ pecl install redis xdebug \
62+ && docker-php-ext-enable redis xdebug
7863
7964# --- Composer ---
80- # Copies Composer binary from official Composer image.
8165COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
8266
83- # ================= Runtime Stage =================
84- FROM php:8.4-cli
67+ # --- Set working directory ---
68+ WORKDIR /app
8569
86- # --- Runtime system dependencies ---
87- # Installs only the libraries required at runtime.
88- RUN apt-get update && apt-get install -y \
89- git \
90- libbrotli-dev \
91- libc-ares-dev \
92- libcurl4-openssl-dev \
93- libnghttp2-dev \
94- libpq-dev \
95- libsqlite3-dev \
96- libssl-dev \
97- libzip-dev \
98- pkg-config \
99- unzip \
100- zlib1g-dev \
101- && rm -rf /var/lib/apt/lists/*
70+ # --- Copy only composer files first for caching ---
71+ COPY composer.json composer.lock* ./
10272
103- # --- Copy compiled PHP extensions and Composer ---
104- COPY --from=build /usr/local/lib/php/extensions/ /usr/local/lib/php/extensions/
105- COPY --from=build /usr/local/etc/php/conf.d/ /usr/local/etc/php/conf.d/
106- COPY --from=build /usr/bin/composer /usr/bin/composer
73+ # --- Install application dependencies with caching ---
74+ RUN --mount=type=cache,target=/root/.composer/cache \
75+ composer install --no-dev --optimize-autoloader --prefer-dist
76+
77+ # --- Ensure logs directory exists ---
78+ RUN mkdir -p /app/logs
79+
80+ # ================= Runtime Stage =================
81+ FROM base AS runtime
10782
10883# --- Set working directory ---
10984WORKDIR /app
11085
111- # --- Composer install ---
112- # Copies composer.json and optionally composer.lock, then installs dependencies.
113- COPY composer.json ./
114- RUN if [ -f composer.lock ]; then \
115- cp composer.lock composer.lock && \
116- composer install --no-dev --optimize-autoloader || true; \
117- fi
86+ # --- Copy compiled PHP extensions and Composer/vendor from build stage ---
87+ COPY --from=build /usr/local/lib/php/extensions/ /usr/local/lib/php/extensions/
88+ COPY --from=build /usr/local/etc/php/conf.d/ /usr/local/etc/php/conf.d/
89+ COPY --from=build /app/vendor /app/vendor
90+ # COPY --from=build /app /app
11891
11992# --- PHP configuration ---
120- # Copies custom PHP configuration including opcache tuning.
12193COPY docker/php/php.ini /usr/local/etc/php/conf.d/zz-preload.ini
12294COPY docker/php/opcache.ini /usr/local/etc/php/conf.d/opcache.ini
12395
124- # --- Copy application source code ---
125- COPY . .
96+ # --- Supervisor configuration ---
97+ COPY docker/supervisord.conf /etc/supervisor/conf.d/swoole.conf
12698
127- # --- Ensure logs directory exists ---
128- RUN mkdir -p /app/logs
99+ # --- Copy all application source code in a single step ---
100+ COPY . .
129101
130102# --- Expose Swoole HTTP and custom ports ---
131103EXPOSE 9501 9310 9502
132104
133105# --- Default command ---
134- CMD ["php" , "public/index.php " ]
106+ CMD ["/usr/bin/supervisord" , "-c" , "/etc/supervisor/conf.d/swoole.conf " ]
0 commit comments