You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# syntax=docker/dockerfile:1.6.0# 1. For build React appFROM node:20.11.1-alpine3.19 AS development
# Set working directoryWORKDIR /app
# Copy dependency filesCOPY package.json /app/package.json
COPY package-lock.json /app/package-lock.json
# ci = clean-install# Same as npm installRUN npm ci
COPY . /app
ENV CI=true
ENV PORT=3000
CMD [ "npm", "start" ]
Run the following command to build Docker Image and run React app
# Build Docker Image name react-nginx
docker build -t react-dev .# See newly build Docker Image
docker images
# Run react with nginx
docker run -d --name react-dev -v $(pwd):/app -p 8080:3000 react-dev
# See running containers
docker ps -a
# Try Web Preview on port 8080 with / as path
Try to change line 14 in ~/workshop/src/react-nginx/src/App.js to https://www.opsta.co.th
Refresh web preview in browser to see the change
Create Dockerfile for React to run in production with Nginx
Append the following content to Dockerfile
# 2.1 For production buildFROM development AS build
# npm run build to create an optimized production buildRUN npm run build
# 2.2 For Nginx setup as resultFROM nginx:alpine
# Copy config nginxCOPY --from=build /app/.nginx/nginx.conf /etc/nginx/conf.d/default.conf
WORKDIR /usr/share/nginx/html
# Remove default nginx static assetsRUN rm -rf ./*
# Copy static assets from builder stageCOPY --from=build /app/build .
# Containers run nginx with global directives and daemon offENTRYPOINT ["nginx", "-g", "daemon off;"]
Run the following commands to build and run React with Nginx container
# Build Docker Image name react-nginx
docker build -t react-nginx .# See newly build Docker Image
docker images
# Run react with nginx
docker run -d --name react-nginx -p 8081:80 react-nginx
# See running containers
docker ps -a
# Try Web Preview onport 8081