-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathphp.Dockerfile
More file actions
440 lines (371 loc) · 13.5 KB
/
php.Dockerfile
File metadata and controls
440 lines (371 loc) · 13.5 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# Using https://github.com/stechstudio/aws-lambda-build
FROM stechstudio/aws-lambda-build as php_builder
ARG installDir="/opt/php"
ARG php
# Use the bash shell, instead of /bin/sh
SHELL ["/bin/bash", "-c"]
# We need a base path for all the sourcecode we will build from.
ENV BUILD_DIR="/tmp/build"
# We need a base path for the builds to install to. This path must
# match the path that php will be unpackaged to in Lambda.
ENV INSTALL_DIR=$installDir
# Apply stack smash protection to functions using local buffers and alloca()
# ## # Enable size optimization (-Os)
# # Enable linker optimization (this sorts the hash buckets to improve cache locality, and is non-default)
# # Adds GNU HASH segments to generated executables (this is used if present, and is much faster than sysv hash; in this configuration, sysv hash is also generated)
# We need some default compiler variables setup
ENV PKG_CONFIG_PATH="${INSTALL_DIR}/lib64/pkgconfig:${INSTALL_DIR}/lib/pkgconfig" \
PKG_CONFIG="/usr/bin/pkg-config" \
PATH="${INSTALL_DIR}/bin:${PATH}"
ENV LD_LIBRARY_PATH="${INSTALL_DIR}/lib64:${INSTALL_DIR}/lib"
# Ensure we have all the directories we require in the container.
RUN mkdir -p ${BUILD_DIR} \
${INSTALL_DIR}/bin \
${INSTALL_DIR}/doc \
${INSTALL_DIR}/etc/php \
${INSTALL_DIR}/include \
${INSTALL_DIR}/lib \
${INSTALL_DIR}/lib64 \
${INSTALL_DIR}/libexec \
${INSTALL_DIR}/sbin \
${INSTALL_DIR}/share
###############################################################################
# ZLIB Build
# https://github.com/madler/zlib/releases
# Needed for:
# - openssl
# - php
# Used By:
# - xml2
ARG zlib
ENV VERSION_ZLIB=${zlib}
ENV ZLIB_BUILD_DIR=${BUILD_DIR}/xml2
RUN set -xe; \
mkdir -p ${ZLIB_BUILD_DIR}; \
# Download and upack the source code
curl -Ls http://zlib.net/zlib-${VERSION_ZLIB}.tar.xz \
| tar xJC ${ZLIB_BUILD_DIR} --strip-components=1
# Move into the unpackaged code directory
WORKDIR ${ZLIB_BUILD_DIR}/
# Configure the build
RUN set -xe; \
make distclean \
&& CFLAGS="" \
CPPFLAGS="-I${INSTALL_DIR}/include -I/usr/include" \
LDFLAGS="-L${INSTALL_DIR}/lib64 -L${INSTALL_DIR}/lib" \
./configure \
--prefix=${INSTALL_DIR} \
--64
RUN set -xe; \
make install \
&& rm ${INSTALL_DIR}/lib/libz.a
###############################################################################
# OPENSSL Build
# https://github.com/openssl/openssl/releases
# Needs:
# - zlib
# Needed by:
# - php
ARG openssl
ENV VERSION_OPENSSL=${openssl}
ENV OPENSSL_BUILD_DIR=${BUILD_DIR}/xml2
ENV CA_BUNDLE_SOURCE="https://curl.haxx.se/ca/cacert.pem"
ENV CA_BUNDLE="${INSTALL_DIR}/ssl/cert.pem"
RUN set -xe; \
mkdir -p ${OPENSSL_BUILD_DIR}; \
# Download and upack the source code
curl -Ls https://github.com/openssl/openssl/archive/OpenSSL_${VERSION_OPENSSL//./_}.tar.gz \
| tar xzC ${OPENSSL_BUILD_DIR} --strip-components=1
# Move into the unpackaged code directory
WORKDIR ${OPENSSL_BUILD_DIR}/
# Configure the build
RUN set -xe; \
CFLAGS="" \
CPPFLAGS="-I${INSTALL_DIR}/include -I/usr/include" \
LDFLAGS="-L${INSTALL_DIR}/lib64 -L${INSTALL_DIR}/lib" \
./config \
--prefix=${INSTALL_DIR} \
--openssldir=${INSTALL_DIR}/ssl \
--release \
no-tests \
shared \
zlib
RUN set -xe; \
make install \
&& curl -k -o ${CA_BUNDLE} ${CA_BUNDLE_SOURCE}
###############################################################################
# LIBSSH2 Build
# https://github.com/libssh2/libssh2/releases/
# Needs:
# - zlib
# - OpenSSL
# Needed by:
# - curl
ARG libssh2
ENV VERSION_LIBSSH2=${libssh2}
ENV LIBSSH2_BUILD_DIR=${BUILD_DIR}/libssh2
RUN set -xe; \
mkdir -p ${LIBSSH2_BUILD_DIR}/bin; \
# Download and upack the source code
curl -Ls https://github.com/libssh2/libssh2/releases/download/libssh2-${VERSION_LIBSSH2}/libssh2-${VERSION_LIBSSH2}.tar.gz \
| tar xzC ${LIBSSH2_BUILD_DIR} --strip-components=1
# Move into the unpackaged code directory
WORKDIR ${LIBSSH2_BUILD_DIR}/bin/
# Configure the build
RUN set -xe; \
CFLAGS="" \
CPPFLAGS="-I${INSTALL_DIR}/include -I/usr/include" \
LDFLAGS="-L${INSTALL_DIR}/lib64 -L${INSTALL_DIR}/lib" \
cmake .. \
-DBUILD_SHARED_LIBS=ON \
-DCRYPTO_BACKEND=OpenSSL \
-DENABLE_ZLIB_COMPRESSION=ON \
-DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} \
-DCMAKE_BUILD_TYPE=RELEASE
RUN set -xe; \
cmake --build . --target install
###############################################################################
# CURL Build
# # https://github.com/curl/curl/releases/
# # Needs:
# # - zlib
# # - OpenSSL
# # - curl
# # Needed by:
# # - php
ARG curl
ENV VERSION_CURL=${curl}
ENV CURL_BUILD_DIR=${BUILD_DIR}/curl
RUN set -xe; \
mkdir -p ${CURL_BUILD_DIR}/bin; \
curl -Ls https://github.com/curl/curl/archive/curl-${VERSION_CURL//./_}.tar.gz \
| tar xzC ${CURL_BUILD_DIR} --strip-components=1
WORKDIR ${CURL_BUILD_DIR}/
RUN set -xe; \
./buildconf \
&& CFLAGS="" \
CPPFLAGS="-I${INSTALL_DIR}/include -I/usr/include" \
LDFLAGS="-L${INSTALL_DIR}/lib64 -L${INSTALL_DIR}/lib" \
./configure \
--prefix=${INSTALL_DIR} \
--with-ca-bundle=${CA_BUNDLE} \
--enable-shared \
--disable-static \
--enable-optimize \
--disable-warnings \
--disable-dependency-tracking \
--with-zlib \
--enable-http \
--enable-ftp \
--enable-file \
--enable-ldap \
--enable-ldaps \
--enable-proxy \
--enable-tftp \
--enable-ipv6 \
--enable-openssl-auto-load-config \
--enable-cookies \
--with-gnu-ld \
--with-ssl \
--with-libssh2
RUN set -xe; \
make install
###############################################################################
# LIBXML2 Build
# https://github.com/GNOME/libxml2/releases
# Uses:
# - zlib
# Needed by:
# - php
ARG libxml2
ENV VERSION_XML2=${libxml2}
ENV XML2_BUILD_DIR=${BUILD_DIR}/xml2
RUN set -xe; \
mkdir -p ${XML2_BUILD_DIR}; \
# Download and upack the source code
curl -Ls http://xmlsoft.org/sources/libxml2-${VERSION_XML2}.tar.gz \
| tar xzC ${XML2_BUILD_DIR} --strip-components=1
# Move into the unpackaged code directory
WORKDIR ${XML2_BUILD_DIR}/
# Configure the build
RUN set -xe; \
CFLAGS="" \
CPPFLAGS="-I${INSTALL_DIR}/include -I/usr/include" \
LDFLAGS="-L${INSTALL_DIR}/lib64 -L${INSTALL_DIR}/lib" \
./configure \
--prefix=${INSTALL_DIR} \
--with-sysroot=${INSTALL_DIR} \
--enable-shared \
--disable-static \
--with-html \
--with-history \
--enable-ipv6=no \
--with-icu \
--with-zlib=${INSTALL_DIR} \
--without-python
RUN set -xe; \
make install \
&& cp xml2-config ${INSTALL_DIR}/bin/xml2-config
###############################################################################
# LIBSODIUM Build
# https://github.com/jedisct1/libsodium/releases
# Uses:
#
# Needed by:
# - php
ARG libsodium
ENV VERSION_LIBSODIUM=${libsodium}
ENV LIBSODIUM_BUILD_DIR=${BUILD_DIR}/libsodium
RUN set -xe; \
mkdir -p ${LIBSODIUM_BUILD_DIR}; \
# Download and upack the source code
curl -Ls https://github.com/jedisct1/libsodium/archive/${VERSION_LIBSODIUM}.tar.gz \
| tar xzC ${LIBSODIUM_BUILD_DIR} --strip-components=1
# Move into the unpackaged code directory
WORKDIR ${LIBSODIUM_BUILD_DIR}/
# Configure the build
RUN set -xe; \
CFLAGS="" \
CPPFLAGS="-I${INSTALL_DIR}/include -I/usr/include" \
LDFLAGS="-L${INSTALL_DIR}/lib64 -L${INSTALL_DIR}/lib" \
./autogen.sh \
&& ./configure --prefix=${INSTALL_DIR}
RUN set -xe; \
make install
###############################################################################
# Postgres Build
# https://github.com/postgres/postgres/releases/
# Needs:
# - OpenSSL
# Needed by:
# - php
ARG postgres
ENV VERSION_POSTGRES=${postgres}
ENV POSTGRES_BUILD_DIR=${BUILD_DIR}/postgres
RUN set -xe; \
mkdir -p ${POSTGRES_BUILD_DIR}/bin; \
curl -Ls https://github.com/postgres/postgres/archive/REL${VERSION_POSTGRES//./_}.tar.gz \
| tar xzC ${POSTGRES_BUILD_DIR} --strip-components=1
WORKDIR ${POSTGRES_BUILD_DIR}/
RUN set -xe; \
CFLAGS="" \
CPPFLAGS="-I${INSTALL_DIR}/include -I/usr/include" \
LDFLAGS="-L${INSTALL_DIR}/lib64 -L${INSTALL_DIR}/lib" \
./configure --prefix=${INSTALL_DIR} --with-openssl --without-readline
RUN set -xe; cd ${POSTGRES_BUILD_DIR}/src/interfaces/libpq && make && make install
RUN set -xe; cd ${POSTGRES_BUILD_DIR}/src/bin/pg_config && make && make install
RUN set -xe; cd ${POSTGRES_BUILD_DIR}/src/backend && make generated-headers
RUN set -xe; cd ${POSTGRES_BUILD_DIR}/src/include && make install
###############################################################################
# PHP Build
# https://github.com/php/php-src/releases
# Needs:
# - zlib
# - libxml2
# - openssl
# - readline
# - sodium
# Setup Build Variables
ENV VERSION_PHP=${php}
ENV PHP_BUILD_DIR=${BUILD_DIR}/php
RUN set -xe; \
mkdir -p ${PHP_BUILD_DIR}; \
# Download and upack the source code
curl -Ls https://github.com/php/php-src/archive/php-${VERSION_PHP}.tar.gz \
| tar xzC ${PHP_BUILD_DIR} --strip-components=1
# Move into the unpackaged code directory
WORKDIR ${PHP_BUILD_DIR}/
# Install some dev files for using old libraries already on the system
# readline-devel : needed for the --with-libedit flag
# gettext-devel : needed for the --with-gettext flag
# libicu-devel : needed for
RUN LD_LIBRARY_PATH= yum install -y readline-devel gettext-devel libicu-devel
# Configure the build
# -fstack-protector-strong : Be paranoid about stack overflows
# -fpic : Make PHP's main executable position-independent (improves ASLR security mechanism, and has no performance impact on x86_64)
# -fpie : Support Address Space Layout Randomization (see -fpic)
# -0s : Optimize for smallest binaries possible.
# -I : Add the path to the list of directories to be searched for header files during preprocessing.
# --enable-option-checking=fatal: make sure invalid --configure-flags are fatal errors instead of just warnings
# --enable-ftp: because ftp_ssl_connect() needs ftp to be compiled statically (see https://github.com/docker-library/php/issues/236)
# --enable-mbstring: because otherwise there's no way to get pecl to use it properly (see https://github.com/docker-library/php/issues/195)
# --enable-maintainer-zts: build PHP as ZTS (Zend Thread Safe) to be able to use pthreads
# --with-zlib and --with-zlib-dir: See https://stackoverflow.com/a/42978649/245552
#
RUN set -xe \
&& ./buildconf --force \
&& CFLAGS="-fstack-protector-strong -fpic -fpie -Os -I${INSTALL_DIR}/include -I/usr/include -ffunction-sections -fdata-sections" \
CPPFLAGS="-fstack-protector-strong -fpic -fpie -Os -I${INSTALL_DIR}/include -I/usr/include -ffunction-sections -fdata-sections" \
LDFLAGS="-L${INSTALL_DIR}/lib64 -L${INSTALL_DIR}/lib -Wl,-O1 -Wl,--strip-all -Wl,--hash-style=both -pie" \
./configure \
--build=x86_64-pc-linux-gnu \
--prefix=${INSTALL_DIR} \
--enable-option-checking=fatal \
--enable-maintainer-zts \
--with-config-file-path=${INSTALL_DIR}/etc/php \
--with-config-file-scan-dir=${INSTALL_DIR}/etc/php/config.d:/var/task/php/config.d \
--enable-fpm \
--enable-cgi \
--enable-cli \
--disable-phpdbg \
--disable-phpdbg-webhelper \
--with-sodium \
--with-readline \
--with-openssl \
--with-zlib=${INSTALL_DIR} \
--with-zlib-dir=${INSTALL_DIR} \
--with-curl \
--enable-exif \
--enable-ftp \
--with-gettext \
--enable-mbstring \
--with-pdo-mysql=shared,mysqlnd \
--enable-pcntl \
--with-pdo-pgsql=shared,${INSTALL_DIR} \
--enable-intl=shared
RUN set -xe \
&& make -j $(nproc) \
&& make install \
&& { find =${INSTALL_DIR}/bin =${INSTALL_DIR}/sbin -type f -perm +0111 -exec strip --strip-all '{}' + || true; } \
&& make clean \
&& cp php.ini-production ${INSTALL_DIR}/etc/php/php.ini
RUN pecl install mongodb
RUN pecl install redis
RUN pecl install APCu
RUN set -xe; \
curl -Ls https://elasticache-downloads.s3.amazonaws.com/ClusterClient/PHP-7.0/latest-64bit \
| tar xzC $(php-config --extension-dir) --strip-components=1
ENV VERSION_PTHREADS=3.2.0
ENV PTHREADS_BUILD_DIR=${BUILD_DIR}/pthreads
RUN set -xe; \
mkdir -p ${PTHREADS_BUILD_DIR}/bin; \
curl -Ls https://github.com/krakjoe/pthreads/archive/v${VERSION_PTHREADS}.tar.gz \
| tar xzC ${PTHREADS_BUILD_DIR} --strip-components=1
WORKDIR ${PTHREADS_BUILD_DIR}/
RUN set -xe; \
phpize \
&& ./configure \
&& make \
&& make install
# Strip all the unneeded symbols from shared libraries to reduce size.
RUN find ${INSTALL_DIR} -type f -name "*.so*" -o -name "*.a" -exec strip --strip-unneeded {} \;
RUN find ${INSTALL_DIR} -type f -executable -exec sh -c "file -i '{}' | grep -q 'x-executable; charset=binary'" \; -print|xargs strip --strip-all
# Symlink all our binaries into /opt/bin so that Lambda sees them in the path.
RUN mkdir -p /opt/bin \
&& ln -s ${INSTALL_DIR}/bin/* /opt/bin \
&& ln -s ${INSTALL_DIR}/sbin/* /opt/bin
# Now we get rid of everything that is unnecessary. All the build tools, source code, and anything else
# that might have created intermediate layers for docker. Back to base AmazonLinux we started with.
FROM amazonlinux:2017.03
ARG installDir
ARG php
ENV INSTALL_DIR=${installDir} \
VERSION_PHP=${php} \
PATH=/opt/bin:${PATH} \
LD_LIBRARY_PATH=${installDir}/lib64:${installDir}/lib
RUN mkdir -p /opt
WORKDIR /opt
# Copy everything we built above into the same dir on the base AmazonLinux container.
COPY --from=php_builder /opt /opt
# We will need this later for packaging
RUN LD_LIBRARY_PATH= yum -y install zip