Dockerizing A React Application (with docker and nginx)

Containerization on the front-end with React and docker in development and production, and deployment of a React application with docker and nginx

Macharia Muguku
The Startup
8 min readOct 15, 2019

--

Deploy react app with docker and nginx. Image rights https://www.hkinfosoft.com

TL;DR:

  1. The github repo -> https://github.com/machariamuguku/Docker-React-Nginx
  2. Steps:

a) Create a react app

b) Install docker and docker compose

c) Create a dockerfile (with a node image for building and an nginx image for deployment)

d) Create docker compose file

e) deploy the container

The Analogy:

Imagine you want to import a laptop¹ from china. You order the laptop online, you are assigned an order tracking number, they package it inside a carton, label the carton with the order number, write it’s details in a catalog², an then pack it alongside other ordered items inside a shipping container. The container is then loaded on a ship containing other containers and then delivered to your country’s port by the ships captain. You then instruct your clearing agent³ to get you⁴ the product labeled with your order tracking number. They read the product catalog and get you your laptop.

The explanation:

  1. The laptop is the software you want to deploy. In this case it’s the react front-end
  2. The catalog is the dockerfile. It contains the items arrangement inside the container and instructions for the clearing agent on how to distribute them. In a dockerfile the instructions are about how to build and deploy the application.
  3. The clearing agent is the docker-compose file. They read the instructions from the catalog (dockerfile) and execute them (deliver)
  4. You are the developer. You issue commands to the clearing agent (docker compose) telling them to read the catalog (dockerfile) and get you your product (deploy/build). In some cases you can write a script to run these commands on your behalf.
  5. The carton is the base image the docker image is build on
  6. An order tracking number is the containers name. It is used by the catalog (docker compose) to identify the appropriate carton (docker image) to deliver (deploy/build)
  7. The shipping container is the whole docker volume which may contain other docker containers (services?) inside.
  8. The ship is a container manager (eg. kubernetes or docker swarm)
  9. The other containers are other docker images inside the ship (container manager)
  10. The ships captain is nginx. They direct the containers (serve) to the correct delivery location (users)

Create a react project:

0. install nodejs and yarn package manager

  1. install the create react app cli globally

2. Create a new react project called react-docker-project

3. Start the project

4. You’ll land on the default Create react app (which to my surprise changed the spinning svg logo btw 😊)

create react app landing page

Setup Docker for development:

0. install docker and docker-compose

a) install docker. Use this digital ocean tutorial to install for ubuntu.

b) install docker compose. Use the release page on github

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose

  1. Open the react app in an editor. For example vscode

2. Create a dockerfile

A dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.

3. Write the following commands inside the dockerfile. The lines starting with ‘#’ are comments. N/b docker file commands are run sequentially.

4. To spin up the container you can use docker run which you can read about here -> https://docs.docker.com/engine/reference/commandline/run/ or you can use the handy docker-compose as we will

a) create a docker-compose file

b) Write the following commands inside the docker-compose file.

N/b: this file is indentation sensitive. Like a certain weird language we all know (I’m looking at you python 👀). Get the exact indentation from the github repo on the TL;DR above

c) Fire up the container. The up flag starts the container, the — build flag builds the container. N/b: you need to stop anything else running on port 3000

d) To stop the container run . The down flag stops and removes stopped containers and delete any associated network. The — rmi flag removes the images

Setup Docker for production:

1. Create production dockerfile

2. Write the following commands inside the dockerfile-prod.

3. For those using react router create an nginx folder with an nginx file inside at the root of the project like so

4. Write the following nginx configurations inside the nginx.conf file

5. a) create a docker-compose-prod file

6. Write the following commands inside the docker-compose-prod file

7. Fire up the container. The -f flag let’s you pick a specific docker-compose file to run, the up flag starts the container, the -d flag runs the container in detach mode (no interactive cli) and the — build flag rebuilds the container. N/b: you need to stop anything else running on port 3000

8. To stop the container run the following commands. The down flag stops and removes stopped containers and delete any associated network. The — rmi flag removes the images locally

Advance Homework:

  1. write a script to run and stop the containers
  2. Do load balancing on the containers and nginx
  3. Sell all your earthly belonging and donate to the poor

--

--

Macharia Muguku
The Startup

A student of the world ; My brain has too many tabs open ; 🇰🇪; www.muguku.co.ke