Hey, fam! 👋 As junior developers, diving into the world of deployment might seem a bit daunting at first, but fear not! Today, I'm here to guide you through deploying a simple React.js frontend with Docker in a breeze. 🌐🐳
Step 1: Get Your React App Ready
Ensure your React app is ready for the big stage! If you don't have one yet, create a basic React app using create-react-app
. 🚀
npx create-react-app dockerized-react-app
cd my-react-app
Step 2: Dockerizing Your React App
Create a file named Dockerfile
in your project root. This file tells Docker how to build your app.
# Use an official Node.js runtime as a base image
FROM node:14
# Set the working directory
WORKDIR /usr/src/app
# Copy package.json and package-lock.json to the working directory
COPY package*.json ./
# Install app dependencies
RUN yarn install
# Copy the app source code
COPY . .
# Expose the port your app will run on
EXPOSE 3000
# Command to run your app
CMD ["yarn", "start"]
Step 3: Build the Docker Image
Open your terminal, navigate to your project directory, and run:
docker build -t dockerized-react-app
This command builds your Docker image with the name dockerized-react-app
.
Step 4: Run Your Dockerized React App
Once the image is built, it's time to run your Docker container!
docker run -p 8080:3000 dockerized-react-app
Your React app is now running in a Docker container, accessible at http://localhost:8080
in your browser. 🎉
Bonus Tip: Simplify Development with Docker Compose
Create a file named docker-compose.yml
in your project root:
version: '3'
services:
web:
build: .
ports:
- "8080:3000"
Run your app with:
docker-compose up
Voilà! Your React app is now deployed using Docker, and you're ready to share it with the world. 🚀
ReactJS #Docker #WebDevelopment #JuniorDevelopers #TechTips
Feel free to reach out if you have any questions or need further guidance. Happy coding! 💻✨
Top comments (0)