Followers

Dockerfile for Reactjs App

  Step 1: Create a ReactJS Application Set Up the React Application: Ensure you have Node.js and npm (Node Package Manager) installed on you...

 

Step 1: Create a ReactJS Application

  1. Set Up the React Application:

    Ensure you have Node.js and npm (Node Package Manager) installed on your machine. You can verify this by running:


    node -v npm -v

    If not installed, you can download and install Node.js from nodejs.org.

  2. Create a New React Application:

    Use the create-react-app tool to set up a new React project. Open a terminal and run:

    npx create-react-app my-react-app

    This will create a new directory named my-react-app with a basic React application template.

  3. Navigate to the Application Directory:

    cd my-react-app
  4. Test the Application:

    To make sure everything is working, you can start the development server:

    npm start

    Open your browser and go to http://localhost:3000 to see the default React application.

Step 2: Create a Dockerfile



  1. Create a Dockerfile:

    In the root of your React application directory (my-react-app), create a file named Dockerfile with the following content:

    # Step 1: Build the React application
    FROM node:18 AS build # Set the working directory WORKDIR /app # Copy package.json and package-lock.json COPY package*.json ./ # Install dependencies RUN npm install # Copy the rest of the application code COPY . . # Build the application RUN npm run build # Step 2: Serve the React application FROM nginx:alpine # Copy the build output to the Nginx server's root directory COPY --from=build /app/build /usr/share/nginx/html # Expose port 80 EXPOSE 80 # Start Nginx server CMD ["nginx", "-g", "daemon off;"]

    This Dockerfile does two main things:

    • Build Stage: Uses a Node.js image to build the React application.
    • Serve Stage: Uses an Nginx image to serve the built React application.

Step 3: Build and Run the Docker Container

  1. Build the Docker Image:

    In the root directory of your project (where the Dockerfile is located), run the following command to build the Docker image:

    docker build -t my-react-app .

    This command builds the Docker image and tags it as my-react-app.

  2. Run the Docker Container:

    After building the image, you can run it in a container with:

    sh
    docker run -p 8080:80 my-react-app

    This command maps port 80 in the container to port 8080 on your host machine.

  3. Access the Application:

    Open your browser and go to http://localhost:8080 to see your React application running inside the Docker container.

COMMENTS

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,git quiz,1,Git Worksheet,1,ITIL,1,ITSM,1,Jira,3,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SDLC Quiz,5,SonarQube,3,Splunk,2,vagrant kubernetes,1,Windows,1,YAML Basics,1,
ltr
item
DevOpsWorld: Dockerfile for Reactjs App
Dockerfile for Reactjs App
DevOpsWorld
https://www.devopsworld.co.in/2024/08/dockerfile-for-reactjs-app.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/2024/08/dockerfile-for-reactjs-app.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content