From d98a0e3d4c2ecf6bf326473853b39d3aba46eb0c Mon Sep 17 00:00:00 2001 From: suguo <25950955@qq.com> Date: Fri, 14 Feb 2025 15:50:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E9=80=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 92 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 202271e..1b36f96 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,12 @@ # PHP-FPM镜像 -FROM php:8.2-fpm +FROM webdevops/php:8.2-fpm -# 安装依赖 -RUN apt-get update && apt-get install -y \ +# 使用阿里云镜像源 +RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list \ + && sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list + +# 安装依赖时添加--no-install-recommends减少不必要的包 +RUN apt-get update && apt-get install -y --no-install-recommends \ git \ zip \ unzip \ @@ -10,7 +14,11 @@ RUN apt-get update && apt-get install -y \ libpng-dev \ libonig-dev \ libxml2-dev \ - && docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd sockets zip + # 合并扩展安装命令 + && docker-php-ext-install -j$(nproc) pdo_mysql mbstring exif pcntl bcmath gd sockets zip \ + # 清理缓存减少镜像体积 + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* # 安装Composer COPY --from=composer:latest /usr/bin/composer /usr/bin/composer @@ -30,4 +38,83 @@ FROM nginx:alpine COPY --from=0 /var/www /var/www COPY .docker/nginx.conf /etc/nginx/conf.d/default.conf -EXPOSE 80 \ No newline at end of file +EXPOSE 80 + +# 直接启用所需扩展(该镜像已预装大部分扩展) +RUN docker-php-ext-enable \ + pdo_mysql \ + mbstring \ + exif \ + pcntl \ + bcmath \ + gd \ + sockets \ + zip + +# 使用并行编译(-j参数) +RUN docker-php-ext-install -j$(nproc) pdo_mysql + +# 优先使用pecl安装(部分扩展有预编译包) +RUN pecl install -o -f redis \ + && docker-php-ext-enable redis + +# 禁用Xdebug等开发扩展 +RUN if [ "$APP_ENV" = "production" ]; then \ + pecl uninstall xdebug \ + && rm -rf /tmp/pear; \ + fi + +# 第一阶段:构建依赖 +FROM php:8.2-fpm as builder + +# 使用国内composer镜像 +RUN composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ + +COPY composer.json composer.lock ./ +RUN composer install --no-dev --no-scripts --optimize-autoloader + +# 第二阶段:生产镜像 +FROM php:8.2-fpm + +# 只复制必要文件 +COPY --from=builder /app/vendor /app/vendor +COPY . /app + +# 使用预编译的扩展(需自行构建) +# COPY ./docker/php/extensions/* /usr/local/lib/php/extensions/no-debug-non-zts-20210902/ + +# 使用阿里云镜像源 +FROM php:8.2-fpm + +# 基础系统优化 +RUN set -eux; \ + # 更换镜像源 + sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list; \ + sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list; \ + # 安装必要工具 + apt-get update && apt-get install -y --no-install-recommends \ + git \ + zip \ + unzip \ + libzip-dev \ + libpng-dev \ + libonig-dev \ + libxml2-dev; \ + # 批量安装扩展 + docker-php-ext-configure zip --with-zip; \ + docker-php-ext-install -j$(nproc) \ + pdo_mysql \ + mbstring \ + exif \ + pcntl \ + bcmath \ + gd \ + sockets \ + zip; \ + # 清理缓存 + apt-get clean; \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*; + +# 使用国内composer源 +COPY --from=composer:latest /usr/bin/composer /usr/bin/composer +RUN composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ \ No newline at end of file