Simple docker file for a React app

Samad
1 min readDec 22, 2022

--

Here is a sample Dockerfile that you can use to build a Docker image for a React.js application:

FROM node:12.16.3-alpine as build
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . ./
RUN npm run build

FROM nginx:1.19.3-alpine
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

This Dockerfile uses two FROM statements to create a multi-stage build process. The first stage uses the node:12.16.3-alpine image as a base, and installs the required dependencies and builds the React.js application. The second stage uses the nginx:1.19.3-alpine image as a base, and copies the built application from the first stage into the image. It then exposes port 80 and sets the default command to start the nginx web server.

To build the Docker image, you will need to run the docker build command and specify the path to the Dockerfile:

docker build -t my-react-app .

This will create a new Docker image with the name “my-react-app”. You can then run the image using the docker run command:

docker run -p 8080:80 my-react-app

This will start the nginx web server and expose it on port 8080 of the host machine. You can access the React.js application by visiting http://localhost:8080 in a web browser.

--

--