Interview Prep/Real-World Interview/Write-a-Dockerfile tasks

Write a Dockerfile for a static React site served by Nginx.

BeginnerHands-onWrite-a-Dockerfile tasks
Answer
FROM node:22-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM nginx:1.27-alpine
COPY --from=build /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80

nginx.conf with SPA routing:

server {
  listen 80;
  root /usr/share/nginx/html;
  location / { try_files $uri $uri/ /index.html; }
}

Key trick → try_files ... /index.html sends client-side routes like /dashboard to the SPA instead of returning 404. The final image has no Node at all.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-a-Dockerfile tasks