Panmure  Anho

Panmure Anho

1579897834

How to Build React Application using Docker vs NGINX

In this post, we will see the details and implementation of Build React Application using Docker vs NGINX. We will go through step by step with an example.

Example Project

This is a simple project which demonstrates serving static React application with NGINX and Docker. We have a simple app with a header, footer and with a message.

Here is the example project where you can clone and run on your machine

// clone the project
git clone https://github.com/bbachi/react-nginx-docker

// install and start the dependencies
npm install
npm start

// build the docker image
docker build -t react-ui .
// run the app
docker run -d --name reactui -p 80:80 react-ui

This is a simple React app that has the header, footer and some message on the dashboard. Here are the Header.js, Footer.js and, App.js files

import React from 'react';

const Header = () => {

    return (
        <div class="header">
            Simple React App
        </div>
    )
    
};

export default Header

Header.js

import React from 'react';

const Footer = () => {

    return (
        <div class="footer">
            2020
        </div>
    )
    
};

export default Footer

Footer.js

import React from 'react';
import Header from './header/header'
import Footer from './footer/footer'
import './App.css';

function App() {
  return (
    <div className="App">
      <Header />
      <div class="dashboard">
        <h1>Simple React App served by NGINX and Docker</h1>
      </div>
      <Footer />
    </div>
  );
}

export default App;

App.js

Just Enough NGINX For This Project

We are not going through everything about NGINX here and we just go through just enough for this project. If you are already familiar with this stuff, you can skip over to the next section.

NGINX processes are divided into one master process and several worker processes. The master process takes care of evaluating configuration and maintaining worker processes and the worker processes take care of actual requests. We can define the number of worker processes in the configuration file which can be placed in the directory /usr/local/etc/nginx, /etc/nginx or /usr/local/nginx/conf.

The configuration file consists of directives that form the modules or contexts. There are two kinds of directives: simple directives and block directives. A simple directive has names and parameters separated by a space and ends with a semicolon like this listen 80; . A block directive is the same but has additional information and surrounded by braces like this { listen 80; root /usr/share/nginx/html; }.

Let’s understand the NGINX configuration file that we used in this project. Below is the nginx.conf file which is located under folder .nginx at root location of the project.

Everything is a context in the configuration file. We have a hierarchical context that starts with the main context. For example, worker_processes and events are defined in the main context and another context starts with http. We have another context inside http called server which listens on port 80 and serving static assets from the root location /usr/share/nginx/html.

We can have multiple declarations of the server inside the http context and we can have multiple declarations of location inside the server context.

worker_processes 4;

events { worker_connections 1024; }

http {
    server {
        listen 80;
        root  /usr/share/nginx/html;
        include /etc/nginx/mime.types;

        location /appui {
            try_files $uri /index.html;
        }
    }
}

nginx.conf

Implementation

We use Docker as a container runtime for this project. We are using multi-stage builds to build the Docker image. Here is the Dockerfile for the project.

# stage1 as builder
FROM node:10-alpine as builder

# copy the package.json to install dependencies
COPY package.json package-lock.json ./

# Install the dependencies and make the folder
RUN npm install && mkdir /react-ui && mv ./node_modules ./react-ui

WORKDIR /react-ui

COPY . .

# Build the project and copy the files
RUN npm run build


FROM nginx:alpine

#!/bin/sh

COPY ./.nginx/nginx.conf /etc/nginx/nginx.conf

