Skip to content

Commit c3f2c25

Browse files
committed
Separate build deps from persistent deps
- From WordPress Docker image: https://github.com/docker-library/wordpress/blob/master/latest/php8.0/fpm/Dockerfile - Makes the image slightly smaller.
1 parent 1975e38 commit c3f2c25

File tree

1 file changed

+56
-27
lines changed

1 file changed

+56
-27
lines changed

Dockerfile

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FROM php:8.0.30-fpm-bullseye
22

33
LABEL maintainer="Evermade"
44

5-
# install the PHP extensions we need (https://make.wordpress.org/hosting/handbook/server-environment/#php-extensions)
5+
# Install the PHP extensions we need (https://make.wordpress.org/hosting/handbook/server-environment/#php-extensions)
66
RUN set -ex; \
77
\
88
# Nginx apt dependencies
@@ -20,22 +20,8 @@ RUN set -ex; \
2020
# Upgrade apt packages
2121
apt-get upgrade -y; \
2222
\
23-
# Install new apt packages
23+
# Install persistent apt packages
2424
apt-get install -y --no-install-recommends \
25-
\
26-
# PHP GD extension dependencies
27-
libfreetype6-dev \
28-
libjpeg-dev \
29-
libpng-dev \
30-
libwebp-dev \
31-
libzip-dev \
32-
\
33-
# PHP Intl extension dependencies
34-
libicu-dev \
35-
\
36-
# PHP ImageMagick extension dependencies
37-
libmagickwand-dev \
38-
\
3925
# PDF preview rendering for WordPress
4026
ghostscript \
4127
\
@@ -65,6 +51,24 @@ RUN set -ex; \
6551
python3-certbot-nginx \
6652
; \
6753
\
54+
savedAptMark="$(apt-mark showmanual)"; \
55+
\
56+
# Install build dependencies to compile PHP extensions
57+
apt-get install -y --no-install-recommends \
58+
# gd
59+
libfreetype6-dev \
60+
libjpeg-dev \
61+
libpng-dev \
62+
libwebp-dev \
63+
libzip-dev \
64+
\
65+
# intl
66+
libicu-dev \
67+
\
68+
# imagick
69+
libmagickwand-dev \
70+
; \
71+
\
6872
# Configure PHP GD extension
6973
docker-php-ext-configure gd \
7074
--with-freetype \
@@ -82,21 +86,46 @@ RUN set -ex; \
8286
opcache \
8387
zip \
8488
; \
89+
pecl install \
90+
imagick-3.7.0 \
91+
igbinary \
92+
redis \
93+
; \
94+
docker-php-ext-enable \
95+
imagick \
96+
igbinary \
97+
redis \
98+
; \
99+
rm -rf /tmp/pear; \
100+
\
101+
# Some misbehaving extensions end up outputting to stdout 🙈 (https://github.com/docker-library/wordpress/issues/669#issuecomment-993945967)
102+
out="$(php -r 'exit(0);')"; \
103+
[ -z "$out" ]; \
104+
err="$(php -r 'exit(0);' 3>&1 1>&2 2>&3)"; \
105+
[ -z "$err" ]; \
106+
\
107+
extDir="$(php -r 'echo ini_get("extension_dir");')"; \
108+
[ -d "$extDir" ]; \
85109
\
86-
# Install PHP ImageMagick extension
87-
pecl install imagick-3.7.0; \
88-
docker-php-ext-enable imagick; \
110+
# Reset apt-mark's "manual" list so that "purge --auto-remove" will remove all build dependencies
111+
apt-mark auto '.*' > /dev/null; \
112+
apt-mark manual $savedAptMark; \
113+
ldd "$extDir"/*.so \
114+
| awk '/=>/ { so = $(NF-1); if (index(so, "/usr/local/") == 1) { next }; gsub("^/(usr/)?", "", so); print so }' \
115+
| sort -u \
116+
| xargs -r dpkg-query --search \
117+
| cut -d: -f1 \
118+
| sort -u \
119+
| xargs -rt apt-mark manual; \
89120
\
90-
# Install PHP Igbinary extension
91-
pecl install igbinary; \
92-
docker-php-ext-enable igbinary; \
121+
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
122+
rm -rf /var/lib/apt/lists/*; \
93123
\
94-
# Install PHP Redis extension
95-
pecl install redis; \
96-
docker-php-ext-enable redis; \
124+
! { ldd "$extDir"/*.so | grep 'not found'; }; \
97125
\
98-
# Cleanup
99-
rm -rf /tmp/pear /var/lib/apt/lists/*; \
126+
# Check for output like "PHP Warning: PHP Startup: Unable to load dynamic library 'foo' (tried: ...)
127+
err="$(php --version 3>&1 1>&2 2>&3)"; \
128+
[ -z "$err" ]; \
100129
\
101130
# Install WP-CLI
102131
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar; \

0 commit comments

Comments
 (0)