DEV Community

Baha chammakhi
Baha chammakhi

Posted on

Dockerizing a React App with Nginx, using multi-stage builds

React-Docker-nginx
Docker is a containerization tool used to speed up the development and deployment processes, It's the most popular solution for containerization.
Containers allow us to run and develop an application in the same environment, regardless of what machine you're on.
 - -
Docker-compose is a tool for defining and running multi-container Docker applications.
 - -
Nginx is a web server we gonna use it to serve static content, it can be used as a reverse proxy, load balancer.
 - -
React is an open-source, front end, JavaScript library for building user interfaces or user interface components.
 - -
This tutorial demonstrates how to Dockerize a React app with Nginx using multi-stage builds. We'll specifically focus on configuring a production-ready image using multistage builds.

For those who only want to read code you can find the GitHub link below:

bahachammakhi/docker-react-nginx-blog

Creating A React Project:

We will use Create react app to generate our react project.

  • Open your terminal in a specific location and run this command.
npx create-react-app react-docker
Enter fullscreen mode Exit fullscreen mode

CRA2

  • Enter into your project directory:
cd react-docker
Enter fullscreen mode Exit fullscreen mode

CRA3
 - -

Docker files:

Create Dockerfile and docker-compose.yml

mkdir nginx
touch Dockerfile docker-compose.yml nginx/nginx.conf
Enter fullscreen mode Exit fullscreen mode

1

Open Dockerfile

# build environment
FROM node:13.12.0-alpine as build
WORKDIR /app
COPY . .
RUN yarn
RUN yarn build
# production environment
FROM nginx:stable-alpine
COPY - from=build /app/build /usr/share/nginx/html
COPY - from=build /app/nginx/nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Enter fullscreen mode Exit fullscreen mode

dockerfile

What's happening here?

  1. We're telling Docker to grab a copy of Node, specify its Linux distribution as Alpine and name it to build. Why Alpine? Alpine Linux is much smaller than most distribution base images (~5MB), and thus leads to much slimmer images in general.
  2. Setting our working directory to app
  3. Copying project to our directory
  4. Running yarn to install packages
  5. Running build script to generate build files
  6. Telling docker to grap nginx-alpine image
  7. Copying build files
  8. Copying nginx configuration files to replace the default configuration
  9. This line is just for documentation that our application will work on port 80
  10. Running nginx

 - -

Open nginx.conf

server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
}
Enter fullscreen mode Exit fullscreen mode

We are just mentioning the position of our application static files to let Nginx consume them whenever someone sends a request to port 80.
nginx-conf

Open docker-compose.yml

version: "2"
services:
nginx-react:
container_name: ngixreactapp
build:
context: .
dockerfile: Dockerfile
ports:
- 80:80
environment:
NODE_ENV: production
Enter fullscreen mode Exit fullscreen mode

We are giving our app a name, mentioning the dockerfile to use, mapping port 80 to the application port 80, adding some environment variables.
docker-compose

Run our container

docker-compose up
Enter fullscreen mode Exit fullscreen mode

Run container in detached mode

docker-compose -d up
Enter fullscreen mode Exit fullscreen mode

docker-compose-up
If you are using linux you need to use sudo on every docker command you use!
now open http://localhost/ and you will see this:
Untitled
 - -

Top comments (3)

Collapse
 
sushantkumar29 profile image
Sushant Kumar • Edited

It is not working
Gives the following error

nginx: [emerg] "daemon" directive is duplicate in /etc/nginx/nginx.conf:85

Collapse
 
jood80 profile image
Jood

have you solved it?

Collapse
 
bendev12 profile image
Onayngo Benard

I seem to be having issues with serving tailwindcss through nginx. Are there any resources you'd recommend?