To run this : 1- curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh 2- curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash 3- source ~/.bashrc 4- nvm list-remote #install version 16 5- nvm install v16.14.0 #install nvm lts 6- nvm install lts/fermium
7- node -v then ; #For example, you can create a new React application using create-react-app: 8- npx create-react-app my-app #If you have locally installed packages, you can run their executables using npx. For example, if you have eslint installed locally: 9-npx eslint . then; download packages and run to
Local: http://localhost:3000 On Your Network: http://192.168.0.113:3000
1- Problem 1:
- Run a container nginx with name my-nginx and attach a and attach a volume 2 volumes to the container
- Volume for containing static html file
- Volume2 for containing nginx configuration
- Run a container nginx with name my-nginx and attach a and attach a volume 2 volumes to the container
- Volume for containing static html file
- Volume2 for containing nginx configuration Run a new 2 containers with the following:
- Attach the 2 volumes that was attached to the previous container in two different ways (volume mount - bind mount) : Acess the to fis to your out machine
Problem 2:
- Create a dockerfile for nginx image with different html content and different nginx conf that listen to port 8080 instead of port 80 on the container
- Create container from the new \image mkdir -p ~/nginx_custom cd ~/nginx_custom mkdir -p html echo "" > html/index.html Create Nginx Configuration
//Create an Nginx configuration file that listens on port 8080: mkdir -p nginx_conf cat < nginx_conf/default.conf server { listen 8080; server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html;
}
} EOF 4. Create the Dockerfile
Create a Dockerfile that sets up the custom Nginx image cat < Dockerfile FROM nginx:latest COPY html /usr/share/nginx/html COPY nginx_conf/default.conf /etc/nginx/conf.d/default.conf EXPOSE 8080 EOF Build the Docker Image
Build the Docker image with a custom name (e.g., nginx_custom): docker build -t nginx_custom .
Problem 3: : Create a deckerile po containerize the reactapp
- Build the image and test it
- (Bonus) create a dockerfile for the same app in smaller size using multi staging
Build Stage: Uses the Node.js image to build the React app. Runtime Stage: Uses the Nginx image to serve the built React app.
Basic Dockerfile :
FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
RUN npm install -g serve
EXPOSE 5000
CMD ["serve", "-s", "build", "-l", "5000"]
docker build -t my-react-app . docker run -p 5000:5000 my-react-app
Multi-Stage Dockerfile (Optimized for Smaller Size)
FROM node:18 AS build
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM nginx:alpine
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
docker build -t my-react-app:multi-stage .
docker run -p 80:80 my-react-app:multi-stage