I've always loved coding in C#. Equally, I love using Heroku to host personal projects - they have a nice UI, and a free tier!
However, there are a few limitations. Notably, .NET Core is not supported out of the box š¢. We can fix that!
The Fix
While Heroku isn't able to run C# code directly, they do have support for Docker containers. With that our solution is clear - dockerize all the things!
Deploying a .NET Core App to Docker
The following is not meant to be a .NET Core course, but you might pick up some basics on navigating the base files if you're a beginner. What we will cover:
- Creating a new .NET Core MVC project from the command-line
- Modifying the base files to be Heroku & Docker compatible
- Configuring a new Heroku app
- Deploy our app to Heroku š
1. Getting Started
Before we get into changing some code, we need to get our foundation components in order. Before continuing, you should have:
- .NET Core installed on your device
- I'm using v3.1 for this tutorial, which can be found here
- Docker installed on your device
- You can follow the steps here
- A Heroku account! - https://signup.heroku.com/
- Install the Heroku CLI, which allows us to communicate with the Container Registry.
With those out of the way, we're ready to go.
2. Creating a New .NET Core App
First off, we need to create our new .NET Core app. I'll be following the steps for setting up a basic .NET Core MVC project, which can be found here.
3. Setting Up a New Dockerfile
The dockerfile required is pretty basic, with nothing too complex happening.
# 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 out
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
# Run the app on container startup
# Use your project name for the second parameter
# e.g. MyProject.dll
ENTRYPOINT [ "dotnet", "HerokuApp.dll" ]
Additionally, you should probably setup a .dockerignore file to keep your image size down.
# .dockerignore
bin/
obj/
If you want to test out your container, you can run
docker build -t YourAppName .
# The name variable (abc) is simply used to refer to the
# container later when we want to close everything down.
docker run -d -p 8080:80 --name abc YourAppName
Navigate to http://localhost:8080 and you should see your app running.
All looking okay? Great! Lets shutdown the container locally. We don't need it running here if it's about to be up on the internet!
docker rm --force abc
4. Configuring a New App in Heroku
- Sign into Heroku, and create a new app from your personal dashboard at https://dashboard.heroku.com/apps
- You'll need to provide an app name, and deployment region
- Heroku will then display the various deployment options. We're going to be using Container Registry
5. Releasing to Heroku š
Last but not least, let's get it online.
1. Using the command-line, login to the Heroku container registry
heroku container:login
2. If you didn't above, make sure you build your docker
container!
docker build -t YourAppName .
3. Push your newly built container up to Heroku
# 'YourAppName' should be the name of the app you
# configured in Heroku in step 4.
heroku container:push -a YourAppName web
4. Finally, release!
heroku container:release -a YourAppName web
Now, if you just navigate to https://your-app-name.herokuapp.com, you should see the base app. Wait... that didn't work! What happend?!
Take a look at the heroku logs
heroku logs --tail
Within the scary looking logs, you'll see that .NET Core failed to start 'Kestrel', which is the web server for .NET Core. This means our app wasn't able to launch š¢.
If you remember when we started our docker container locally back when we configure it, one of the parameters we passed was a port. When Heroku gives us a port to use, our app needs to know about it!
Modify the end of the Dockerfile to be
# Dockerfile
# ...
# ENTRYPOINT [ "dotnet", "HerokuApp.dll" ]
# Use the following instead for Heroku
CMD ASPNETCORE_URLS=http://*:$PORT dotnet HerokuApp.dll
This allows the our container to use the Heroku-provided port on startup.
Re-deploy everything to Heroku:
- Build docker image
- Do a heroku container:push
- Do a heroku container:release
And voila - your very own .NET Core app hosted on the web.
Conclusion
I hope the above tutorial is helpful, and allows some newbies to get started on their own .NET Core side projects.
If anyone reading has some extra tips, or a better approach, please leave a comment below!
Thanks for reading š
Top comments (16)
Thanks Andrew, this was exactly the problem I run into.
I tried to use EXPOSE $PORT and ENV ASPNETCORE_URLS=http://*:$PORT
but for some strange reason only passing ENV var on command line helped.
Really helpfull post, thank you !
Great job! If you do not want to use Docker, there is also a third party buildpack for .NET Core; github.com/jincod/dotnetcore-build...
Hi, I followed all the steps in your tutorial, but after doing the release, when entering the page of the app that I published, I get an error that says:
Error: Missing required flag:
-a, --app APP app to run command against.
My application is called miappmvc and the commands that I enter in the heroku CLI are:
heroku container: push -a miappmvc web
heroku container: release -a miappmvc web
there it says that I am missing a flag. Do you know what flag it refers to?
Hi Luis,
It looks like you're nearly there! The error you're getting is just referring to the app flag '-a', which you're already including.
I suspect the issue is with the commands you're running. It should be:
heroku container:push -a miappmvc web
heroku container:release -a miappmvc web
(no space after "container:")
Lifesaver. Thank you so much! :D
Here's what I got at the end:
herokunett.herokuapp.com/
Nice job getting it live! Thanks for sharing š
I was scouting for options for Heroku and .Net Core and this is perfect! I haven't tried out the tutorial but i really appreciate the time and effort you took to document the steps. Will it work if I'm trying to deploy an API as well?
thank you so much, that was very usefull
Man you are a legend!
Thank you so much for this article! I hope you are doing well š
Andrew, thanks.
It worked!
This blog was really great, never seen a great blog like this before. I think Iām going to share this with my friends.
espirittech.com/dot-net-applicatio...
hello!! is it possible deploy asp.net core website with data in the database to heroku?