Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Vineeth.TR
Vineeth.TR

Posted on

🚀 Dockerizing a React Application with Nginx

When developing React applications, using Docker ensures consistency across different environments. Instead of running React with npm start, we can serve the production build using nginx, improving performance and efficiency.

In this post, we will:

  1. - Create a Dockerfile to build and serve a React application using nginx.
  2. - Write a custom nginx.conf to properly serve the React app.
  3. - Use docker-compose.yml to run the containerized application.

📌 Project Structure

Your project should have the following structure:

/my-react-app ├── src/               # React source files ├── node_modules/      # Installed dependencies ├── public/            # Public assets ├── package.json       # Dependencies and scripts ├── Dockerfile         # Docker configuration ├── docker-compose.yml # Docker Compose setup ├── nginx.conf         # Nginx configuration
Enter fullscreen modeExit fullscreen mode

We will try model

  1. Run React application in dev mode docker
  2. Build & Run React application in prod mode with nginx docker

🌱 Run React application in dev mode docker

Here’s theDockerfile anddocker-compose.yml setup for your React application. This will allow you to run your app in a containerized environment and access it onhttp://localhost:3000.

🛠 Step 1: Create theDockerfile

This Dockerfile will:

  • Use node:23 to build the React app.
  • Use nginx to serve the production-ready app.
# Use Node.js image as the base imageFROMnode:23-alpineASbuild-stage# Set the working directory inside the containerWORKDIR /app# Copy package.json and package-lock.json to the working directoryCOPY package*.json ./# Install dependenciesRUNnpminstall# Copy the entire project to the working directoryCOPY . .# Expose the port React runs onEXPOSE 3000# Start the React applicationCMD ["npm", "start"]
Enter fullscreen modeExit fullscreen mode

⚙️ Step 2: Createdocker-compose.yml

This allows us to run the containerized application easily.

version:"3.8"services:react-app:container_name:react_appbuild:.ports:-"3000:3000"volumes:-.:/app-/app/node_modulesstdin_open:truetty:true
Enter fullscreen modeExit fullscreen mode

🧇 Build & Run React application in prod mode with nginx docker

This setup ensures that our React application runs efficiently in production withnginx.

🛠 Step 1: Create theDockerfile

This Dockerfile will:

  • Use node:23 to build the React app.
  • Use nginx to serve the production-ready app.
  • Copy the built files into the nginx server.
# Use Node.js image to build the React appFROMnode:23-alpineASbuild-stage# Set working directoryWORKDIR /app# Copy package.json and package-lock.jsonCOPY package*.json ./# Install dependenciesRUNnpminstall# Copy the entire projectCOPY . .# Build the React appRUNnpm run build# Use Nginx to serve the build filesFROM nginx:alpine# Copy the built files from the previous stepCOPY --from=build /app/build /usr/share/nginx/html# Copy the custom Nginx configurationCOPY nginx.conf /etc/nginx/conf.d/default.conf# Expose port 3000EXPOSE 3000# Start NginxCMD ["nginx", "-g", "daemon off;"]
Enter fullscreen modeExit fullscreen mode

🔧 Step 2: Createnginx.conf

This file ensures that nginx correctly serves the React app, handling routes properly.

server{listen3000;server_namelocalhost;location/{root/usr/share/nginx/html;indexindex.html;try_files$uri/index.html;}error_page404/index.html;location/api/{proxy_passhttp://backend:5000;#Modifyifyouhaveabackendserviceproxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;}}
Enter fullscreen modeExit fullscreen mode

⚙️ Step 3: Createdocker-compose.yml

This allows us to run the containerized application easily.

version:"3.8"services:react-app:container_name:unified_portal_cbuild:.ports:-"3000:3000"restart:always
Enter fullscreen modeExit fullscreen mode

🚀 Running the Application

Now that everything is set up, follow these steps to run your React app in a Docker container:

1️⃣ Build and start the container

docker-compose up --build
Enter fullscreen modeExit fullscreen mode

2️⃣ Open in your browser

http://localhost:3000
Enter fullscreen modeExit fullscreen mode

3️⃣ Stop the container

docker-compose down
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A resilient and go-curious person who loves getting his hands dirty in web development.
  • Location
    Kerala
  • Work
    Software Engineer at EY
  • Joined

More fromVineeth.TR

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp