-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrankenphp.Dockerfile
More file actions
306 lines (265 loc) · 8.18 KB
/
frankenphp.Dockerfile
File metadata and controls
306 lines (265 loc) · 8.18 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# syntax=docker/dockerfile:1
ARG PHP_VERSION="8.5"
ARG PIE_VERSION="1.4.5"
ARG FRANKENPHP_VERSION="1.12"
ARG COMPOSER_VERSION="2"
FROM ghcr.io/php/pie:${PIE_VERSION}-bin AS pie
FROM composer:${COMPOSER_VERSION} AS composer-bin
FROM dunglas/frankenphp:${FRANKENPHP_VERSION}-php${PHP_VERSION} AS upstream
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked <<EOF
set -eux
export DEBIAN_FRONTEND=noninteractive
apt-get update
# NOTE: soname-versioned packages (libmemcached11t64, libicu76, libzip5)
# are tied to the Debian release of the upstream image and must be bumped
# when it rebases onto a new Debian version.
apt-get install \
--yes \
--no-install-recommends \
postgresql-client \
libmemcached11t64 \
ca-certificates \
libyaml-0-2 \
libicu76 \
libzip5 \
gettext \
openssl \
libuv1 \
zlib1g \
unzip \
curl \
file \
;
EOF
FROM upstream AS builder
ARG UV_VERSION="0.3.0"
ARG EXCIMER_VERSION="1.2.6"
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=bind,from=pie,source=/pie,target=/usr/bin/pie \
<<EOF
set -eux
# region Install Dependencies
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install \
--yes \
--no-install-recommends \
${PHPIZE_DEPS} \
libmemcached-dev \
libsqlite3-dev \
libyaml-dev \
libicu-dev \
libzip-dev \
zlib1g-dev \
libuv1-dev \
libpq-dev \
libtool \
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-${EXCIMER_VERSION}"
pecl clear-cache || true
docker-php-ext-enable \
excimer \
uv \
;
# endregion
# NOTE: Swoole is intentionally not built for this variant: FrankenPHP is
# the application server here, so the CLI variants are the ones that ship
# Swoole.
EOF
FROM upstream AS base
ARG user="php"
ARG uid="900"
RUN <<EOF
set -eux
# region Add a non-root user to run the application
addgroup \
--gid "${uid}" \
"${user}"
adduser \
--home "/home/${user}" \
--disabled-password \
--disabled-login \
--uid "${uid}" \
--gid ${uid} \
--system \
"${user}"
# endregion
# Add additional capability to bind to port 80 and 443
setcap CAP_NET_BIND_SERVICE=+eip /usr/local/bin/frankenphp
# Create the application folder
mkdir -p /app
# Give write access to /data (Caddy state, Mercure Bolt DB), /config,
# and /app
chown -R "${uid}:${uid}" \
/config \
/data \
/app
EOF
COPY --link --from=builder /usr/local/lib/php/extensions/ /usr/local/lib/php/extensions/
COPY --link --from=builder /usr/local/etc/php/conf.d/ /usr/local/etc/php/conf.d/
# Copy custom PHP settings
COPY --link ./php.ini "${PHP_INI_DIR}/conf.d/99-docker.ini"
COPY --link ./Caddyfile /etc/caddy/Caddyfile
FROM base AS dev
ARG user="php"
ARG uid="900"
ENV COMPOSER_ALLOW_SUPERUSER="1"
ENV PHP_OPCACHE_VALIDATE_TIMESTAMPS="1"
ENV XDG_DATA_HOME="/data"
ENV XDG_CONFIG_HOME="/config"
ENV GODEBUG="cgocheck=0"
# Enables PHPStorm to apply the correct path mapping on Xdebug breakpoints
ENV PHP_IDE_CONFIG="serverName=Docker"
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--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
# libtool is required by PIE to build extensions
# git is required by Composer to install dependencies from VCS sources
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install \
--yes \
--no-install-recommends \
libtool \
git \
;
# 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
rm -rf /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-bin /usr/bin/composer /usr/bin/composer
WORKDIR "/app"
ONBUILD ARG user="php"
ONBUILD ARG uid="900"
USER "${uid}:${uid}"
ENTRYPOINT ["docker-php-entrypoint"]
CMD ["--config", "/etc/caddy/Caddyfile", "--adapter", "caddyfile"]
HEALTHCHECK NONE
EXPOSE 80/tcp
FROM base AS prod-pre
RUN <<EOF
set -eux
# region Remove Build Dependencies
export DEBIAN_FRONTEND=noninteractive
apt-get remove \
--yes \
--purge \
${PHPIZE_DEPS} \
;
apt-get purge \
--option APT::AutoRemove::RecommendsImportant=false \
--auto-remove \
--yes \
;
apt-get autoremove --yes
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
# Activate the production PHP configuration. This must happen in this
# stage rather than in the scratch stage below: scratch stages do not
# inherit environment variables, so PHP_INI_DIR would expand to an empty
# string there, leaving a broken symlink at / and no loaded php.ini.
ln -sf "${PHP_INI_DIR}/php.ini-production" "${PHP_INI_DIR}/php.ini"
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"
ENV XDG_DATA_HOME="/data"
ENV XDG_CONFIG_HOME="/config"
ENV GODEBUG="cgocheck=0"
COPY --link --from=prod-pre / /
WORKDIR "/app"
ONBUILD ARG user="php"
ONBUILD ARG uid="900"
USER "${uid}:${uid}"
ENTRYPOINT ["docker-php-entrypoint"]
CMD ["--config", "/etc/caddy/Caddyfile", "--adapter", "caddyfile"]
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
CMD [ "curl", "-ISsfo", "/dev/null", "http://localhost:2019/metrics" ]
EXPOSE 80/tcp
EXPOSE 443/tcp
EXPOSE 443/udp