Deploy dotnet core app to heroku
An aspnet core web app/api app is assumed to have been implemented already. Following is how to deploy it.
Heroku PORT
Heroku saves in the environment variable PORT
, the port where the application should listen. So the port needs to be changed to that one.
//Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
var herokuPort = Environment.GetEnvironmentVariable("PORT");
if (!string.IsNullOrWhiteSpace(herokuPort))
{
webBuilder.UseUrls($"http://*:{herokuPort}");
}
webBuilder.UseStartup<Startup>();
});
Dockerfile
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o publish
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/publish .
ENTRYPOINT ["dotnet", "Bustroker.Notes.WebUI.dll"]
Build image, push and deploy to heroku using heroku cli and heroku containers registry.
An app is assumed to have been created in heroku site. Deployment can be done, as heroku explaines in their documentation, in various ways. Here’s one of them, using heroku container registry.
First install heroku cli
curl https://cli-assets.heroku.com/install-ubuntu.sh | sh
Login to heroku and heroku containers registry, using command line
heroku login -i
heroku container:login
Have heroku build and push the image to their registry
heroku container:push <REPOSITORY_NAME> --app <APP_NAME>
Deploy a container from the image pushed
heroku container:release <REPOSITORY_NAME> --app <APP_NAME>
Browse to (or curl) https://<APP_NAME>.herokuapp.com/
.