Updated Dockerfile (#65)

Optimized Dockerfile

* Updated Dockerfile to utilize a multi-stage build. This will make the built image lighter, by about 300mb.
Also safer, as 8.3-fpm has 55 known vulnerabilities, while 8.3-fpm-alpine has 0.

* Removed runtime dependencies from the build stage.

* remove unneeded php extensions from stage 1 build step
This commit is contained in:
Karjack182
2025-02-26 03:04:02 +02:00
committed by GitHub
parent a3f875270b
commit 0d06ca6a04
+46 -18
View File
@@ -1,17 +1,16 @@
FROM php:8.3-fpm
# Stage 1: Build stage
FROM php:8.3-fpm AS builder
ENV DEBIAN_FRONTEND=noninteractive
ENV APP_NAME=Investbrain
ENV VITE_APP_NAME=Investbrain
# Set the working directory
COPY . /var/app
WORKDIR /var/app
# Install required packages
RUN apt-get update && apt-get upgrade -y \
&& apt-get install -y \
nginx \
libfreetype-dev \
libjpeg62-turbo-dev \
libpng-dev \
@@ -20,22 +19,18 @@ RUN apt-get update && apt-get upgrade -y \
libicu-dev \
libpq-dev \
binutils libc6-dev \
supervisor \
unzip curl git \
nodejs npm \
# Clean up APT
&& apt-get -y autoremove \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
# Install PHP extensions
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) \
gd pgsql zip pdo_mysql mysqli intl
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# Remove default nginx config
RUN rm /etc/nginx/sites-enabled/default \
&& rm -rf /var/www/html \
&& ln -s /var/app /var/www/app
# Install PHP extensions
RUN docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd zip
# Copy application files
COPY . .
# Install Composer and Node.js Install PHP dependencies and build front end assets
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
@@ -43,13 +38,46 @@ RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local
&& npm install && npm run build \
&& rm -rf node_modules
# Stage 2: Production stage
FROM php:8.3-fpm-alpine
# Set the working directory
WORKDIR /var/app
# Copy necessary files from the builder stage
COPY --from=builder /var/app /var/app
COPY --from=builder /usr/local/etc/php/conf.d /usr/local/etc/php/conf.d
COPY --from=builder /usr/local/bin/composer /usr/local/bin/composer
# Install required Alpine packages
RUN apk add --no-cache \
nginx \
supervisor \
libpng-dev \
libzip-dev \
icu-dev \
postgresql-dev \
freetype-dev \
libjpeg-turbo-dev \
bash \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) \
gd pgsql zip pdo_mysql mysqli intl
# Remove default nginx config
RUN rm -rf /var/www/html \
&& ln -s /var/app /var/www/app
# Create required directories for supervisord
RUN mkdir -p /var/log/supervisor /var/run/supervisor
# Copy over configs
COPY ./docker/nginx.conf /etc/nginx/conf.d/default.conf
COPY ./docker/nginx.conf /etc/nginx/http.d/default.conf
COPY ./docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
# Set permissions and link storage
RUN php artisan storage:link \
&& chown -R www-data:www-data . \
&& chown -R www-data:www-data . \
&& chmod +x ./docker/entrypoint.sh
# Serve on port 80
@@ -59,4 +87,4 @@ EXPOSE 80
HEALTHCHECK --interval=30s --timeout=10s --retries=3 CMD curl -f http://localhost/up || exit 1
# Run everything else
ENTRYPOINT ["/bin/bash", "./docker/entrypoint.sh"]
ENTRYPOINT ["/bin/sh", "./docker/entrypoint.sh"]