How would you containerize and safely configure an ASP.NET Core and React application for production?
Flipkart · Full Stack Developer · 3-4-years · technical
Published
Year not specified · .NET, Docker, React
Community answers
Posted by wasAsked editorial collection
Build an immutable multi-stage image, run as a non-root user, expose only the application port, and keep environment-specific configuration outside the image.
Example:
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY . .
RUN dotnet publish -c Release -o /app --no-self-contained
FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY --from=build /app .
USER $APP_UID
ENTRYPOINT ["dotnet", "Api.dll"]
Pin supported base versions/digests, scan dependencies and images, add readiness/liveness endpoints, set CPU/memory limits, and handle SIGTERM gracefully. Inject secrets through a secret store or mounted file, never build arguments or committed appsettings files.
Backbencher Developer · · 0 likes
A common production layout is two images, or one image where ASP.NET Core serves the built React files.
Dockerfiles
- Use multi-stage builds so build tools never reach production:
- React: build with node, then copy dist into nginx (or into the ASP.NET Core wwwroot).
- ASP.NET Core: build with mcr.microsoft.com/dotnet/sdk, publish, then copy into the smaller aspnet runtime image (or a chiseled or Alpine variant).
- Pin base image versions and rebuild regularly for security patches.
- Run as non-root: .NET 8+ images include a non-root "app" user (USER app), and listen on port 8080.
- Add a .dockerignore so bin/obj, node_modules, .git and local secrets are not copied.
- Keep images small and scan them (Trivy, Docker Scout, Defender for Containers).
Configuration
- Follow twelve-factor: the same image runs in every environment; configuration comes from environment variables (ConnectionStrings__Default, ASPNETCORE_ENVIRONMENT=Production).
- React is static, so its configuration is baked in at build time. Either build per environment, or load /config.json at startup so one image can serve every environment. Never put secrets in frontend bundles.
Secrets
- Do not bake secrets into images or commit .env files. Use Kubernetes Secrets with an external secret store, Azure Key Vault with managed identity, or your platform's secret injection.
Runtime safety
- Health checks: expose /health/live and /health/ready (AddHealthChecks) for orchestrator probes.
- Resource limits for CPU and memory; .NET respects container limits for GC and thread pool sizing.
- Read-only root filesystem where possible, with writable volumes only where needed.
- HTTPS termination at the ingress or load balancer; configure forwarded headers in ASP.NET Core so redirects and scheme detection work.
- Data Protection keys persisted to shared storage (blob, Redis) when running several replicas, or cookies and antiforgery tokens break after scaling or restarts.
- Logs to stdout in structured JSON for the platform to collect.
Delivery
- Build once in CI, tag with the commit SHA, push to a registry, and promote the same image through environments.