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.
More on Write-a-Dockerfile tasks
Write a production Dockerfile for a Node.js API using a multi-stage build.IntermediateWrite a Dockerfile for a Python Flask app that runs with gunicorn as a non-root user.BeginnerWrite a Dockerfile for a Go service that produces a tiny image (under 20 MB).IntermediateWrite a multi-stage Dockerfile for a Java Spring Boot app built with Maven.IntermediateThis Dockerfile rebuilds dependencies on every code change. Fix it.BeginnerWrite a `.dockerignore` for a typical Node or Python project and explain why it matters.Beginner