Multi-Stage Docker Builds for Frontend Assets and Static Collection

Docker Django

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?

  1. Deterministic CSS in every build: Relying on a pre-compiled static/css/output.css from the developer's host machine means a clean Git checkout or CI runner will run collectstatic without the compiled stylesheet, deploying an unstyled UI.
  2. Correct ordering (tailwindcss -> collectstatic): Django's collectstatic copies files from each app's static/ folder (and STATICFILES_DIRS) into STATIC_ROOT. Compiling output.css before collectstatic ensures the newly minified stylesheet is included in STATIC_ROOT.
  3. Minimal runtime footprint: Installing Node.js in builder-base and running rm -rf node_modules before COPY --from=builder-base /app /app keeps the Node runtime, npm, and hundreds of megabytes of node_modules out 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

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.js is in content
  • 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.