build: switch to Alpine base image and simplify Dockerfile

- Use php:8.4-fpm-alpine for smaller image size
- Replace apt-get with apk package manager
- Use docker-php-ext-install instead of external extension installer
- Remove unnecessary comments and clean up cache appropriately
This commit is contained in:
Jp
2026-02-26 13:17:33 +08:00
parent 4ade74421c
commit 77ed347e36

View File

@@ -1,31 +1,27 @@
FROM php:8.4-fpm FROM php:8.4-fpm-alpine
# Set working directory early to avoid conflicts with host files
WORKDIR /var/www WORKDIR /var/www
# Install system dependencies # Install system dependencies
RUN apt-get update && apt-get install -y \ RUN apk add --no-cache \
git \ git \
curl \ curl \
libpng-dev \ libpng-dev \
libonig-dev \ oniguruma-dev \
libxml2-dev \ libxml2-dev \
zip \ zip \
unzip \ unzip \
libzip-dev \ libzip-dev \
libicu-dev icu-dev
# Install PHP extensions installer # Install PHP extensions
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/ RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip intl
RUN chmod +x /usr/local/bin/install-php-extensions && \
install-php-extensions pdo_mysql mbstring exif pcntl bcmath gd zip intl
# Clear cache # Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/* RUN rm -rf /var/cache/apk/*
# Get latest Composer # Get latest Composer
COPY --from=docker.io/library/composer:latest /usr/bin/composer /usr/bin/composer COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Copy application files and fix permissions # Copy application files and fix permissions
COPY . /var/www COPY . /var/www
@@ -34,10 +30,8 @@ RUN chown -R www-data:www-data /var/www && \
find /var/www -type f -exec chmod 644 {} + && \ find /var/www -type f -exec chmod 644 {} + && \
rm -rf /var/www/.circleci /var/www/.github rm -rf /var/www/.circleci /var/www/.github
# Change current user to www
USER www-data USER www-data
# Expose port 8000
EXPOSE 8000 EXPOSE 8000
CMD ["php", "artisan", "serve", "--host=0.0.0.0", "--port=8000"] CMD ["php", "artisan", "serve", "--host=0.0.0.0", "--port=8000"]