fix: 🧑‍💻 update build time and size of dev image

This commit is contained in:
Alexis Gousseau 2026-04-13 17:05:56 +02:00
parent f1a240835b
commit 5319ceff2f
No known key found for this signature in database
GPG key ID: 7F3C786EE12DBB72
6 changed files with 208 additions and 105 deletions

View file

@ -1,114 +1,178 @@
FROM php:8.5.4-apache-trixie
ENV DEBIAN_FRONTEND=noninteractive
# Stage n°1: builder.
# Cette étape ne sert qu'à l'installation et la compilation des extensions php.
# Une fois fait, on copie les fichiers compilés vers le stage base,
# et on supprime celui-là qui n'est donc plus utile, on gagne à peu près ~653MB.
FROM php:8.5.4-fpm-alpine AS builder
# Utilisation de l'image docker php:8.5.4-fpm-alpine: https://hub.docker.com/layers/library/php/8.5.4-fpm-alpine/images
# Bien plus légère que l'ancienne image php:8.5.4-apache-trixie.
# Récupération de la valeur de l'argument passé lors de la commande pour utiliser notre proxy.
ARG http_proxy
ENV http_proxy=${http_proxy}
ENV https_proxy=${http_proxy}
ENV HTTP_PROXY=${http_proxy}
ENV HTTPS_PROXY=${http_proxy}
# Définit des variables d'environnement dans l'image sous plusieurs formats car ça peu varier selon les outils.
ENV http_proxy=${http_proxy} \
https_proxy=${http_proxy} \
HTTP_PROXY=${http_proxy} \
HTTPS_PROXY=${http_proxy}
# Install system dependencies and PHP extensions
RUN apt-get update -qq && \
apt-get install -qy --no-install-recommends \
nano vim \
libfreetype6-dev libjpeg62-turbo-dev libpng-dev libwebp-dev libxpm-dev \
zlib1g-dev libicu-dev g++ \
libxslt-dev \
libzip-dev unzip zip \
libmagick++-dev libmagickwand-dev imagemagick \
libcurl4-openssl-dev \
libmemcached-dev \
ldap-utils libldap2-dev \
locales \
git zsh htop \
ca-certificates curl gnupg \
mariadb-client \
libonig-dev \
build-essential pkg-config autoconf libtool \
&& echo "fr_FR.UTF-8 UTF-8" > /etc/locale.gen \
&& locale-gen fr_FR.UTF-8 \
&& dpkg-reconfigure locales \
&& /usr/sbin/update-locale LANG=fr_FR.UTF-8
# Installation des packages nécessaires à la compilation des extensions php.
RUN apk add --no-cache \
unzip \
freetype-dev jpeg-dev libpng-dev libwebp-dev libxpm-dev \
zlib-dev icu-dev libxslt-dev libzip-dev \
imagemagick-dev \
libmemcached-dev cyrus-sasl-dev \
openldap-dev \
oniguruma-dev gettext-dev gmp-dev \
linux-headers \
g++ autoconf libtool pkgconfig alpine-sdk
# Installation des extensions php natives.
RUN docker-php-ext-configure gd --enable-gd --with-webp --with-jpeg --with-xpm --with-freetype \
&& docker-php-ext-configure zip --with-zip \
&& docker-php-ext-install -j$(nproc) exif gd gettext intl mysqli pdo_mysql xsl zip bcmath ldap gmp
&& docker-php-ext-configure zip --with-zip \
&& docker-php-ext-install -j"$(nproc)" \
exif \
gd \
gettext \
intl \
mysqli \
pdo_mysql \
xsl \
zip \
bcmath \
ldap \
gmp
# PECL PROXY HACK
RUN if [ -n "${http_proxy}" ]; then \
pear config-set http_proxy "${http_proxy}"; \
fi
# PECL maintenant déprécié, on installe directement les extensions souhaitée depuis github.
RUN \
curl -fsSL https://github.com/Imagick/imagick/archive/refs/tags/3.8.1.tar.gz -o /tmp/imagick.tar.gz & \
curl -fsSL https://github.com/php-memcached-dev/php-memcached/archive/refs/tags/v3.4.0.tar.gz -o /tmp/memcached.tar.gz & \
curl -fsSL https://github.com/phpredis/phpredis/archive/refs/tags/6.3.0.tar.gz -o /tmp/redis.tar.gz & \
curl -fsSL https://github.com/xdebug/xdebug/archive/refs/tags/3.5.1.tar.gz -o /tmp/xdebug.tar.gz & \
curl -fsSL https://github.com/krakjoe/pcov/archive/refs/tags/v1.0.12.tar.gz -o /tmp/pcov.tar.gz & \
wait \
\
&& tar -xzf /tmp/imagick.tar.gz -C /tmp \
&& cd /tmp/imagick-3.8.1 \
&& phpize && ./configure && make -j"$(nproc)" && make install \
&& rm -rf /tmp/imagick* \
\
&& tar -xzf /tmp/memcached.tar.gz -C /tmp \
&& cd /tmp/php-memcached-3.4.0 \
&& phpize && ./configure && make -j"$(nproc)" && make install \
&& rm -rf /tmp/memcached* \
\
&& tar -xzf /tmp/redis.tar.gz -C /tmp \
&& cd /tmp/phpredis-6.3.0 \
&& phpize && ./configure && make -j"$(nproc)" && make install \
&& rm -rf /tmp/redis* \
\
&& tar -xzf /tmp/xdebug.tar.gz -C /tmp \
&& cd /tmp/xdebug-3.5.1 \
&& phpize && ./configure && make -j"$(nproc)" && make install \
&& rm -rf /tmp/xdebug* \
\
&& tar -xzf /tmp/pcov.tar.gz -C /tmp \
&& cd /tmp/pcov-1.0.12 \
&& phpize && ./configure && make -j"$(nproc)" && make install \
&& rm -rf /tmp/pcov*
# PECL EXTENSIONS
RUN pecl channel-update pecl.php.net \
&& pecl install imagick-3.8.1 redis-6.3.0 \
&& docker-php-ext-enable imagick redis
# Installation composer + Installation bun (sous alpine).
RUN curl -fsSL https://getcomposer.org/installer -o /tmp/composer-installer & \
curl -fsSL -o /tmp/bun.zip "https://github.com/oven-sh/bun/releases/download/bun-v1.2.10/bun-linux-x64-musl.zip" & \
wait \
\
&& php /tmp/composer-installer --install-dir=/usr/local/bin --filename=composer --version=2.9.7 \
\
&& rm /tmp/composer-installer \
&& unzip -oqd /tmp/bun-extracted /tmp/bun.zip \
&& mv $(find /tmp/bun-extracted -type f -name 'bun' -perm /a+x | head -n1) /usr/local/bin/bun \
&& chmod +x /usr/local/bin/bun \
&& rm -rf /tmp/bun.zip /tmp/bun-extracted
RUN pecl install xdebug pcov \
&& docker-php-ext-enable xdebug pcov
# Stage n°2: base.
# Image finale qui sera utilisée sans compilateurs.
FROM php:8.5.4-fpm-alpine AS base
# CLEAN UP XDEBUG ENABLE
RUN rm -f /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
# Récupération de la valeur de l'argument passé lors de la commande pour utiliser notre proxy.
ARG http_proxy
# CLEANUP
RUN apt-get clean \
&& apt-get purge -y --auto-remove build-essential pkg-config autoconf \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# Définit des variables d'environnement dans l'image sous plusieurs formats car ça peu varier selon les outils.
ENV http_proxy=${http_proxy} \
https_proxy=${http_proxy} \
HTTP_PROXY=${http_proxy} \
HTTPS_PROXY=${http_proxy} \
LC_ALL=fr_FR.UTF-8 \
LANG=fr_FR.UTF-8 \
LANGUAGE=fr_FR.UTF-8:fr \
SHELL=/bin/zsh
# Set locale environment variables
ENV LC_ALL=fr_FR.UTF-8
ENV LANG=fr_FR.UTF-8
ENV LANGUAGE=fr_FR.UTF-8:fr
# Installation des packages runtime + utilitaires + outils QA.
RUN apk add --no-cache \
ca-certificates curl \
git unzip zip nano vim zsh htop \
apache2 apache2-proxy apache2-http2 apache2-ssl apache2-brotli \
nodejs npm \
freetype libjpeg libpng libwebp libxpm \
icu-libs libxslt libzip \
imagemagick \
libcurl libmemcached \
libldap \
musl-locales musl-locales-lang \
mariadb-client \
oniguruma \
shadow man-db \
gmp gettext-libs \
zsh-vcs
# Install Node.js 24.x
RUN mkdir -p /etc/apt/keyrings \
&& curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \
&& echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_24.x nodistro main" > /etc/apt/sources.list.d/nodesource.list \
&& apt-get update -qq \
&& apt-get install -y --no-install-recommends nodejs \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# Copie des extensions compilées depuis le stage builder.
COPY --from=builder /usr/local/lib/php/extensions/ /usr/local/lib/php/extensions/
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
COPY --from=builder /usr/local/bin/bun /usr/local/bin/bun
# Install Bun
RUN set -eux; \
dir="${BUN_INSTALL:-$HOME/.bun}/bin"; mkdir -p "$dir"; \
url="https://github.com/oven-sh/bun/releases/latest/download/bun-linux-x64.zip"; \
curl -fsSL -o /tmp/bun.zip "$url"; \
unzip -oqd "$dir" /tmp/bun.zip; \
bunbin=$(find "$dir" -type f -name 'bun*' -perm /a+x | head -n1); \
[ -n "$bunbin" ] && mv -f "$bunbin" "$dir/bun"; \
chmod +x "$dir/bun" || true; \
rm -f /tmp/bun.zip; \
ln -sf "$dir/bun" /usr/local/bin/bun
# Active les extensions PECL copiées + supprime xdebug.ini auto-généré car on veut utiliser notre propre fichier de config.
RUN docker-php-ext-enable imagick memcached redis xdebug pcov \
&& rm -f /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& mkdir -p /app/logs && chmod 777 /app/logs
# Install Composers
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer --version=2.9.5
# Setup Oh-My-Zsh and plugins (dev only)
# Installation omzsh + plugin + theme.
RUN sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" \
&& git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions \
&& git clone --depth=1 https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions \
&& sed -i 's/plugins=(git)/plugins=(git laravel zsh-autosuggestions)/g' ~/.zshrc \
&& sed -i 's/ZSH_THEME="robbyrussell"/ZSH_THEME="agnoster"/g' ~/.zshrc
&& sed -i 's/ZSH_THEME="robbyrussell"/ZSH_THEME="agnoster"/g' ~/.zshrc \
&& chsh -s /bin/zsh
ENV SHELL=/bin/zsh
# Configuration des modules apache sans duplication (par défaut dans alpine c'est commenté, on décommente juste ce dont on a besoin),
# + supprime ssl.conf auto-généré car on veut utiliser notre propre fichier de config.
RUN sed -i \
-e 's/#LoadModule proxy_module/LoadModule proxy_module/' \
-e 's/#LoadModule proxy_fcgi_module/LoadModule proxy_fcgi_module/' \
-e 's/#LoadModule rewrite_module/LoadModule rewrite_module/' \
-e 's/#LoadModule remoteip_module/LoadModule remoteip_module/' \
-e 's/#LoadModule headers_module/LoadModule headers_module/' \
-e 's/#LoadModule http2_module/LoadModule http2_module/' \
-e 's/#LoadModule expires_module/LoadModule expires_module/' \
-e 's/#LoadModule deflate_module/LoadModule deflate_module/' \
-e 's/#LoadModule mime_module/LoadModule mime_module/' \
-e 's/#LoadModule brotli_module/LoadModule brotli_module/' \
-e 's/^Listen 80/#Listen 80/' \
/etc/apache2/httpd.conf \
\
&& rm -f /etc/apache2/conf.d/ssl.conf
# Set default shell to zsh
RUN chsh -s /bin/zsh
# Copie des fichiers de configuration.
COPY conf/php.ini /usr/local/etc/php/conf.d/app.ini
COPY conf/xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini
COPY conf/vhost.conf /etc/apache2/conf.d/vhost.conf
COPY conf/php-fpm.conf /usr/local/etc/php-fpm.d/www.conf
COPY conf/common-apache.conf /etc/apache2/conf.d/z-app.conf
COPY conf/common-ports.conf /etc/apache2/conf.d/ports.conf
COPY conf/common-security.conf /etc/apache2/conf.d/security.conf
# Copy configuration files
COPY conf/common-apache.conf /etc/apache2/conf-available/z-app.conf
COPY conf/common-ports.conf /etc/apache2/ports.conf
COPY conf/common-security.conf /etc/apache2/conf-enabled/security.conf
COPY conf/php.ini /usr/local/etc/php/conf.d/app.ini
COPY conf/xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini
COPY conf/vhost.conf /etc/apache2/sites-available/000-default.conf
# Enable Apache modules and configuration
RUN a2enmod rewrite remoteip headers http2 && \
a2enconf z-app
# XDEBUG LOGS
RUN mkdir -p /app/logs/
# Définit le répertoire de travail par défaut dans le conteneur.
WORKDIR /app/srcs
# Entrypoint custom pour démarrer php-fpm et apache (en executable seule).
COPY --chmod=755 conf/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]

