wasAsked.

How would you containerize and securely configure a Spring Boot and React application for production?

Flipkart · Full Stack Developer · 3-4-years · technical

Published

Year not specified · Java, Docker, React

Community answers

Posted by wasAsked editorial collection

A common setup: a Spring Boot image for the API and an nginx image serving the React build (or the API serving the static files), deployed to Kubernetes or a container platform. Building images - React: multi-stage Dockerfile — build with a node image (npm ci, npm run build), then copy the static files into a minimal nginx image (nginx-unprivileged runs as non-root). Configure SPA fallback to index.html, gzip or Brotli, long cache headers for hashed assets and no-cache for index.html. - Spring Boot: - Use layered jars (Spring Boot's layertools or jarmode) or Cloud Native Buildpacks (./mvnw spring-boot:build-image) to create efficient, cache-friendly layers. - Base image: a JRE (not JDK) image such as eclipse-temurin JRE, or distroless or chiseled images, pinned by version and patched regularly. - Run as a non-root user and expose port 8080. - .dockerignore to keep target, node_modules, .git and secrets out of the build context. - Scan images (Trivy, Grype, Docker Scout) and sign them (cosign) in CI. JVM in containers - Modern JVMs respect container memory limits; set -XX:MaxRAMPercentage=75 rather than a fixed -Xmx, and leave headroom for metaspace and threads. - Consider Class Data Sharing (AppCDS) or Spring AOT and GraalVM native images for faster startup when needed. Configuration - Same image in every environment; configuration through environment variables or mounted files (SPRING_PROFILES_ACTIVE, SPRING_DATASOURCE_URL), Kubernetes ConfigMaps, or Spring Cloud Config. - React configuration is fixed at build time; for one image across environments, load a runtime config.json, and never put secrets in frontend bundles. Secrets - Never bake secrets into images or commit them. Use Kubernetes Secrets fed by an external secret manager (External Secrets Operator with AWS Secrets Manager, Azure Key Vault or HashiCorp Vault), or cloud workload identity so the app needs no stored credentials. Runtime hardening and operations - Actuator health groups for readiness and liveness probes; graceful shutdown (server.shutdown=graceful). - Resource requests and limits; read-only root filesystem with writable volumes only where needed; dropped Linux capabilities; no privilege escalation. - TLS at the ingress; configure forwarded headers (server.forward-headers-strategy=framework) so redirects and client IPs are correct. - Structured logs to stdout, metrics via Actuator and Prometheus, and tracing via OpenTelemetry. - Expose only necessary Actuator endpoints, secured or on a separate management port. Delivery: build once in CI, tag images with the commit SHA, push to a registry and promote the same image through environments.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics