-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrankenphp.Dockerfile
More file actions
275 lines (239 loc) · 7.07 KB
/
frankenphp.Dockerfile
File metadata and controls
275 lines (239 loc) · 7.07 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 dunglas/frankenphp:1.12-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
apt-get install \
--yes \
--no-install-recommends \
postgresql-client \
libmemcached11t64 \
ca-certificates \
libyaml-0-2 \
libicu76 \
libzip5 \
gettext \
openssl \
libuv1 \
zlib1g \
unzip \
file \
;
rm -rf /var/lib/apt/lists/*
EOF
FROM upstream AS builder
ARG UV_VERSION="0.3.0"
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 \
git \
;
rm -rf /var/lib/apt/lists/*
# 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
EOF
FROM upstream AS base
ARG user="php"
ARG uid="900"
RUN <<EOF
# 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
# Give write access to /data/caddy and /config/caddy
chown -R "${uid}:${uid}" \
/config/caddy \
/data/caddy
# Create the application folder
mkdir -p /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
# 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:latest /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
EXPOSE 2019/tcp
FROM base AS prod-pre
RUN <<EOF
# 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
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 / /
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"]
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
EXPOSE 2019/tcp