Deploying NET 6 WEB API to Heroku using Docker
Tags: net6, docker, heroku
Published: 2021-11-14 12:38:52

---
title: Deploying NET 6 WEB API to Heroku using Docker
published: true
description: 
tags: NET6, Docker, Heroku
//cover_image: https://direct_url_to_image.jpg
---
In this article we’ll publishing a NET 6 application on Heroku using Docker.

### Prerequisite:

* [Heroku Command Line Interface](https://devcenter.heroku.com/articles/heroku-cli)
* [Docker](https://download.docker.com/)
* [.NET 6](https://dotnet.microsoft.com/download/dotnet/6.0)


### Create a new .NET 6 WEB API project

The First step we need to create a new .NET 6 project, to do this  we’ll use dotnet cli. Open a terminal or CMD in your computer and type this command below:

```csharp
dotnet new webapi
```

This command will create a minimal web API:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c6tko7duog5ilxpry291.png)

to get to know more about minimal web API, I recommend this article [Minimal APIs overview](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis?view=aspnetcore-6.0)

### Create a Heroku project

To host our project on Heroku, we need to go to [Heroku](https://dashboard.heroku.com/login) website and Sign In or Sign Up

After this we need to create a new project. To this article I'll call net6heroku, but you can choose another name.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hfw6hrudxvbw7v13bm8h.png)



### Containerize a .NET 6 web API

In your project folder, create a file `Dockerfile` without any extension and paste below lines: (please, change dotnet-heroku.dll based on your Solution name)

```csharp
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["dotnet-heroku.csproj", "."]
RUN dotnet restore "./dotnet-heroku.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "dotnet-heroku.csproj" -c Release -o /app/build

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
CMD ASPNETCORE_URLS=http://*:$PORT dotnet dotnet-heroku.dll
```

After this run this command bellow to create a new Docker image from you project:

```csharp
docker build -t {your heroku project name} .
//like docker build -t net6heroku .
```

And this command to logIn at heroku-cli

```csharp
heroku login
heroku container:login
```

Now we need to TAG our imagem to push to Heroku’s Container registry:

```csharp
docker tag <MyApp> registry.heroku.com/<MyApp>/web
//like docker tag net6heroku registry.heroku.com/net6heroku/web
```

Build the image locally and push it to Heroku’s Container Registry

```csharp
heroku container:push web -a <MyApp>
//like heroku container:push web -a net6heroku
```

Now release the image on Heroku to be deployed:

```csharp
heroku container:release web -a <MyApp>
//like heroku container:release web -a net6heroku
```

for this demo, I have used net6heroku as MyApp

[net6heroku-example](https://net6heroku.herokuapp.com/WeatherForecast)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q1dsq2zwltcslssvp97v.png)