Skip to content

Commit 80aeff2

Browse files
Tinywanclaude
andcommitted
优化镜像大小从 858MB 减少到 332MB
- 使用虚拟包 .build-deps 管理编译依赖 - 移除运行时不需要的包(bash、git、nano、openssh 等) - 扩展编译后添加清理(swoole、xhprof、event 等) - 合并 RUN 命令减少层数 - 更彻底的缓存清理(/var/cache/apk、/tmp、.pearrc 等) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 84831bc commit 80aeff2

2 files changed

Lines changed: 26 additions & 37 deletions

File tree

Dockerfile

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,44 @@
1-
# FROM php:7.4.33-cli-alpine
2-
# FROM php:8.1.28-cli-alpine
3-
#FROM php:8.2.18-cli-alpine
41
FROM php:8.4.16-cli-alpine
52

63
LABEL Maintainer="ShaoBo Wan (Tinywan) <756684177@qq.com>" \
74
Description="Webman Lightweight container with PHP 8.4.16 based on Alpine Linux."
85

9-
# Container package : mirrors.163.com、mirrors.aliyun.com、mirrors.ustc.edu.cn
6+
# Use Alibaba Cloud mirror for faster downloads
107
RUN sed -i "s/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g" /etc/apk/repositories
11-
# RUN sed -i "s/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g" /etc/apk/repositories
128

13-
RUN cat /etc/issue
14-
15-
# [php74] Add basics first
16-
# RUN apk update && apk add bash curl ca-certificates openssl openssh git libxml2-dev tzdata icu-dev openntpd libedit-dev libzip-dev libjpeg-turbo-dev libpng-dev freetype-dev autoconf dpkg-dev dpkg file g++ gcc libc-dev make pkgconf re2c pcre-dev libffi-dev libressl-dev libevent-dev zlib-dev libtool automake supervisor
17-
18-
# [php8] Add basics first
19-
RUN apk update && apk add bash curl ca-certificates openssl openssh git nano libxml2-dev tzdata icu-dev openntpd libedit-dev libzip-dev libjpeg-turbo-dev libpng-dev freetype-dev autoconf file g++ gcc libc-dev make pkgconf re2c pcre-dev openssl-dev libffi-dev libevent-dev zlib-dev libtool automake supervisor
9+
# Install runtime dependencies only (build deps will be installed and removed later)
10+
RUN apk add --no-cache curl ca-certificates tzdata supervisor
2011

2112
COPY ./extension /tmp/extension
2213
WORKDIR /tmp/extension
23-
RUN sed -i 's/\r$//' install.sh \
24-
&& chmod +x install.sh \
25-
&& sh install.sh \
26-
&& rm -rf /tmp/extension
2714

28-
RUN php -m
15+
# Install build dependencies, compile extensions, then cleanup everything in one layer
16+
RUN apk add --no-cache --virtual .build-deps \
17+
libxml2-dev libzip-dev libjpeg-turbo-dev libpng-dev freetype-dev \
18+
libevent-dev openssl-dev libffi-dev icu-dev bzip2-dev postgresql-dev \
19+
autoconf g++ gcc make libc-dev pkgconf re2c libtool automake \
20+
&& sed -i 's/\r$//' install.sh \
21+
&& sh install.sh \
22+
&& rm -rf /tmp/extension \
23+
&& apk del .build-deps \
24+
&& rm -rf /var/cache/apk/* /tmp/* /root/.pearrc /usr/local/include/php
2925

30-
# Add Composer
31-
RUN curl -sS https://getcomposer.org/installer | php && mv composer.phar /usr/local/bin/composer
26+
# Add Composer (with cache cleanup)
27+
RUN curl -sS https://getcomposer.org/installer | php \
28+
&& mv composer.phar /usr/local/bin/composer \
29+
&& rm -rf /root/.composer/cache
3230

3331
# Configure PHP
3432
COPY config/php.ini /usr/local/etc/php/conf.d/zzz_custom.ini
3533

3634
# Configure supervisord
3735
COPY config/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
3836

39-
# Make sure files/folders needed by the processes are accessable when they run under the nobody user
40-
# RUN chown -R nobody.nobody /run
41-
4237
# Setup document root
4338
RUN mkdir -p /app
4439

45-
# Make the document root a volume
4640
VOLUME /app
47-
48-
#echo " > /usr/local/etc/php/conf.d/phalcon.ini
49-
# Switch to use a non-root user from here on
50-
USER root
51-
52-
# Add application
5341
WORKDIR /app
54-
55-
# Expose the port nginx is reachable on
5642
EXPOSE 8787
5743

58-
# Let supervisord start nginx & php
5944
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

extension/install.sh

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ installExtensionFromTgz() {
5252
(cd ${extensionName} && phpize && ./configure && make ${MC} && make install)
5353

5454
docker-php-ext-enable ${extensionName} $2
55+
rm -rf ${extensionName}
5556
}
5657

5758
if [[ -z "${EXTENSIONS##*,pdo_mysql,*}" ]]; then
@@ -550,11 +551,13 @@ if [[ -z "${EXTENSIONS##*,swoole,*}" ]]; then
550551
tar -xf swoole-6.1.6.tgz -C swoole --strip-components=1
551552
(cd swoole && phpize && ./configure --enable-sockets --enable-openssl && make ${MC} && make install)
552553
docker-php-ext-enable swoole
554+
rm -rf swoole
553555
else
554556
mkdir swoole
555557
tar -xf swoole-6.1.6.tgz -C swoole --strip-components=1
556558
(cd swoole && phpize && ./configure --enable-sockets --enable-openssl && make ${MC} && make install)
557559
docker-php-ext-enable swoole
560+
rm -rf swoole
558561
fi
559562
fi
560563

@@ -580,7 +583,8 @@ if [[ -z "${EXTENSIONS##*,xhprof,*}" ]]; then
580583
mkdir xhprof &&
581584
tar -xf xhprof-2.2.0.tgz -C xhprof --strip-components=1 &&
582585
(cd xhprof/extension/ && phpize && ./configure && make ${MC} && make install) &&
583-
docker-php-ext-enable xhprof
586+
docker-php-ext-enable xhprof &&
587+
rm -rf xhprof
584588
else
585589
echo "---------- PHP Version>= 7.0----------"
586590
fi
@@ -676,7 +680,7 @@ if [[ -z "${EXTENSIONS##*,grpc,*}" ]]; then
676680
fi
677681
fi
678682

679-
if [ "${PHP_EXTENSIONS}" != "" ]; then
680-
apk del .build-deps &&
681-
docker-php-source delete
682-
fi
683+
# Cleanup build artifacts and caches
684+
echo "---------- Cleanup ----------"
685+
rm -rf /tmp/* /var/cache/apk/* /root/.pearrc /usr/local/include/php
686+
docker-php-source delete

0 commit comments

Comments
 (0)