How to Run ReactJs Application in a Docker Container?

by Aug 9, 2022IoT Continuous Integration, React JS

If you have developed a ReactJs application and wish to deploy it on a docker container, this tutorial is for you. “It worked on my machine!!!” is a classic dialogue from every developer who gets frustrated when his application doesn’t work on a client’s machine. In this case, docker is the possible solution to run all your applications or services in a container. It doesn’t matter which machine you are working on.

Docker for beginners tutorial: Read this article to get a basic idea of what docker is and how to install docker on your machine.

When you deploy an application on a machine, you need to install all the dependencies such as Node and MySQL. But with docker, you need to mention the list of all the dependencies in a docker file. We’ll also discuss the contents in a docker file later in this tutorial.

Docker Image Vs. Docker Container

Docker will build a unique image for your application from all your dependencies listed in your docker file. You can use this docker image specified to that application and share them through public or private repos. Also, you can re-use them in various machines.

A docker container is an instance of a docker image. Every container is identified with a unique ID. Containers are like lightweight Virtual Machines. You can run multiple images in the same container and the same image in multiple containers and take advantage of scaling problems. There are seven stages for a container: create, restart, run, remove, pause, exit, and dead. Also, containers can be run on a virtual machine.

Create a ReactJs Application

I have created a ReactJs application using React router dom. Click here for the tutorial. We will try to deploy this ReactJs application on the container.

If you are new to ReactJs, click here to learn the basics of ReactJs and develop a simple ReactJs application.

Download this GitHub repository for the React and Docker files.

Check your Docker Version

Let’s say you have your ReactJs application ready, and before proceeding any further, check your docker version on your machine by running the command,

docker -v

How to Run a Docker Container?

This tutorial’s goal is to run these containers on the docker engine having applications inside them. It is simple if you follow these steps:

  1. Go to your application’s main directory, and create a docker file. (say “dockerfile”)
  2. Then, you have to create another file for docker-compose; let’s name it as “docker-compose.yml.”
  3. Build the docker image using all the dependencies needed for the application.
  4. Then run the docker container having the docker image of your application and all its dependencies.

Dockerizing a React Application

Let us discuss briefly the files used to create and build the docker image.

  1. Dockerfile: A Dockerfile consists of a set of instructions that is used to build an image. You list all the dependencies in this list, for instance for a React project, Node is the most important dependency.
  2. docker-compose.yml: Compose is used to run a container for the docker application. You create a YAML file and configure your application.

1. Creating a Docker file

Create a file called “dockerfile” in your main directory. Add the following lines to your file.

FROM node:alpine

WORKDIR /app
# install app dependencies
COPY package.json /app

RUN npm install

# add app
COPY . /app
EXPOSE 3000

# start app
CMD ["npm", "start"]
  • In the first line, FROM node:alpine, we give an instruction to take Node and specify the Linux distribution as Alpine. The Alpine Linux is smaller than most of the distribution images. Alpine Linux is a security-oriented, lightweight Linux distribution based on musl libc and Busybox. A container requires no more than 8 MB and a minimal installation to disk requires around 130 MB of storage. Not only do you get a fully-fledged Linux environment but a large selection of packages from the repository.
  • WORKDIR is the working directory, we specify docker to create a folder called app, and build an image in the app folder
  • COPY tends to copy all the packages needed for the application from the local file system, you can also copy package-lock.json if you want
  • EXPOSE tends to define a port to host the application. This is optional, you can also use -p in the command to define the port
  • CMD is the command to execute the container by default

2. Creating the docker-compose.yml

Create a file called “docker-compose.yml” in your main directory. Add the following lines to your file.

version: "2.6.1"
services:
 client:
   stdin_open: true
   build:
     context: .
     dockerfile: Dockerfile
   ports:
     - "3000:3000"
   volumes:
     - "./:/app"
     - "./:/node_modules"

This is a simple docker-compose YML file that chains up all the basic commands and the services altogether. We will create a service called “client” and run some basic commands. We will define port 3000, and mention the volumes as well to define the root of the project on the container side as well. You can also create a .dockerignore file, but that is not necessary.

Build and Run the container

After writing these two files, now we can build a docker image. Run the following command,

docker-compose build

If you do not get any error, you will find the following image ID in your execution. That means, it created an image.

After that, run the command on your docker-compose yml location,

docker-compose up -d

Successfully, you have created a docker container, besides that, you are also running the docker image in the docker container.

Testing your Container

You can check your ReactJs application on your local host port 3000, http://localhost:3000/ on your web browser. If the web application can be started successfully, the application will be shown on the browser.

Run the command, docker ps to check the containers running.

docker ps

Some of the alternative methods are, that you can just build an image without a docker-compose and run the image in a detached mode in a container.

Building Docker Image

docker build -t <username>/reactapp .

