Docker is a technology that helps to speed up the development and deployment processes. If you’re working with microservices, Docker makes it much easier to link together small, independent services. It also helps to eliminate environment-specific bugs since you can replicate your production environment locally.

This tutorial demonstrates how to Dockerize a React app using the Create React App generator. We’ll specifically focus on setting up a development environment with code hot-reloading.

docker


We will be using:

  • Docker v17.09.0-ce
  • Create React App v1.4.3
  • Node v9.2

Contents

Project Setup

Install Create React App:

$ npm install -g create-react-app@1.4.3

Generate a new app:

$ create-react-app sample-app
$ cd sample-app

Docker

Add a Dockerfile to the project root:

# base image
FROM node:9.2

# set working directory
RUN mkdir /usr/src/app
WORKDIR /usr/src/app

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

# install and cache app dependencies
ADD package.json /usr/src/app/package.json
RUN npm install --silent
RUN npm install react-scripts@1.0.17 -g --silent

# start app
CMD ["npm", "start"]

Silencing the NPM output via --silent is a personal choice. It’s often frowned upon, though, since it can swallow errors. Keep this in mind so you don’t waste time debugging.

Add a .dockerignore:

node_modules

So, this will greatly speed up the Docker build process as our local dependencies will not be sent to the Docker daemon.

Build and tag the Docker image:

$ docker build -t sample-app .

Then, spin up the container once the build is done:

$ docker run -it -v ${PWD}:/usr/src/app -p 3000:3000 --rm sample-app

Open your browser to http://localhost:3000/ and you should see the app. Try making a change to the App component within your code editor. You should see the app hot-reload.

Want to use Docker Compose? Add a docker-compose.yml file to the project root:

version: '3.3'

services:

  sample-app:
    container_name: sample-app
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - '.:/usr/src/app'
    ports:
      - '3000:3000'
    environment:
      - NODE_ENV=development

Build the image and fire up the container:

$ docker-compose up -d --build

Ensure the app is running in the browser and test hot-reloading again. Bring down the containers before moving on:

$ docker-compose stop

Docker Machine

To get hot-reloading to work with Docker Machine and VirtualBox you’ll need to enable a polling mechanism via chokidar (which wraps fs.watch, fs.watchFile, and fsevents).

Create a new Machine:

$ docker-machine create -d virtualbox sample
$ docker-machine env sample
$ eval $(docker-machine env sample)

Grab the IP address:

$ docker-machine ip sample

Then, build the images and run the containers:

$ docker build -t sample-app .
$ docker run -it -v ${PWD}:/usr/src/app -p 3000:3000 --rm sample-app

Test the app again in the browser at http://DOCKER_MACHINE_IP:3000/. Also, confirm that auto reload is not working. You can try with Docker Compose as well, but the results will be the same.

To get hot-reload working, we need to add an environment variable:

$ docker run -it -v ${PWD}:/usr/src/app -p 3000:3000 \
  -e CHOKIDAR_USEPOLLING=true --rm sample-app

Test it out again. You could add the variable to a .env file, however you don’t need it for a production build.

Updated docker-compose.yml file:

version: '3.3'

services:

  sample-app:
    container_name: sample-app
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - '.:/usr/src/app'
    ports:
      - '3000:3000'
    environment:
      - NODE_ENV=development
      - CHOKIDAR_USEPOLLING=true

Want to destroy the Machine?

$ eval $(docker-machine env -u)
$ docker-machine rm sample

Next Steps

With that, you should now be able to add React to a larger Docker-powered project. If you’d like to learn more about working with React and Docker along with building and testing microservices, check out Microservices with Docker, Flask, and React.