-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalpine.Dockerfile
More file actions
275 lines (243 loc) · 6.74 KB
/
alpine.Dockerfile
File metadata and controls
275 lines (243 loc) · 6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# syntax=docker/dockerfile:1
ARG PHP_VERSION="8.4"
ARG PIE_VERSION="1.3.10"
FROM ghcr.io/php/pie:${PIE_VERSION}-bin AS pie
FROM php:${PHP_VERSION}-cli-alpine AS upstream
FROM upstream AS base
ARG UV_VERSION="0.3.0"
ARG user="php"
ARG uid="900"
# Install gnu-libiconv and set LD_PRELOAD env to make iconv work fully on Alpine image.
# see https://github.com/docker-library/php/issues/240#issuecomment-763112749
ENV LD_PRELOAD="/usr/lib/preloadable_libiconv.so"
RUN --mount=type=bind,from=pie,source=/pie,target=/usr/bin/pie \
<<EOF
set -eux
# region Install Dependencies
apk add \
--no-cache \
postgresql-client \
libmemcached-dev \
ca-certificates \
gnu-libiconv \
libzip-dev \
yaml-dev \
zlib-dev \
gettext \
libuv \
unzip \
fcgi \
file \
acl \
;
apk add \
--no-cache \
--virtual .build-deps \
${PHPIZE_DEPS} \
postgresql-dev \
linux-headers \
liburing-dev \
sqlite-dev \
pcre2-dev \
libuv-dev \
curl-dev \
pcre-dev \
icu-dev \
git \
;
# endregion
docker-php-source extract
export num_cpu=$(nproc)
# region Install PIE extensions
pie install -j${num_cpu} phpredis/phpredis \
--enable-redis \
;
pie install -j${num_cpu} apcu/apcu \
--enable-apcu \
;
pie install -j${num_cpu} pecl/yaml
pie install -j${num_cpu} php-memcached/php-memcached \
--enable-memcached-session \
--enable-memcached-json \
;
#pie install -j${num_cpu} csvtoolkit/fastcsv \
# --enable-fastcsv \
#;
# endregion
# region Install built-in extensions
docker-php-ext-configure zip
docker-php-ext-install -j${num_cpu} \
pdo_sqlite \
pdo_pgsql \
sockets \
bcmath \
pcntl \
intl \
zip \
;
# If we're running on PHP 8.4, install the opcache extension (it's bundled in later versions)
if php --version | grep -q "PHP 8\.4"; then
docker-php-ext-install -j${num_cpu} opcache
fi
# endregion
# region Install uv extension
pecl config-set preferred_state beta
pecl install "uv-${UV_VERSION}"
pecl config-set preferred_state stable
# endregion
# region Install PECL extensions
pecl install excimer
pecl clear-cache || true
docker-php-ext-enable \
excimer \
uv \
;
# endregion
# region Install Swoole with extra features
pie install -j${num_cpu} swoole/swoole \
--enable-swoole-sqlite \
--enable-swoole-pgsql \
--enable-uring-socket \
--enable-swoole-curl \
--enable-sockets \
--enable-iouring \
--enable-brotli \
|| (cat /tmp/pie_make_output_* 2>&1; exit 2)
# endregion
docker-php-source delete
# endregion
# region Remove Build Dependencies
runDeps="$( \
scanelf \
--format '%n#p' \
--recursive \
--nobanner \
--needed \
/usr/local/lib/php/extensions \
| tr ',' '\n' \
| sort -u \
| awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
)"
apk add \
--no-cache \
--virtual .phpexts-rundeps \
${runDeps}
apk del .build-deps
rm -rf \
/usr/local/lib/php/test \
/usr/local/bin/phpdbg \
/usr/local/bin/install-php-extensions \
/usr/local/bin/docker-php-source \
/usr/local/bin/docker-php-ext-* \
/usr/local/bin/phpize \
/usr/local/bin/pear* \
/usr/local/bin/pecl \
/usr/local/bin/phpize \
/var/cache/* \
/usr/src/* \
/tmp/* \
;
# endregion
# region Add a non-root user to run the application
addgroup \
-g "${uid}" \
-S \
"${user}"
adduser \
-h "/home/${user}" \
-u "${uid}" \
-G ${user} \
-DS \
"${user}"
# endregion
EOF
# Copy custom PHP settings
COPY --link ./php.ini "${PHP_INI_DIR}/conf.d/99-docker.ini"
FROM base AS dev
ARG user="php"
ARG uid="900"
ENV COMPOSER_ALLOW_SUPERUSER="1"
ENV PHP_OPCACHE_VALIDATE_TIMESTAMPS="1"
# Enables PHPStorm to apply the correct path mapping on Xdebug breakpoints
ENV PHP_IDE_CONFIG="serverName=Docker"
RUN --mount=type=bind,from=pie,source=/pie,target=/usr/bin/pie \
--mount=type=bind,from=upstream,source=/usr/local/bin,target=/usr/local/bin \
<<EOF
set -eux
ln -sf "${PHP_INI_DIR}/php.ini-development" "${PHP_INI_DIR}/php.ini"
# region Install XDebug
apk add \
--no-cache \
--virtual .build-deps \
${PHPIZE_DEPS} \
linux-headers \
;
# TODO: Switch to stable when available
if php --version | grep -q "PHP 8\.5"; then
pie install xdebug/xdebug:@alpha
else
pie install xdebug/xdebug
fi
apk del .build-deps
rm -rf \
/var/cache/* \
/tmp/*
# endregion
# region Configure XDebug
# See https://docs.docker.com/desktop/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host
# See https://github.com/docker/for-linux/issues/264
# The `client_host` below may optionally be replaced with `discover_client_host=yes`
# Add `start_with_request=yes` to start debug session on each request
echo "xdebug.client_host = host.docker.internal" >> "${PHP_INI_DIR}/conf.d/docker-php-ext-xdebug.ini";
echo "xdebug.mode = off" >> "${PHP_INI_DIR}/conf.d/docker-php-ext-xdebug.ini";
# endregion
EOF
COPY --link --from=composer:latest /usr/bin/composer /usr/bin/composer
WORKDIR "/app"
ONBUILD ARG user="php"
ONBUILD ARG uid="900"
USER "${uid}:${uid}"
ENTRYPOINT ["docker-php-entrypoint"]
EXPOSE 9000/tcp
FROM base AS prod-pre
RUN <<EOF
# region Remove Build Dependencies
set -eux
apk del ${PHPIZE_DEPS} *-dev
rm -rf \
/usr/local/bin/phpdbg \
/usr/local/bin/php-cgi \
/usr/local/bin/php-config \
/usr/local/bin/install-php-extensions \
/usr/local/bin/docker-php-source \
/usr/local/bin/docker-php-ext-* \
/usr/local/bin/phpize \
/usr/local/bin/pear* \
/usr/local/bin/phar* \
/usr/local/bin/pecl \
/usr/local/php/man \
/usr/local/etc/pear.conf \
/usr/local/lib/php/PEAR \
/usr/local/lib/php/.registry \
/usr/src/* \
/var/cache/* \
/var/log/* \
/tmp/* \
;
# endregion
EOF
FROM scratch AS prod
ARG user="php"
ARG uid="900"
ENV PHP_OPCACHE_VALIDATE_TIMESTAMPS="0"
ENV PHP_OPCACHE_MAX_ACCELERATED_FILES="10000"
ENV PHP_OPCACHE_MEMORY_CONSUMPTION="192"
ENV PHP_OPCACHE_MAX_WASTED_PERCENTAGE="10"
COPY --link --from=prod-pre / /
RUN ln -sf "${PHP_INI_DIR}/php.ini-production" "${PHP_INI_DIR}/php.ini"
WORKDIR "/app"
ONBUILD ARG user="php"
ONBUILD ARG uid="900"
USER "${uid}:${uid}"
ENTRYPOINT ["docker-php-entrypoint"]
EXPOSE 9000/tcp