mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-01-14 06:09:33 +00:00
Optimize Docker build: Build assets outside ARM emulation
- Add multi-stage build with separate composer-deps and assets stages - Assets are built once on native platform (node:22-bookworm-slim) - Pre-built assets copied into final images for all platforms - Remove Node.js/Yarn installation from final images (smaller size) - Use --ignore-platform-reqs for composer in assets stage - Applies to both Dockerfile and Dockerfile-frankenphp Co-authored-by: jbtronics <5410681+jbtronics@users.noreply.github.com>
This commit is contained in:
parent
380a11dafe
commit
2e79df3155
2 changed files with 68 additions and 45 deletions
49
Dockerfile
49
Dockerfile
|
|
@ -1,6 +1,37 @@
|
|||
ARG BASE_IMAGE=debian:bookworm-slim
|
||||
ARG PHP_VERSION=8.4
|
||||
|
||||
# ---
|
||||
# Build assets stage - runs on native platform (not emulated)
|
||||
# This stage builds the frontend assets (JavaScript, CSS) using Node.js and Yarn
|
||||
FROM composer:latest AS composer-deps
|
||||
|
||||
WORKDIR /build
|
||||
|
||||
# Copy composer files and install PHP dependencies (needed for Symfony UX assets)
|
||||
COPY composer.json composer.lock symfony.lock ./
|
||||
RUN composer install --no-dev --no-scripts --no-autoloader --prefer-dist --ignore-platform-reqs
|
||||
|
||||
# ---
|
||||
|
||||
FROM node:22-bookworm-slim AS assets
|
||||
|
||||
WORKDIR /build
|
||||
|
||||
# Copy vendor directory from composer stage (needed for Symfony UX packages)
|
||||
COPY --from=composer-deps /build/vendor ./vendor
|
||||
|
||||
# Copy package files
|
||||
COPY package.json yarn.lock webpack.config.js ./
|
||||
COPY assets ./assets
|
||||
|
||||
# Install dependencies and build assets
|
||||
RUN yarn install --network-timeout 600000 && \
|
||||
yarn build && \
|
||||
yarn cache clean
|
||||
|
||||
# ---
|
||||
|
||||
FROM ${BASE_IMAGE} AS base
|
||||
ARG PHP_VERSION
|
||||
|
||||
|
|
@ -45,15 +76,6 @@ RUN apt-get update && apt-get -y install \
|
|||
# delete the "index.html" that installing Apache drops in here
|
||||
&& rm -rvf /var/www/html/*
|
||||
|
||||
# Install node and yarn
|
||||
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
|
||||
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
|
||||
curl -sL https://deb.nodesource.com/setup_22.x | bash - && \
|
||||
apt-get update && apt-get install -y \
|
||||
nodejs \
|
||||
yarn \
|
||||
&& apt-get -y autoremove && apt-get clean autoclean && rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Install composer
|
||||
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
|
||||
|
||||
|
|
@ -149,14 +171,13 @@ RUN a2dissite 000-default.conf && \
|
|||
a2enconf docker-php && \
|
||||
a2enmod rewrite
|
||||
|
||||
# Install composer and yarn dependencies for Part-DB
|
||||
# Install composer dependencies for Part-DB
|
||||
USER www-data
|
||||
RUN composer install -a --no-dev && \
|
||||
composer clear-cache
|
||||
RUN yarn install --network-timeout 600000 && \
|
||||
yarn build && \
|
||||
yarn cache clean && \
|
||||
rm -rf node_modules/
|
||||
|
||||
# Copy pre-built assets from the assets stage
|
||||
COPY --from=assets --chown=www-data:www-data /build/public/build ./public/build
|
||||
|
||||
# Use docker env to output logs to stdout
|
||||
ENV APP_ENV=docker
|
||||
|
|
|
|||
|
|
@ -1,3 +1,34 @@
|
|||
# ---
|
||||
# Build assets stage - runs on native platform (not emulated)
|
||||
# This stage builds the frontend assets (JavaScript, CSS) using Node.js and Yarn
|
||||
FROM composer:latest AS composer-deps
|
||||
|
||||
WORKDIR /build
|
||||
|
||||
# Copy composer files and install PHP dependencies (needed for Symfony UX assets)
|
||||
COPY composer.json composer.lock symfony.lock ./
|
||||
RUN composer install --no-dev --no-scripts --no-autoloader --prefer-dist --ignore-platform-reqs
|
||||
|
||||
# ---
|
||||
|
||||
FROM node:22-bookworm-slim AS assets
|
||||
|
||||
WORKDIR /build
|
||||
|
||||
# Copy vendor directory from composer stage (needed for Symfony UX packages)
|
||||
COPY --from=composer-deps /build/vendor ./vendor
|
||||
|
||||
# Copy package files
|
||||
COPY package.json yarn.lock webpack.config.js ./
|
||||
COPY assets ./assets
|
||||
|
||||
# Install dependencies and build assets
|
||||
RUN yarn install --network-timeout 600000 && \
|
||||
yarn build && \
|
||||
yarn cache clean
|
||||
|
||||
# ---
|
||||
|
||||
FROM dunglas/frankenphp:1-php8.4 AS frankenphp_upstream
|
||||
|
||||
RUN apt-get update && apt-get -y install \
|
||||
|
|
@ -13,33 +44,6 @@ RUN apt-get update && apt-get -y install \
|
|||
zip \
|
||||
&& apt-get -y autoremove && apt-get clean autoclean && rm -rf /var/lib/apt/lists/*;
|
||||
|
||||
RUN set -eux; \
|
||||
# Prepare keyrings directory
|
||||
mkdir -p /etc/apt/keyrings; \
|
||||
\
|
||||
# Import Yarn GPG key
|
||||
curl -fsSL https://dl.yarnpkg.com/debian/pubkey.gpg \
|
||||
| tee /etc/apt/keyrings/yarn.gpg >/dev/null; \
|
||||
chmod 644 /etc/apt/keyrings/yarn.gpg; \
|
||||
\
|
||||
# Add Yarn repo with signed-by
|
||||
echo "deb [signed-by=/etc/apt/keyrings/yarn.gpg] https://dl.yarnpkg.com/debian stable main" \
|
||||
| tee /etc/apt/sources.list.d/yarn.list; \
|
||||
\
|
||||
# Run NodeSource setup script (unchanged)
|
||||
curl -sL https://deb.nodesource.com/setup_22.x | bash -; \
|
||||
\
|
||||
# Install Node.js + Yarn
|
||||
apt-get update; \
|
||||
apt-get install -y --no-install-recommends \
|
||||
nodejs \
|
||||
yarn; \
|
||||
\
|
||||
# Cleanup
|
||||
apt-get -y autoremove; \
|
||||
apt-get clean autoclean; \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
|
||||
|
||||
# Install PHP
|
||||
RUN set -eux; \
|
||||
|
|
@ -90,10 +94,8 @@ RUN set -eux; \
|
|||
composer run-script --no-dev post-install-cmd; \
|
||||
chmod +x bin/console; sync;
|
||||
|
||||
RUN yarn install --network-timeout 600000 && \
|
||||
yarn build && \
|
||||
yarn cache clean && \
|
||||
rm -rf node_modules/
|
||||
# Copy pre-built assets from the assets stage
|
||||
COPY --from=assets /build/public/build ./public/build
|
||||
|
||||
# Use docker env to output logs to stdout
|
||||
ENV APP_ENV=docker
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue