docker · Oct 2024
Docker
Docker simplifies development workflows and ensures that there is consistency across environments. Below is a quick guide for dockerizing a React project, pushing it to Docker Hub, and deploying it…
Docker simplifies development workflows and ensures that there is consistency across environments. Below is a quick guide for dockerizing a React project, pushing it to Docker Hub, and deploying it on a DigitalOcean Droplet.
Steps to Dockerize and Deploy:
-
Create a Docker Account:
- Create an account on Docker Hub and install Docker on your machine.
-
Spin Up a React Project:
- Use
npx create-react-app dockerProjectto create a React project. - Navigate into the project folder:
cd dockerProject. - Install dependencies:
npm install.
- Use
-
Create a Dockerfile:
- In the root of the project, create a Dockerfile. This file will outline the instructions for Docker on how to build and run your project in the cloud.
-
Build the Docker Image:
- Run the command:
docker build -t dockerreactimage .
- Run the command:
-
Run the Docker Container:
- Make sure nothing else is running on port 3000 to avoid conflicts.
- Use:
docker run -p 3000:3000 dockerreactimageto run the project. - It should now be available in the browser at: http://localhost:3000
-
Login to Docker Hub:
- Run:
docker loginin your terminal and log into Docker Hub.
- Run:
-
Tag the Docker Image:
- Tag the image for Docker Hub:
docker tag dockerreactimage:latest yourNameHere/dockerreactapp:latest - Replace
yourNameHerewith your actual Docker Hub username.
- Tag the image for Docker Hub:
-
Push the Docker Image:
- Push the image to Docker Hub:
docker push yourNameHere/dockerreactapp:latest
- Push the image to Docker Hub:
-
Deploy to DigitalOcean:
- SSH into your DigitalOcean Droplet:
ssh root@<DROPLET_IP>(replace<DROPLET_IP>with your Droplet's IP). - Pull the Docker image from Docker Hub:
docker pull yourNameHere/dockerreactapp:latest - Run the container on the Droplet:
docker run -d -p 8080:3000 yourNameHere/dockerreactapp:latest - Your project will now be live in the cloud at: http://<DROPLET_IP>:8080
- SSH into your DigitalOcean Droplet:
That's it! You've successfully dockerized a React app and deployed it to the cloud using Docker Hub and DigitalOcean.