Skip to content

Dockerizing React

Posted on:September 21, 2020 at 07:53 PM

An easy way to dockerize your React (create-react-app or a react app created with VITE) would be like this:

# pull official base image, check which is the latest node version
FROM node:13.12.0-alpine

# set working directory
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install app dependencies
COPY package.json ./
COPY package-lock.json ./
RUN npm install
RUN npm install react-scripts@3.4.1 -g

# add app
COPY . ./

# start app
CMD ["npm", "start"]
node_modules
build
.dockerignore
Dockerfile

This will speed up the Docker build process as our local dependencies inside the “node_modules” directory will not be sent to the Docker daemon.

Then start the contianer when the build is done:

$ docker run \
    -it \
    --rm \
    -v ${PWD}:/app \
    -v /app/node_modules \
    -p 3001:3000 \
    -e CHOKIDAR_USEPOLLING=true \
    dockerized:react

So whats happening here ?

{PWD} may not work on Windows. Stackoverflow question for more info

Visit http://localhost:3001 and check if everything is working!