Navigate to your dockerfile directory, and run the command to build the docker image. -t helps to tag your docker image so that later you can find your image.

After the successful build, run the command to check the docker images in your file system.

docker images

Run the Docker Image

Running your image -d runs the container in independent mode, exiting the container running in the background. The -p flag turns a public port into a private port inside the container. Run the image you previously built:

docker run -p 3000:3000 -d <username>/docker-react-sample

Docker Desktop

You can download the docker desktop and view the containers and images instantly over there.

Inside the Docker Desktop, you may find the container list and the image list which you are running on docker. Also, you can open your web browser directly from the image you have built.

Happy learning!

Creating a multiplication Skill in Alexa using python

Written By Monisha Macharla

Hi, I'm Monisha. I am a tech blogger and a hobbyist. I am eager to learn and explore tech related stuff! also, I wanted to deliver you the same as much as the simpler way with more informative content. I generally appreciate learning by doing, rather than only learning. Thank you for reading my blog! Happy learning!

RELATED POSTS

Create a Simple ReactJs Application – Part 1

Create a Simple ReactJs Application – Part 1

ReactJs is one of the famous front-end open-source libraries of JavaScript developed by Facebook. It aims to allow developers to quickly create fast user interfaces for websites and applications. In this blog, we will create a simple and basic ReactJs...

Create a Simple ReactJs Application – Part 2

Create a Simple ReactJs Application – Part 2

In the tutorial's last part, we discussed about ReactJs and how to run a simple react app on a web browser. In this part, we will dig more into some interesting stuff, such as creating a home, about us, and contact pages. Click here for the first part - Create a...

Comparison Between MongoDB and MySQL

Comparison Between MongoDB and MySQL

In this article, we are going to discuss comparison between MongoDB and MySQL. World is experiencing fourth industrial revolution. These revolution comprised of Artificial Intelligence (AI), Block chain, Cloud Computing and Internet of Thing (IoT) technologies. Data...

Plotly R vs Python Dash: Best of the bunch

Plotly R vs Python Dash: Best of the bunch

Introduction Plotly R vs Python Dash both are managed and developed by Plotly for data visualization. But there are some differences in the way you use them. The R graphing library from Plotly allows users to create interactive, publication-quality graphs. Line plots,...

VIDEOS – FOLLOW US ON YOUTUBE

EXPLORE OUR IOT PROJECTS

IoT Smart Gardening System – ESP8266, MQTT, Adafruit IO

Gardening is always a very calming pastime. However, our gardens' plants may not always receive the care they require due to our active lifestyles. What if we could remotely keep an eye on their health and provide them with the attention they require? In this article,...

How to Simulate IoT projects using Cisco Packet Tracer

In this tutorial, let's learn how to simulate the IoT project using the Cisco packet tracer. As an example, we shall build a simple Home Automation project to control and monitor devices. Introduction Firstly, let's quickly look at the overview of the software. Packet...

All you need to know about integrating NodeMCU with Ubidots over MQTT

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

All you need to know about integrating NodeMCU with Ubidots over Https

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

How to design a Wireless Blind Stick using nRF24L01 Module?

Introduction Let's learn to design a low-cost wireless blind stick using the nRF24L01 transceiver module. So the complete project is divided into the transmitter part and receiver part. Thus, the Transmitter part consists of an Arduino Nano microcontroller, ultrasonic...

Sending Temperature data to ThingSpeak Cloud and Visualize

In this article, we are going to learn “How to send temperature data to ThingSpeak Cloud?”. We can then visualize the temperature data uploaded to ThingSpeak Cloud anywhere in the world. But "What is ThingSpeak?” ThingSpeak is an open-source IoT platform that allows...

Amaze your friend with latest tricks of Raspberry Pi and Firebase

Introduction to our Raspberry Pi and Firebase trick Let me introduce you to the latest trick of Raspberry Pi and Firebase we'll be using to fool them. It begins with a small circuit to connect a temperature sensor and an Infrared sensor with Raspberry Pi. The circuit...

How to implement Machine Learning on IoT based Data?

Introduction The industrial scope for the convergence of the Internet of Things(IoT) and Machine learning(ML) is wide and informative. IoT renders an enormous amount of data from various sensors. On the other hand, ML opens up insight hidden in the acquired data....

Smart Display Board based on IoT and Google Firebase

Introduction In this tutorial, we are going to build a Smart Display Board based on IoT and Google Firebase by using NodeMCU8266 (or you can even use NodeMCU32) and LCD. Generally, in shops, hotels, offices, railway stations, notice/ display boards are used. They are...

Smart Gardening System – GO GREEN Project

Automation of farm activities can transform agricultural domain from being manual into a dynamic field to yield higher production with less human intervention. The project Green is developed to manage farms using modern information and communication technologies....