DEV Community

Cover image for Dockerizing React App
Jun Ueno
Jun Ueno

Posted on • Edited on

Dockerizing React App

Written with Cameron Gibson

Versions

Node version: v16.13.0
Docker version: 20.10.12

Link

GitHub Repository
Here’s How to Dockerize React App

Table of Contents

  1. Create React app
  2. Create Dockerfile, image and run the container

1. Create React app

① Create React App following official tutorial
② Run command below

npx create-react-app <app name> # witch is going to be the folder name
cd <app name>
npm start
Enter fullscreen mode Exit fullscreen mode

③ Access to http://localhost:3000
Confirm you can see React app welcome page
localhost:3000_reactapp

④ Stop Running by Ctrl + C

2. Create Dockerfile, image and run the container

① CreateDockerfile in root directory
And copy the code below

# Dockerfile
FROM node:16
WORKDIR /reactapp
ENV PATH /reactapp/node_modules/.bin:$PATH
COPY package.json ./
COPY package-lock.json ./
RUN npm i
COPY . ./
CMD ["npm", "start"]
Enter fullscreen mode Exit fullscreen mode

② Build image

docker build -t <image name> . 
Enter fullscreen mode Exit fullscreen mode

③ Run container

docker run \
    -it \
    --rm \
    -v ${PWD}:/reactapp \
    -v /reactapp/node_modules \
    -p 3001:3000 \
    -e CHOKIDAR_USEPOLLING=true \
    <image name>:latest
Enter fullscreen mode Exit fullscreen mode

④ Access to http://localhost:3001
Confirm you can see React app welcome page
localhost:3001_reactapp

Top comments (0)