## Remove default nginx index page
RUN rm -rf /usr/share/nginx/html/*

# Copy from the stahg 1
COPY --from=builder /react-ui/build /usr/share/nginx/html

EXPOSE 3000 80

ENTRYPOINT ["nginx", "-g", "daemon off;"]

Dockerfile*\

Stage 1

We are using node:10-alpine as a base image for the stage1 and copying package.json to install all the dependencies. We then copy the remaining project later, in that way we can skip the installing dependencies every time there is a change in the files. Docker uses a cache to build the image from existing layers if there is no change.

We build the project with the react-scripts and all the built static files are placed in the /build folder.

Stage 2

Stage 2 starts with the base image nginx:alpine and copy the nginx.conf file, remove the index file from the root location, and finally, copy all the files from stage 1 to the root location where it can serve the content from.

Build the Image and Run the Project

Let’s build the project with this command docker build -t react-ui. and you can run the project with this command docker run -d --name reactui -p 80:80 react-ui . You can run the app on [http://localhost:80](http://localhost:80/appui)

Project running on localhost

Important Things to notice

The container port and nginx listen port should be the same which is 80 otherwise you would get ERR_EMPTY_RESPONSE when you run the project.

// container port
docker run -d --name react-ui -p 80:80 reactui

// nginx conf
http {   
   server {
      listen 80;
   }
}

We should include this directive in the nginx.conf file otherwise all the styles are rendered as plain text in the browser.

include /etc/nginx/mime.types;

Exec Into the Running Container

While the container is in running state we can exec into it and see the contents of the file system.

docker exec -it reactui /bin/sh

We can actually see all the contents under /usr/share/nginx/html

File system inside docker

Summary

  • NGINX can be used as a web server or reverse proxy to serve the static content.
  • All the NGINX configuration can be placed in this file nginx.conf .
  • We need to build the react app and place all the static files in the root location of the NGINX to serve the web.
  • Docker is used as the container runtime.
  • We use multi-stage builds to reduce the final image size and remove unnecessary files from the production environment.
  • Docker image can be built with docker build -t react-ui .
  • Run the container with this command docker run -d --name reactui -p 80:80 react-ui.
  • It’s very important to match ports while running the container and the listen port in nginx.conf file. Otherwise, you would get an ERR_EMPTY_RESPONSE error**.**
  • You can exec into the container to explore the file system with this command docker exec -it reactui /bin/sh.

Conclusion

NGINX is a high-performance web server that serves content and application delivery, improves security, facilitates availability and scalability for the web apps. If you want to avoid or there is no need for building UI apps with Java or node js server runtime, you can build the UI app and serve the static files with the NGINX at scale. Thank you for reading!

#react #docker #javascript #nginx #development

What is GEEK

Buddha Community

How to Build React Application using Docker vs NGINX
Autumn  Blick

Autumn Blick

1598839687

How native is React Native? | React Native vs Native App Development

If you are undertaking a mobile app development for your start-up or enterprise, you are likely wondering whether to use React Native. As a popular development framework, React Native helps you to develop near-native mobile apps. However, you are probably also wondering how close you can get to a native app by using React Native. How native is React Native?

In the article, we discuss the similarities between native mobile development and development using React Native. We also touch upon where they differ and how to bridge the gaps. Read on.

A brief introduction to React Native

Let’s briefly set the context first. We will briefly touch upon what React Native is and how it differs from earlier hybrid frameworks.

React Native is a popular JavaScript framework that Facebook has created. You can use this open-source framework to code natively rendering Android and iOS mobile apps. You can use it to develop web apps too.

Facebook has developed React Native based on React, its JavaScript library. The first release of React Native came in March 2015. At the time of writing this article, the latest stable release of React Native is 0.62.0, and it was released in March 2020.

Although relatively new, React Native has acquired a high degree of popularity. The “Stack Overflow Developer Survey 2019” report identifies it as the 8th most loved framework. Facebook, Walmart, and Bloomberg are some of the top companies that use React Native.

The popularity of React Native comes from its advantages. Some of its advantages are as follows:

  • Performance: It delivers optimal performance.
  • Cross-platform development: You can develop both Android and iOS apps with it. The reuse of code expedites development and reduces costs.
  • UI design: React Native enables you to design simple and responsive UI for your mobile app.
  • 3rd party plugins: This framework supports 3rd party plugins.
  • Developer community: A vibrant community of developers support React Native.

Why React Native is fundamentally different from earlier hybrid frameworks

Are you wondering whether React Native is just another of those hybrid frameworks like Ionic or Cordova? It’s not! React Native is fundamentally different from these earlier hybrid frameworks.

React Native is very close to native. Consider the following aspects as described on the React Native website:

  • Access to many native platforms features: The primitives of React Native render to native platform UI. This means that your React Native app will use many native platform APIs as native apps would do.
  • Near-native user experience: React Native provides several native components, and these are platform agnostic.
  • The ease of accessing native APIs: React Native uses a declarative UI paradigm. This enables React Native to interact easily with native platform APIs since React Native wraps existing native code.

Due to these factors, React Native offers many more advantages compared to those earlier hybrid frameworks. We now review them.

#android app #frontend #ios app #mobile app development #benefits of react native #is react native good for mobile app development #native vs #pros and cons of react native #react mobile development #react native development #react native experience #react native framework #react native ios vs android #react native pros and cons #react native vs android #react native vs native #react native vs native performance #react vs native #why react native #why use react native

Panmure  Anho

Panmure Anho

1579897834

How to Build React Application using Docker vs NGINX

In this post, we will see the details and implementation of Build React Application using Docker vs NGINX. We will go through step by step with an example.

Example Project

This is a simple project which demonstrates serving static React application with NGINX and Docker. We have a simple app with a header, footer and with a message.

Here is the example project where you can clone and run on your machine

// clone the project
git clone https://github.com/bbachi/react-nginx-docker

// install and start the dependencies
npm install
npm start

// build the docker image
docker build -t react-ui .
// run the app
docker run -d --name reactui -p 80:80 react-ui

This is a simple React app that has the header, footer and some message on the dashboard. Here are the Header.js, Footer.js and, App.js files

import React from 'react';

const Header = () => {

    return (
        <div class="header">
            Simple React App
        </div>
    )
    
};

export default Header

Header.js

import React from 'react';

const Footer = () => {

    return (
        <div class="footer">
            2020
        </div>
    )
    
};

export default Footer

Footer.js

import React from 'react';
import Header from './header/header'
import Footer from './footer/footer'
import './App.css';

function App() {
  return (
    <div className="App">
      <Header />
      <div class="dashboard">
        <h1>Simple React App served by NGINX and Docker</h1>
      </div>
      <Footer />
    </div>
  );
}

export default App;

App.js

Just Enough NGINX For This Project

We are not going through everything about NGINX here and we just go through just enough for this project. If you are already familiar with this stuff, you can skip over to the next section.

NGINX processes are divided into one master process and several worker processes. The master process takes care of evaluating configuration and maintaining worker processes and the worker processes take care of actual requests. We can define the number of worker processes in the configuration file which can be placed in the directory /usr/local/etc/nginx, /etc/nginx or /usr/local/nginx/conf.

The configuration file consists of directives that form the modules or contexts. There are two kinds of directives: simple directives and block directives. A simple directive has names and parameters separated by a space and ends with a semicolon like this listen 80; . A block directive is the same but has additional information and surrounded by braces like this { listen 80; root /usr/share/nginx/html; }.

Let’s understand the NGINX configuration file that we used in this project. Below is the nginx.conf file which is located under folder .nginx at root location of the project.

Everything is a context in the configuration file. We have a hierarchical context that starts with the main context. For example, worker_processes and events are defined in the main context and another context starts with http. We have another context inside http called server which listens on port 80 and serving static assets from the root location /usr/share/nginx/html.

We can have multiple declarations of the server inside the http context and we can have multiple declarations of location inside the server context.

worker_processes 4;

events { worker_connections 1024; }

http {
    server {
        listen 80;
        root  /usr/share/nginx/html;
        include /etc/nginx/mime.types;

        location /appui {
            try_files $uri /index.html;
        }
    }
}

nginx.conf

Implementation

We use Docker as a container runtime for this project. We are using multi-stage builds to build the Docker image. Here is the Dockerfile for the project.

# stage1 as builder
FROM node:10-alpine as builder

# copy the package.json to install dependencies
COPY package.json package-lock.json ./

# Install the dependencies and make the folder
RUN npm install && mkdir /react-ui && mv ./node_modules ./react-ui

WORKDIR /react-ui

COPY . .

# Build the project and copy the files
RUN npm run build


FROM nginx:alpine

#!/bin/sh

COPY ./.nginx/nginx.conf /etc/nginx/nginx.conf

## Remove default nginx index page
RUN rm -rf /usr/share/nginx/html/*

# Copy from the stahg 1
COPY --from=builder /react-ui/build /usr/share/nginx/html

EXPOSE 3000 80

ENTRYPOINT ["nginx", "-g", "daemon off;"]

Dockerfile*\

Stage 1

We are using node:10-alpine as a base image for the stage1 and copying package.json to install all the dependencies. We then copy the remaining project later, in that way we can skip the installing dependencies every time there is a change in the files. Docker uses a cache to build the image from existing layers if there is no change.

We build the project with the react-scripts and all the built static files are placed in the /build folder.

Stage 2

Stage 2 starts with the base image nginx:alpine and copy the nginx.conf file, remove the index file from the root location, and finally, copy all the files from stage 1 to the root location where it can serve the content from.

Build the Image and Run the Project

Let’s build the project with this command docker build -t react-ui. and you can run the project with this command docker run -d --name reactui -p 80:80 react-ui . You can run the app on [http://localhost:80](http://localhost:80/appui)

Project running on localhost

Important Things to notice

The container port and nginx listen port should be the same which is 80 otherwise you would get ERR_EMPTY_RESPONSE when you run the project.

// container port
docker run -d --name react-ui -p 80:80 reactui

// nginx conf
http {   
   server {
      listen 80;
   }
}

We should include this directive in the nginx.conf file otherwise all the styles are rendered as plain text in the browser.

include /etc/nginx/mime.types;

Exec Into the Running Container

While the container is in running state we can exec into it and see the contents of the file system.

docker exec -it reactui /bin/sh

We can actually see all the contents under /usr/share/nginx/html

File system inside docker

Summary

  • NGINX can be used as a web server or reverse proxy to serve the static content.
  • All the NGINX configuration can be placed in this file nginx.conf .
  • We need to build the react app and place all the static files in the root location of the NGINX to serve the web.
  • Docker is used as the container runtime.
  • We use multi-stage builds to reduce the final image size and remove unnecessary files from the production environment.
  • Docker image can be built with docker build -t react-ui .
  • Run the container with this command docker run -d --name reactui -p 80:80 react-ui.
  • It’s very important to match ports while running the container and the listen port in nginx.conf file. Otherwise, you would get an ERR_EMPTY_RESPONSE error**.**
  • You can exec into the container to explore the file system with this command docker exec -it reactui /bin/sh.

Conclusion

NGINX is a high-performance web server that serves content and application delivery, improves security, facilitates availability and scalability for the web apps. If you want to avoid or there is no need for building UI apps with Java or node js server runtime, you can build the UI app and serve the static files with the NGINX at scale. Thank you for reading!

#react #docker #javascript #nginx #development

Franz  Becker

Franz Becker

1651604400

React Starter Kit: Build Web Apps with React, Relay and GraphQL.

React Starter Kit — "isomorphic" web app boilerplate   

React Starter Kit is an opinionated boilerplate for web development built on top of Node.js, Express, GraphQL and React, containing modern web development tools such as Webpack, Babel and Browsersync. Helping you to stay productive following the best practices. A solid starting point for both professionals and newcomers to the industry.

See getting started guide, demo, docs, roadmap  |  Join #react-starter-kit chat room on Gitter  |  Visit our sponsors:

 

Hiring

Getting Started

Customization

The master branch of React Starter Kit doesn't include a Flux implementation or any other advanced integrations. Nevertheless, we have some integrations available to you in feature branches that you can use either as a reference or merge into your project:

You can see status of most reasonable merge combination as PRs labeled as TRACKING

If you think that any of these features should be on master, or vice versa, some features should removed from the master branch, please let us know. We love your feedback!

Comparison

 

React Starter Kit

React Static Boilerplate

ASP.NET Core Starter Kit

App typeIsomorphic (universal)Single-page applicationSingle-page application
Frontend
LanguageJavaScript (ES2015+, JSX)JavaScript (ES2015+, JSX)JavaScript (ES2015+, JSX)
LibrariesReact, History, Universal RouterReact, History, ReduxReact, History, Redux
RoutesImperative (functional)DeclarativeDeclarative, cross-stack
Backend
LanguageJavaScript (ES2015+, JSX)n/aC#, F#
LibrariesNode.js, Express, Sequelize,
GraphQL
n/aASP.NET Core, EF Core,
ASP.NET Identity
SSRYesn/an/a
Data APIGraphQLn/aWeb API

Backers

♥ React Starter Kit? Help us keep it alive by donating funds to cover project expenses via OpenCollective or Bountysource!

lehneres Tarkan Anlar Morten Olsen Adam David Ernst Zane Hitchcox  

How to Contribute

Anyone and everyone is welcome to contribute to this project. The best way to start is by checking our open issues, submit a new issue or feature request, participate in discussions, upvote or downvote the issues you like or dislike, send pull requests.

Learn More

Related Projects

  • GraphQL Starter Kit — Boilerplate for building data APIs with Node.js, JavaScript (via Babel) and GraphQL
  • Membership Database — SQL schema boilerplate for user accounts, profiles, roles, and auth claims
  • Babel Starter Kit — Boilerplate for authoring JavaScript/React.js libraries

Support

License

Copyright © 2014-present Kriasoft, LLC. This source code is licensed under the MIT license found in the LICENSE.txt file. The documentation to the project is licensed under the CC BY-SA 4.0 license.


Author: kriasoft
Source Code: https://github.com/kriasoft/react-starter-kit
License: MIT License

#graphql #react 

What are hooks in React JS? - INFO AT ONE

In this article, you will learn what are hooks in React JS? and when to use react hooks? React JS is developed by Facebook in the year 2013. There are many students and the new developers who have confusion between react and hooks in react. Well, it is not different, react is a programming language and hooks is a function which is used in react programming language.
Read More:- https://infoatone.com/what-are-hooks-in-react-js/

#react #hooks in react #react hooks example #react js projects for beginners #what are hooks in react js? #when to use react hooks

Rowena  Waters

Rowena Waters

1628924400

Guide to Using React with Docker and NodeJs & Nginx

In this video, learn about: Guide to Using React with Docker and NodeJs & Nginx
#react #docker #nestjs #nginx