Creating and Deploying React App with .NET Core 3.1 on DigitalOcean
Tags: react, dotnet, github
Published: 2021-03-10 17:35:12

A few weeks ago I decided to build a [personal website](https://hiba-eldursi.com/). I did some research and finally decided on using React for the frontend and .NET Core for the backend. 

In this tutorial, I'll go through all the steps needed to create and deploy a brand new react app with .NET Core 3.1 from a new repository on Github to DigitalOcean. 
> This assumes that you have .NET Core 3.1 installed on your machine.



#Creating and Running React App

- Run the following command to create your react app.

> This will also create the directory

```bash
dotnet new react -o my-react-app
```

This command uses the "ASP.NET Core with React.js" template installed with .NET core. After running the command, your new directory should contain something like this 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hpu1axeqso6lu1yl6f8o.png)

- Click on the .csproj file and open it using your favourite IDE. 
- Looking at the project, you will notice that you have a "ClientApp" folder. This folder contains all the files needed for the "React" part of the app.
- Build the project. Building the project restores the npm dependencies on the first run.
- Run the app as normal by right-clicking on the project and selecting "run". This should open a new page that looks something like this.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sny0qpdmz0zl6ousdbbd.png)
 
#Deploying React App

Now that we have the app running we'll prepare the app to be deployed. To do this we're going to: 

1. Create a Github account and a new repository and add the project we created to the repository. 
2. Add a DockerFile to the root of the repository

###Creating Github Account 

- We'll start by creating a new account on Github. You can skip this step if you already have one. Navigate to [Github](https://github.com/) and click on "Sign Up" at the top of the page. The page you'll see is 👇🏼

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jeibt9csvsiydv50rfcb.png)
 
After filling in your details you'll get taken to a page where you can select your experience, what kind of work you do and what you're interested in. Finally, you'll be sent an email to verify your email address and voilà!

###Creating Github Repository
- Now that the Github account is all set up, we'll create our new repository.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hhgf6frtsbol59b1wjxf.png)
 
 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7a3l2f89cbgr8d1cliwk.png)

- We'll clone our repository by clicking on "Code" 👇🏼 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n8l5ersdlmq6wexedhxt.png)
 
- Copy the HTTPS URL, open the terminal on your computer and run the following: 
> Replace {https-url} with your HTTPS URL

```bash
git clone {https-url}
```

If you have just created a new Github account you will be asked to authenticate yourself and enter your username and password.

####Adding Project to Github Repository

- Once we've got our new repository cloned copy the folder containing your new project("my-react-app") and paste/move it to your repository folder("my-react-app"). 

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/euequy615vezeezyxrsj.png)

Run the commands below to commit and push your changes.
```bash
git add .
git commit -m "Add new react project"
git push
```

You should now be able to see your changes in your Github repository.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pn40f6b8jff5ccczk3bb.png)

####Adding Dockerfile to Github Repository

The final step to prepare your project to be deployed to DigitalOcean is to add a Dockerfile to the root of your repository.

#####Dockerfile

```GO
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
RUN curl -sL https://deb.nodesource.com/setup_10.x |  bash -
RUN apt-get install -y nodejs

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
RUN curl -sL https://deb.nodesource.com/setup_10.x |  bash -
RUN apt-get install -y nodejs

WORKDIR /src
COPY ["my-react-app/my-react-app.csproj", ""]
RUN dotnet restore "my-react-app.csproj"

COPY . .
WORKDIR "/src/"
RUN dotnet build "my-react-app/my-react-app.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "my-react-app/my-react-app.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "my-react-app.dll"]
```

> If you have Docker installed on your machine and want to test the dockerfile, navigate to the root of your repository and run the following commands
`docker build -t my-react-app .
docker run -p 80:80 my-react-app`

Run the commands below to commit and push your changes.
```bash
git add .
git commit -m "Add Dockerfile"
git push
```

Now your repository is all set and we can move on to create an account with our hosting provider(DigitalOcean). 

> **You can find the code on my Github account here 👇🏼
 {% github eldursi/my-react-app no-readme %}**


###Creating DigitalOcean Account

At the time of writing 9th of March 2021, DigitalOcean offer $100, 60-day credit when you create a new account with them using a referral link. I've already created an account with them so this is my **[Referral Link](https://m.do.co/c/08f47fdc856e)**

You will need to add your billing information but you won't be charged until the end of the 2 months. 

So after you've created your account, you'll be asked to create a new project. Add in the project name but skip the "Move Resources" step.

Once that's done you'll be shown to the default account page.

DigitalOcean offers some really great services but the one that we'll be using here is a new one which is the App Management feature.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvt6yvkw8b9ph5dc74nd.png)
 
Once you've clicked on "Apps", you'll see this screen.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pxnse669hlk92udsdrd8.png)

Select "Launch Your App" and you'll be taken to a screen to select where source code will be coming from

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4lq2hti2a8ytsa2xmio.png)
  
Our source is on Github so we'll select that. Then you'll be taken to a screen where you can login to your Github account. You may need to modify the access permissions on your repository to allow DigitalOcean access

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0le9pmyodlwhecq9chsx.png)
 
Now that your DigitalOcean account has access to your repo you will be able to select it. A great thing that DigitalOcean allows you to do is to publish from a certain branch whenever you push into it. Master will be selected as default. 

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vn7stekimq0e5kpl1u6f.png)

When you click on "Next" DigitalOcean will go looking for your Dockerfile. If it all goes well, you should see this page 

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7rk3e7x8sr447dyj054x.png)
 
Moving on, I will keep the selected region and the app name as they are

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/056wyf7xcnuk33nozh07.png)


We finally get to the final step and **launch**

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/poy6wx9i1r4sx9thb1lm.png)
  
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9olfa521icjkpsfy7jya.png)
 
If everything is going well you'll get transferred to this page where you'll be able to monitor your deployment.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ehkpp2m3p66jj99vuizs.png)
 
Until it succeeds 🥳

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ba7g36r1wtpxtjo3d9os.png)
 
On first deploy, you might get this error (Error code: DeployContainerHealthChecksFailed)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/84p2mz7ry2c4fgyhi7b5.png)

If that happens, navigate to "Components" and make sure that the exposed HTTP port is set to 80

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5g61h5gyzspd64keyd3s.png)


----

As DigitalOcean isn’t a domain registrar, you can’t purchase a domain from them. If you have an existing domain you can transfer it over to DigitalOcean to manage. DigitalOcean have a great article on how to do that [here](https://www.digitalocean.com/docs/app-platform/how-to/manage-domains/#:~:text=Go%20to%20https%3A%2F%2Fcloud,Name%2C%20then%20click%20to%20proceed.) 

----

You can find the full source code on my Github account:

{% github eldursi/my-react-app no-readme %}