DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 968,547 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Thiago da Silva Adriano
Thiago da Silva Adriano

Posted on

Deploying NET 6 WEB API to Heroku using Docker

In this article we’ll publishing a NET 6 application on Heroku using Docker.

Prerequisite:

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:

dotnet new webapi
Enter fullscreen mode Exit fullscreen mode

This command will create a minimal web API:

Image description

to get to know more about minimal web API, I recommend this article Minimal APIs overview

Create a Heroku project

To host our project on Heroku, we need to go to Heroku 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

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)

#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
Enter fullscreen mode Exit fullscreen mode

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

docker build -t {your heroku project name} .
//like docker build -t net6heroku .
Enter fullscreen mode Exit fullscreen mode

And this command to logIn at heroku-cli

heroku login
heroku container:login
Enter fullscreen mode Exit fullscreen mode

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

docker tag <MyApp> registry.heroku.com/<MyApp>/web
//like docker tag net6heroku registry.heroku.com/net6heroku/web
Enter fullscreen mode Exit fullscreen mode

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

heroku container:push web -a <MyApp>
//like heroku container:push web -a net6heroku
Enter fullscreen mode Exit fullscreen mode

Now release the image on Heroku to be deployed:

heroku container:release web -a <MyApp>
//like heroku container:release web -a net6heroku
Enter fullscreen mode Exit fullscreen mode

for this demo, I have used net6heroku as MyApp

net6heroku-example

Image description

Top comments (2)

Collapse
 
alexeyromanchenko profile image
AlexeyRomanchenko

It seems that something has changed .. I have this error in the logs ,

State changed from crashed to starting
2022-07-23T11:55:41.622521+00:00 heroku[web.1]: Starting process with command /bin/sh -c ASPNETCORE_URLS\=http://\*:\42902\ dotnet\ WebApi.API.dll
2022-07-23T11:55:42.743639+00:00 heroku[web.1]: State changed from starting to crashed

Haven't you faced with something similar ?

Collapse
 
alexeyromanchenko profile image
AlexeyRomanchenko

I assume , it can be connected with a slash before the port number, I do not know why it is created there, investigating

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.