Files
nhgooi.nl/Dockerfile
T
eric.busandClaude Opus 5.5 f7b1dbac8f Generate minified CSS/JS in the Docker build, remove unused assets
- New 'assets' stage in the Dockerfile builds style.min.css and
  functions.min.js from the sources (npm run build:min); they are no
  longer committed, so they can't go stale anymore
- Production (APP_DEBUG off) loads the minified files, local development
  the readable ones, both with a cache-busting version (AssetHelper,
  Asset::url() in the layout)
- Source maps are excluded from the image
- Remove unused assets: public/css/old, public/js/old and unused copies
  of jQuery, wavesurfer, prettyPhoto (min) and mediaplayer_with_plugins
- npm scripts: build:css (committed CSS), build:min (Docker build);
  sass-minify scripts removed
- readme: how the CSS/JS build works

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 11:00:35 +01:00

107 lines
3.1 KiB
Docker

# Build the minified CSS/JS (style.min.css, functions.min.js) from the sources.
# Needs the npm devDependencies (sass, grunt), which the final image doesn't get.
FROM node:20-alpine AS assets
WORKDIR /build
COPY package.json package-lock.json ./
RUN npm ci --ignore-scripts
COPY Gruntfile.cjs ./
COPY resources/assets ./resources/assets
RUN mkdir -p public/css public/js && npm run build:min
FROM php:8.3.37-apache
# Install Node.js
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && apt-get install -y nodejs
RUN apt-get update && apt-get install -y \
git \
curl \
libicu-dev \
libzip-dev \
libonig-dev \
libssh2-1 \
libssh2-1-dev \
&& rm -rf /var/lib/apt/lists/*
RUN pecl install ssh2 \
&& docker-php-ext-enable ssh2 \
&& docker-php-ext-configure intl \
&& docker-php-ext-install \
pdo \
pdo_mysql \
mysqli \
intl \
ftp \
mbstring \
bcmath \
zip
RUN a2enmod rewrite
COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer
WORKDIR /var/www/app
ENV APACHE_DOCUMENT_ROOT=/var/www/app/public
RUN sed -ri \
-e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' \
/etc/apache2/sites-available/*.conf \
/etc/apache2/apache2.conf \
/etc/apache2/conf-available/*.conf
RUN echo '<Directory /var/www/app/public>\n\
AllowOverride All\n\
Require all granted\n\
</Directory>' > /etc/apache2/conf-available/laravel.conf \
&& a2enconf laravel
# Install PHP dependencies first (cached layer, only rebuilt when composer files change).
# Scripts and autoloader are skipped here because the app code isn't copied yet.
COPY composer.json composer.lock ./
RUN composer install \
--no-dev \
--prefer-dist \
--no-interaction \
--no-scripts \
--no-autoloader
# Install npm dependencies (cached layer, only rebuilt when package.json/lock change).
# The postinstall script is skipped here; the assets are copied after the app code below.
COPY package.json package-lock.json ./
RUN npm ci --omit=dev --ignore-scripts
# Copy app files
COPY . .
# Minified CSS/JS from the 'assets' stage (generated, not in git)
COPY --from=assets /build/public/css/style.min.css public/css/
COPY --from=assets /build/public/js/functions.min.js public/js/
# Create storage directories
RUN mkdir -p \
storage/app \
storage/framework/cache/data \
storage/framework/sessions \
storage/framework/views \
storage/logs \
bootstrap/cache \
public/assets/vendor
# Generate the optimized autoloader and run composer scripts (package:discover)
RUN composer dump-autoload --no-dev --optimize --no-interaction
# Copy the npm packages into public/assets/vendor, then make the directories
# Laravel writes to owned by www-data, so the container can run as uid 33
RUN node scripts/setup-vendor.js \
&& chown -R www-data:www-data storage bootstrap/cache public/assets/vendor \
&& chmod -R ug+rwX storage bootstrap/cache public/assets/vendor
# Copy and set up entrypoint script
COPY docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 80