Skip to content

Commit aa6bf3c

Browse files
committed
Optimized images
Signed-off-by: Moritz Friedrich <moritz@matchory.com>
1 parent ed8f98c commit aa6bf3c

File tree

4 files changed

+474
-406
lines changed

4 files changed

+474
-406
lines changed

Dockerfile

Lines changed: 110 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,61 @@
11
# syntax=docker/dockerfile:1.13
2-
FROM php:8.4-cli AS base
3-
ARG user="5000"
4-
ARG uid="5000"
5-
2+
FROM php:8.4-cli AS upstream
3+
FROM upstream AS base
64
ARG APCU_VERSION="5.1.24"
75
ARG REDIS_VERSION="6.1.0"
86
ARG OPENSWOOLE_VERSION="25.2.0"
7+
ARG user="php"
8+
ARG uid="5000"
99

10-
# Persistent/Runtime dependencies
11-
RUN <<EOF
12-
set -eux
13-
apt-get update
14-
apt-get install --yes --no-install-recommends \
15-
postgresql-client \
16-
libstdc++6 \
17-
colordiff \
18-
gettext \
19-
file \
20-
acl \
21-
;
22-
apt-get purge --yes --auto-remove
23-
rm -rf /var/lib/apt/lists/*
24-
EOF
25-
26-
# Create the application user
2710
RUN <<EOF
2811
set -eux
2912

30-
# Add a non-root user to run the application
31-
adduser -D -G www-data -u "${uid}" -h "/home/${user}" "${user}"
32-
33-
# region Install Build Dependencies
34-
apt-get update
35-
apt-get install -y --no-install-recommends \
36-
${PHPIZE_DEPS} \
37-
linux-headers-generic \
13+
# region Install Dependencies
14+
export RUNTIME_DEPENDENCIES="\
15+
postgresql-client \
3816
libmemcached-dev \
39-
postgresql-dev \
40-
oniguruma-dev \
41-
gnu-libiconv \
42-
openssl-dev \
17+
ca-certificates \
18+
libyaml-dev \
4319
libzip-dev \
44-
c-ares-dev \
45-
pcre2-dev \
46-
libstdc++ \
47-
pcre-dev \
48-
yaml-dev \
49-
curl-dev \
50-
zlib-dev \
51-
icu-dev \
52-
fcgi \
53-
; \
20+
zlib1g-dev \
21+
gettext \
22+
openssl \
23+
file \
24+
"
25+
export BUILD_DEPENDENCIES="\
26+
${PHPIZE_DEPS} \
27+
linux-headers-generic \
28+
libcurl4-openssl-dev \
29+
libpcre3-dev \
30+
libonig-dev \
31+
libssl-dev \
32+
libicu-dev \
33+
libpq-dev \
34+
"
35+
36+
apt-get update
37+
apt-get install \
38+
--yes \
39+
--no-install-recommends \
40+
${BUILD_DEPENDENCIES} \
41+
${RUNTIME_DEPENDENCIES}
5442
# endregion
5543

56-
# region Install redis
57-
curl -L -o /tmp/redis.tar.gz "https://github.com/phpredis/phpredis/archive/${REDIS_VERSION}.tar.gz"
44+
# region Install redis extension
45+
curl \
46+
--fail \
47+
--silent \
48+
--location \
49+
--output /tmp/redis.tar.gz \
50+
"https://github.com/phpredis/phpredis/archive/${REDIS_VERSION}.tar.gz"
5851
tar xfz /tmp/redis.tar.gz
59-
rm -r /tmp/redis.tar.gz
52+
rm -rf /tmp/redis.tar.gz
6053
mkdir -p /usr/src/php/ext
6154
mv phpredis-* /usr/src/php/ext/redis
6255
# endregion
6356

6457
# region Install Extensions
58+
docker-php-source extract
6559
docker-php-ext-configure zip
6660
docker-php-ext-install -j$(nproc) \
6761
pdo_pgsql \
@@ -71,133 +65,130 @@ RUN <<EOF
7165
bcmath \
7266
pcntl \
7367
redis \
68+
iconv \
7469
intl \
7570
zip \
7671
;
7772
# endregion
7873

79-
# region Install OpenSwoole
80-
docker-php-source extract
81-
mkdir --parents /usr/src/php/ext/openswoole
82-
curl \
83-
--silent \
84-
--fail \
85-
--location \
86-
--output openswoole.tar.gz \
87-
"https://github.com/openswoole/swoole-src/archive/refs/tags/v${OPENSWOOLE_VERSION}.tar.gz"
88-
tar xfz openswoole.tar.gz --strip-components=1 -C /usr/src/php/ext/openswoole
89-
docker-php-ext-configure openswoole \
90-
--enable-hook-curl \
91-
--enable-openssl \
92-
--enable-sockets \
93-
--enable-mysqlnd \
94-
--with-postgres \
95-
--enable-http2 \
96-
--enable-cares \
97-
;
98-
docker-php-ext-install -j$(nproc) --ini-name zz-openswoole.ini openswoole
99-
rm -f openswoole.tar.gz
100-
docker-php-source delete
101-
# endregion
102-
10374
# region Install PECL Extensions
104-
pecl install \
105-
memcached \
106-
excimer \
107-
"apcu-${APCU_VERSION}" \
108-
yaml \
109-
;
75+
pecl install memcached
76+
pecl install excimer
77+
pecl install "apcu-${APCU_VERSION}"
78+
pecl install yaml
79+
pecl install "openswoole-${OPENSWOOLE_VERSION}"
11080
pecl clear-cache || true
11181
docker-php-ext-enable \
82+
openswoole \
83+
memcached \
11284
opcache \
11385
excimer \
11486
apcu \
11587
yaml \
11688
;
89+
docker-php-source delete
11790
# endregion
11891

11992
# region Remove Build Dependencies
120-
# Reset apt-mark's "manual" list so that "purge --auto-remove" will remove all build dependencies
121-
apt-mark auto '.*' > /dev/null
122-
[ -z "$savedAptMark" ] || apt-mark manual $savedAptMark
123-
124-
# Find and mark runtime dependencies for installed extensions
125-
find /usr/local -type f -executable -exec ldd '{}' ';' \
126-
| awk '/=>/ { so = $(NF-1); if (index(so, "/usr/local/") == 1) { next }; gsub("^/(usr/)?", "", so); printf "*%s\n", so }' \
127-
| sort -u \
128-
| xargs -r dpkg-query --search \
129-
| cut -d: -f1 \
130-
| sort -u \
131-
| xargs -r apt-mark manual
132-
133-
# Remove build dependencies and clean up
134-
apt-get purge --yes --auto-remove -o APT::AutoRemove::RecommendsImportant=false
135-
rm -rf /var/lib/apt/lists/*
93+
apt-get purge \
94+
--option APT::AutoRemove::RecommendsImportant=false \
95+
--auto-remove \
96+
--yes \
97+
${BUILD_DEPENDENCIES} \
98+
;
99+
rm -rf \
100+
/usr/local/lib/php/test \
101+
/usr/local/bin/phpdbg \
102+
/usr/local/bin/docker-php-ext-* \
103+
/usr/local/bin/docker-php-source \
104+
/usr/local/bin/install-php-extensions \
105+
/usr/local/bin/pear* \
106+
/usr/local/bin/pecl \
107+
/usr/local/bin/phpize \
108+
/var/lib/apt/lists/* \
109+
/var/cache/* \
110+
/usr/src/* \
111+
/tmp/*
136112
# endregion
113+
114+
# Add a non-root user to run the application
115+
addgroup \
116+
--gid "${uid}" \
117+
"${user}"
118+
adduser \
119+
--home "/home/${user}" \
120+
--disabled-password \
121+
--disabled-login \
122+
--uid "${uid}" \
123+
--gid ${uid} \
124+
--system \
125+
"${user}"
137126
EOF
138127

139128
# Copy custom PHP settings
140129
COPY --link php.ini "${PHP_INI_DIR}/conf.d/99-docker.ini"
141130

142131
ENTRYPOINT ["docker-php-entrypoint"]
132+
143133
VOLUME /var/run/php
134+
VOLUME /app
144135
EXPOSE 9000
145136

146137
FROM base AS dev
147138
ENV COMPOSER_ALLOW_SUPERUSER="1"
139+
ENV PHP_OPCACHE_VALIDATE_TIMESTAMPS="1"
148140

149141
# Enables PHPStorm to apply the correct path mapping on Xdebug breakpoints
150142
ENV PHP_IDE_CONFIG="serverName=Docker"
151143

152-
RUN <<EOF
144+
RUN --mount=type=bind,from=upstream,source=/usr/local/bin,target=/usr/local/bin <<EOF
153145
set -eux
154146
ln -sf "${PHP_INI_DIR}/php.ini-development" "${PHP_INI_DIR}/php.ini"
155147

156-
# region Install Build Dependencies
157-
savedAptMark="$(apt-mark showmanual)"
158-
apt-get update
159-
apt-get install --yes --no-install-recommends \
160-
${PHPIZE_DEPS} \
161-
linux-headers-generic \
162-
;
163-
# endregion
164-
165148
# region Install XDebug
149+
apt-get update
150+
apt-get install \
151+
--yes \
152+
--no-install-recommends \
153+
${PHPIZE_DEPS}
166154
pecl install xdebug
167155
docker-php-ext-enable xdebug
168-
# endregion
169-
170-
# region Remove Build Dependencies
171-
apt-mark auto '.*' > /dev/null
172-
[ -z "$savedAptMark" ] || apt-mark manual $savedAptMark
173-
find /usr/local -type f -executable -exec ldd '{}' ';' \
174-
| awk '/=>/ { so = $(NF-1); if (index(so, "/usr/local/") == 1) { next }; gsub("^/(usr/)?", "", so); printf "*%s\n", so }' \
175-
| sort -u \
176-
| xargs -r dpkg-query --search \
177-
| cut -d: -f1 \
178-
| sort -u \
179-
| xargs -r apt-mark manual
180-
apt-get purge --yes --auto-remove -o APT::AutoRemove::RecommendsImportant=false
181-
rm -rf /var/lib/apt/lists/*
156+
apt-get purge \
157+
--yes \
158+
--auto-remove \
159+
${PHPIZE_DEPS}
160+
rm -rf \
161+
/var/lib/apt/lists/* \
162+
/var/cache/* \
163+
/tmp/*
182164
# endregion
183165

184166
# region Configure XDebug
185167
# See https://docs.docker.com/desktop/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host
186168
# See https://github.com/docker/for-linux/issues/264
187169
# The `client_host` below may optionally be replaced with `discover_client_host=yes`
188170
# Add `start_with_request=yes` to start debug session on each request
189-
echo "xdebug.client_host = host.docker.internal" >> "${PHP_INI_DIR}/conf.d/docker-php-ext-xdebug.ini";
190-
echo "xdebug.mode = off" >> "${PHP_INI_DIR}/conf.d/docker-php-ext-xdebug.ini";
171+
echo "xdebug.client_host = host.docker.internal" >> "${PHP_INI_DIR}/conf.d/xdebug.ini";
172+
echo "xdebug.mode = off" >> "${PHP_INI_DIR}/conf.d/xdebug.ini";
191173
# endregion
192174
EOF
193175

194176
COPY --link --from=composer:latest /usr/bin/composer /usr/bin/composer
195177

178+
ONBUILD ARG user="php"
179+
ONBUILD ARG uid="5000"
180+
ONBUILD USER "${uid}:${uid}"
181+
ONBUILD WORKDIR "/app"
182+
196183
FROM base AS prod
197-
ENV PHP_OPCACHE_ENABLE="1"
198184
ENV PHP_OPCACHE_VALIDATE_TIMESTAMPS="0"
199185
ENV PHP_OPCACHE_MAX_ACCELERATED_FILES="10000"
200186
ENV PHP_OPCACHE_MEMORY_CONSUMPTION="192"
201187
ENV PHP_OPCACHE_MAX_WASTED_PERCENTAGE="10"
202188

203189
RUN ln -sf "${PHP_INI_DIR}/php.ini-production" "${PHP_INI_DIR}/php.ini"
190+
191+
ONBUILD ARG user="php"
192+
ONBUILD ARG uid="5000"
193+
ONBUILD USER "${uid}:${uid}"
194+
ONBUILD WORKDIR "/app"

0 commit comments

Comments
 (0)