Skip to content

Commit dfc7eb7

Browse files
Copilotjbtronics
andcommitted
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 <[email protected]>
1 parent e00dfe1 commit dfc7eb7

File tree

2 files changed

+68
-45
lines changed

2 files changed

+68
-45
lines changed

Dockerfile

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
11
ARG BASE_IMAGE=debian:bookworm-slim
22
ARG PHP_VERSION=8.4
33

4+
# ---
5+
# Build assets stage - runs on native platform (not emulated)
6+
# This stage builds the frontend assets (JavaScript, CSS) using Node.js and Yarn
7+
FROM composer:latest AS composer-deps
8+
9+
WORKDIR /build
10+
11+
# Copy composer files and install PHP dependencies (needed for Symfony UX assets)
12+
COPY composer.json composer.lock symfony.lock ./
13+
RUN composer install --no-dev --no-scripts --no-autoloader --prefer-dist --ignore-platform-reqs
14+
15+
# ---
16+
17+
FROM node:22-bookworm-slim AS assets
18+
19+
WORKDIR /build
20+
21+
# Copy vendor directory from composer stage (needed for Symfony UX packages)
22+
COPY --from=composer-deps /build/vendor ./vendor
23+
24+
# Copy package files
25+
COPY package.json yarn.lock webpack.config.js ./
26+
COPY assets ./assets
27+
28+
# Install dependencies and build assets
29+
RUN yarn install --network-timeout 600000 && \
30+
yarn build && \
31+
yarn cache clean
32+
33+
# ---
34+
435
FROM ${BASE_IMAGE} AS base
536
ARG PHP_VERSION
637

@@ -45,15 +76,6 @@ RUN apt-get update && apt-get -y install \
4576
# delete the "index.html" that installing Apache drops in here
4677
&& rm -rvf /var/www/html/*
4778

48-
# Install node and yarn
49-
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
50-
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
51-
curl -sL https://deb.nodesource.com/setup_22.x | bash - && \
52-
apt-get update && apt-get install -y \
53-
nodejs \
54-
yarn \
55-
&& apt-get -y autoremove && apt-get clean autoclean && rm -rf /var/lib/apt/lists/*
56-
5779
# Install composer
5880
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
5981

@@ -149,14 +171,13 @@ RUN a2dissite 000-default.conf && \
149171
a2enconf docker-php && \
150172
a2enmod rewrite
151173

152-
# Install composer and yarn dependencies for Part-DB
174+
# Install composer dependencies for Part-DB
153175
USER www-data
154176
RUN composer install -a --no-dev && \
155177
composer clear-cache
156-
RUN yarn install --network-timeout 600000 && \
157-
yarn build && \
158-
yarn cache clean && \
159-
rm -rf node_modules/
178+
179+
# Copy pre-built assets from the assets stage
180+
COPY --from=assets --chown=www-data:www-data /build/public/build ./public/build
160181

161182
# Use docker env to output logs to stdout
162183
ENV APP_ENV=docker

Dockerfile-frankenphp

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1+
# ---
2+
# Build assets stage - runs on native platform (not emulated)
3+
# This stage builds the frontend assets (JavaScript, CSS) using Node.js and Yarn
4+
FROM composer:latest AS composer-deps
5+
6+
WORKDIR /build
7+
8+
# Copy composer files and install PHP dependencies (needed for Symfony UX assets)
9+
COPY composer.json composer.lock symfony.lock ./
10+
RUN composer install --no-dev --no-scripts --no-autoloader --prefer-dist --ignore-platform-reqs
11+
12+
# ---
13+
14+
FROM node:22-bookworm-slim AS assets
15+
16+
WORKDIR /build
17+
18+
# Copy vendor directory from composer stage (needed for Symfony UX packages)
19+
COPY --from=composer-deps /build/vendor ./vendor
20+
21+
# Copy package files
22+
COPY package.json yarn.lock webpack.config.js ./
23+
COPY assets ./assets
24+
25+
# Install dependencies and build assets
26+
RUN yarn install --network-timeout 600000 && \
27+
yarn build && \
28+
yarn cache clean
29+
30+
# ---
31+
132
FROM dunglas/frankenphp:1-php8.4 AS frankenphp_upstream
233

334
RUN apt-get update && apt-get -y install \
@@ -13,33 +44,6 @@ RUN apt-get update && apt-get -y install \
1344
zip \
1445
&& apt-get -y autoremove && apt-get clean autoclean && rm -rf /var/lib/apt/lists/*;
1546

16-
RUN set -eux; \
17-
# Prepare keyrings directory
18-
mkdir -p /etc/apt/keyrings; \
19-
\
20-
# Import Yarn GPG key
21-
curl -fsSL https://dl.yarnpkg.com/debian/pubkey.gpg \
22-
| tee /etc/apt/keyrings/yarn.gpg >/dev/null; \
23-
chmod 644 /etc/apt/keyrings/yarn.gpg; \
24-
\
25-
# Add Yarn repo with signed-by
26-
echo "deb [signed-by=/etc/apt/keyrings/yarn.gpg] https://dl.yarnpkg.com/debian stable main" \
27-
| tee /etc/apt/sources.list.d/yarn.list; \
28-
\
29-
# Run NodeSource setup script (unchanged)
30-
curl -sL https://deb.nodesource.com/setup_22.x | bash -; \
31-
\
32-
# Install Node.js + Yarn
33-
apt-get update; \
34-
apt-get install -y --no-install-recommends \
35-
nodejs \
36-
yarn; \
37-
\
38-
# Cleanup
39-
apt-get -y autoremove; \
40-
apt-get clean autoclean; \
41-
rm -rf /var/lib/apt/lists/*
42-
4347

4448
# Install PHP
4549
RUN set -eux; \
@@ -90,10 +94,8 @@ RUN set -eux; \
9094
composer run-script --no-dev post-install-cmd; \
9195
chmod +x bin/console; sync;
9296

93-
RUN yarn install --network-timeout 600000 && \
94-
yarn build && \
95-
yarn cache clean && \
96-
rm -rf node_modules/
97+
# Copy pre-built assets from the assets stage
98+
COPY --from=assets /build/public/build ./public/build
9799

98100
# Use docker env to output logs to stdout
99101
ENV APP_ENV=docker

0 commit comments

Comments
 (0)