Dockerfiles for React.js apps


Develpment

Dockerfile.dev

FROM node:alpine

RUN apk update && apk upgrade && \
    apk add --no-cache bash git vim

WORKDIR '/project'

COPY ./package*.json .
RUN npm install

COPY . .

CMD ["npm", "run", "start"]

$ docker build -t marley/docker-react -f Dockerfile.dev .

$ docker run -p 3000:3000 marley/docker-react

or

$ docker run -p 3000:3000 -v /project/node_modules -v $(pwd):/project marley/docker-react

or

$ docker run -it -p 3000:3000 -v /project/node_modules -v $(pwd):/project marley/docker-react bash


Tests

$ docker run -it <image_id> npm run test


Production

Dockerfile

FROM node:alpine as builder
WORKDIR '/project'

COPY ./package.json .
RUN npm install
COPY . .
RUN npm run build

FROM nginx
COPY --from=builder /project/build /usr/share/nginx/html
$ docker build .
$ docker run -p 8080:80 <image_id>


From video course

[Stephen Grider] Docker and Kubernetes: The Complete Guide [2018, ENG]