View file

@ -73,7 +73,6 @@ AccessFileName .htaccess
#
DefaultType None
#
# HostnameLookups: Log the names of clients or just their IP addresses
# e.g., www.apache.org (on) or 204.62.129.132 (off).
@ -89,8 +88,9 @@ HostnameLookups Off
# container, error messages relating to that virtual host will be
# logged here. If you *do* define an error logfile for a <VirtualHost>
# container, that host's errors will be logged there and not here.
# APACHE_LOG_DIR n'existe pas sous alpine.
#
ErrorLog ${APACHE_LOG_DIR}/error.log
ErrorLog /var/log/apache2/error.log
#
# LogLevel: Control the number of messages logged to the error_log.

View file

@ -2,7 +2,6 @@
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf
#Listen 80
Listen 443
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

View file

@ -0,0 +1,13 @@
#!/bin/sh
set -e
mkdir -p /app/logs
# Vérifier la config php-fpm.
php-fpm -t
# Démarrer php-fpm en arrière-plan.
php-fpm -D
# Démarrer apache au premier plan.
exec httpd -D FOREGROUND

View file

@ -0,0 +1,24 @@
[global]
; Fait en sorte que php fpm reste au premier plan et soit toujours géré par docker.
daemonize = no
[www]
; Utilisateur par défaut sous alpine.
user = nobody
group = nobody
; Localhost + port par défaut de php-fpm.
listen = 127.0.0.1:9000
; Gestion des processus nécéssaire au bon fonctionnement de php-fpm:
pm = dynamic ; Type de processus.
pm.max_children = 20 ; Nombre maximum de processus en même temps.
pm.start_servers = 4 ; Nombre de processus créés au démarrage.
pm.min_spare_servers = 2 ; Minimum de processus en attente.
pm.max_spare_servers = 6 ; Maximum de processus en attente.
; Directives php définies dans le scope php-fpm et pas apache.
; Anciennement définies dans le vhost avec le module mod_php.
php_admin_value[open_basedir] = /usr/local/bin/php:/app/srcs/:/app/tmp/
php_admin_value[upload_tmp_dir] = /app/tmp
php_admin_value[error_log] = /app/logs/app_error_${PROJECT}.log

View file

@ -1,18 +1,21 @@
<VirtualHost *:443>
Protocols h2 h2c http/1.1
ServerAdmin contact@ldc.fr
DocumentRoot /app/${DOCUMENTROOT}
ServerAdmin digital@ldc.fr
DocumentRoot /app${DOCUMENTROOT}
DirectoryIndex index.php index.html
<Directory /app/${DOCUMENTROOT}>
# Anciennement activé par défaut via le module d'apachemod_proxy_fcgi,
# sur alpine, on doit l'écrire (php-fpm sur le port 9000).
<FilesMatch "\.php$">
SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>
<Directory /app${DOCUMENTROOT}>
Options +MultiViews -Indexes +FollowSymLinks
AllowOverride All
Require all granted
php_admin_value open_basedir "/usr/local/bin/php:/app/srcs/:/app/tmp/"
php_admin_value upload_tmp_dir /app/tmp
php_admin_value error_log /app/logs/app_error_${PROJECT}.log
</Directory>
ErrorLog /app/logs/error_${PROJECT}.log
CustomLog /app/logs/access_${PROJECT}.log combined
ErrorLog /app/logs/error_${PROJECT}.log
CustomLog /app/logs/access_${PROJECT}.log combined
</VirtualHost>