Multi-Stage Docker Builds for Frontend Assets and Static Collection
Server-rendered Python applications (such as Django) often depend on a Node.js toolchain (e.g., Tailwind CSS, PostCSS, or esbuild) to compile frontend stylesheets and scripts before Django's collectstatic gathers them into STATIC_ROOT. Running collectstatic in a Dockerfile without compiling frontend assets first risks deploying missing or stale CSS, while installing Node.js in the final runtime stage bloats the image and expands its attack surface.
The Builder-to-Runtime Pipeline
A multi-stage Dockerfile resolves both problems by compiling frontend assets and collecting static files in an intermediate builder-base stage, removing transient build dependencies (node_modules), and copying only the finished artifacts into the minimal webapp runtime stage:
┌─────────────────────────────────────────────────────────────────────┐
│ Stage 1: builder-base (Python + Node.js + build-essential) │
│ 1. Install Python dependencies into virtualenv │
│ 2. npm ci (deterministic install from package-lock.json) │
│ 3. npx tailwindcss -i input.css -o output.css --minify │
│ 4. python manage.py collectstatic --noinput │
│ 5. rm -rf node_modules │
└──────────────────────────────────┬──────────────────────────────────┘
│ COPY --from=builder-base /app /app
▼
┌─────────────────────────────────────────────────────────────────────┐
│ Stage 2: webapp (Slim Python runtime only, non-root appuser) │
│ - Contains compiled output.css & populated STATIC_ROOT │
│ - Zero Node.js binaries, zero npm cache, zero node_modules │
└─────────────────────────────────────────────────────────────────────┘
Why Compile Inside the Builder Stage?
- Deterministic CSS in every build: Relying on a pre-compiled
static/css/output.cssfrom the developer's host machine means a clean Git checkout or CI runner will runcollectstaticwithout the compiled stylesheet, deploying an unstyled UI. - Correct ordering (
tailwindcss->collectstatic): Django'scollectstaticcopies files from each app'sstatic/folder (andSTATICFILES_DIRS) intoSTATIC_ROOT. Compilingoutput.cssbeforecollectstaticensures the newly minified stylesheet is included inSTATIC_ROOT. - Minimal runtime footprint: Installing Node.js in
builder-baseand runningrm -rf node_modulesbeforeCOPY --from=builder-base /app /appkeeps the Node runtime, npm, and hundreds of megabytes ofnode_modulesout of the production image.
Preventing Dynamic Class Purging in tailwind.config.js
Tailwind CSS generates only the utility classes it finds as literal strings in the files matched by the content array in tailwind.config.js. If client-side JavaScript (such as ./static/js/main.js) toggles utility classes dynamically via classList.add(), classList.toggle(), or template literals, omitting the JS path from content causes npx tailwindcss --minify to purge those classes from output.css unless they happen to appear in an HTML template:
module.exports = {
content: [
"./templates/**/*.html",
"./apps/**/templates/**/*.html",
"./static/js/main.js", // Required so JS-toggled classes are not purged
],
// ...
}
References
- Docker Docs: Multi-stage builds - Isolating build toolchains in intermediate stages so only compiled artifacts reach the runtime image.
- Tailwind CSS Documentation: Content Configuration - How Tailwind scans template and JS files to generate used utility classes and purge unused ones.
- Django Documentation: Managing static files (
collectstatic) - Gathering static assets intoSTATIC_ROOTfor production deployment. - Docker and Django Build and Runtime Hardening – Entrypoint, Multi-Stage Assets, and Compose Volumes - Lab implementing multi-stage Tailwind compilation in
Dockerfile.prod. - Container Entrypoint Hardening and Gunicorn Worker Configuration - Why
collectstatic --clearshould be removed fromentrypoint.shonce static files are baked at build time. - Docker and Container Security MOC - Hub for container security and build notes.
Review cards
Flashcard Why must Tailwind compile `output.css` before `collectstatic` in the builder stage?
collectstatic only copies files that already exist. Run it first on a clean checkout and STATIC_ROOT has no stylesheet, so the site deploys unstyled.
Flashcard How does the multi-stage build keep Node.js out of the production image?
Node and npm are only installed in builder-base, node_modules is deleted there, and the runtime stage only receives /app via COPY --from=builder-base /app /app.
Flashcard Why add `./static/js/main.js` to Tailwind's `content` array?
Tailwind only generates classes it finds in content files, so classes toggled only from JS would be purged from output.css.
Quiz `main.js` is in `content` and builds `"text-" + color + "-500"`. What happens to `text-red-500` in the production build?
- It's kept, because
main.jsis incontent - It's purged unless the full name appears in another scanned file
- Tailwind runs the script to find the class
- The build fails on the dynamic class
The scanner reads files as plain text and never runs JavaScript, so it only finds complete class names. Write "text-red-500" in full.