DEV Community

Baha chammakhi
Baha chammakhi

Posted on

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

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:


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


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

 - -

Docker files:

Create Dockerfile and docker-compose.yml

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


Open Dockerfile

# build environment
FROM node:13.12.0-alpine as build
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
CMD ["nginx", "-g", "daemon off;"]
Enter fullscreen mode Exit fullscreen mode


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.

Open docker-compose.yml

version: "2"
container_name: ngixreactapp
context: .
dockerfile: Dockerfile
- 80:80
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.

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

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:
 - -

Top comments (3)

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

jood80 profile image

have you solved it?

bendev12 profile image
Onayngo Benard

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