Dockerizing A React Application Using Multi Stage Dockerfile
Build a React app with Node and serve the production files with Nginx.
React applications are usually built into static files.
During development, you use Node and a dev server. For production, you normally build optimized files and serve them using a web server such as Nginx.
This makes React a great example for multi-stage Dockerfiles.
Multi-stage Dockerfile
FROM node:20-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM nginx:alpine
COPY --from=build /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]For Create React App, the build folder may be /app/build instead of /app/dist.
Stage 1: build React
The first stage uses Node:
FROM node:20-alpine AS buildIt installs dependencies and creates production files:
RUN npm run buildStage 2: serve with Nginx
The second stage uses Nginx:
FROM nginx:alpineOnly the final static files are copied:
COPY --from=build /app/dist /usr/share/nginx/htmlThe final image does not need Node, source files, or development dependencies.
Build and run
docker build -t react-app:1.0 .
docker run -d -p 8080:80 --name react-web react-app:1.0Open:
http://localhost:8080Common folder difference
Different React tools use different output folders.
| Tool | Common build output |
|---|---|
| Vite | dist |
| Create React App | build |
| Next.js static export | out |
Use the folder your project actually creates.
The Node stage builds the React app. The Nginx stage serves the final files. That is the clean production pattern.
Written By: Shiva Srivastava
How is this guide?
Last updated on
