1
1
# Dockerfile for PHP Swoole CRUD Microservice
2
2
#
3
3
# 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 .
5
5
#
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.
9
7
#
10
8
# Exposes ports for Swoole HTTP servers.
11
9
#
12
10
# Usage:
13
11
# docker build -t php-swoole-crud-microservice .
14
12
# docker run -p 9501:9501 php-swoole-crud-microservice
15
13
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
18
17
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 ---
22
19
RUN apt-get update && apt-get install -y \
23
- autoconf \
24
- build-essential \
25
20
git \
26
21
libbrotli-dev \
27
22
libc-ares-dev \
@@ -31,104 +26,81 @@ RUN apt-get update && apt-get install -y \
31
26
libsqlite3-dev \
32
27
libssl-dev \
33
28
libzip-dev \
34
- pkg-config \
29
+ supervisor \
35
30
unzip \
36
- zlib1g-dev \
37
31
&& rm -rf /var/lib/apt/lists/*
38
32
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
40
38
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 ---
43
50
ARG 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 \
45
53
&& cd /usr/src/swoole \
46
54
&& 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 \
53
56
&& make -j$(nproc) && make install \
54
57
&& docker-php-ext-enable swoole opcache
55
58
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
78
63
79
64
# --- Composer ---
80
- # Copies Composer binary from official Composer image.
81
65
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
82
66
83
- # ================= Runtime Stage =================
84
- FROM php:8.4-cli
67
+ # --- Set working directory ---
68
+ WORKDIR /app
85
69
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* ./
102
72
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
107
82
108
83
# --- Set working directory ---
109
84
WORKDIR /app
110
85
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
118
91
119
92
# --- PHP configuration ---
120
- # Copies custom PHP configuration including opcache tuning.
121
93
COPY docker/php/php.ini /usr/local/etc/php/conf.d/zz-preload.ini
122
94
COPY docker/php/opcache.ini /usr/local/etc/php/conf.d/opcache.ini
123
95
124
- # --- Copy application source code ---
125
- COPY . .
96
+ # --- Supervisor configuration ---
97
+ COPY docker/supervisord.conf /etc/supervisor/conf.d/swoole.conf
126
98
127
- # --- Ensure logs directory exists ---
128
- RUN mkdir -p /app/logs
99
+ # --- Copy all application source code in a single step ---
100
+ COPY . .
129
101
130
102
# --- Expose Swoole HTTP and custom ports ---
131
103
EXPOSE 9501 9310 9502
132
104
133
105
# --- Default command ---
134
- CMD ["php" , "public/index.php " ]
106
+ CMD ["/usr/bin/supervisord" , "-c" , "/etc/supervisor/conf.d/swoole.conf " ]
0 commit comments