Deploying .Net Core web API to Heroku using Docker

Yogita Kumar
3 min readJul 16, 2020

Prerequisite:

RESTful Web API in .Net Core 3.1 with Database, preferably on server/cloud

You can refer medium article here , if you are new to REST API Development.

Please note this demo is on local DB and so we can create some database steps on free cloud service, please follow below steps:

  1. Create an account in https://www.freesqldatabase.com/ which provides 5MB of free space and that will help for our demo.
  2. Once you register and your MySql DB is create you will receive email stating your DB details something like below:
Host: sql2.freesqldatabase.com
Database name: xxxxxxx
Database user: xxxxxxxxx
Database password: xxxxxx
Port number: 3306

3. Visit phpMyAdmin to manage your database and create Department table as you need. (refer below screenshot for example)

4. We need to adjust the appsetting.json to provide the DB details as below: (change details as per your credential replacing xxxxxx)

“cs”: “server=sql2.freesqldatabase.com;port=3306;database=xxxxxxxx;uid=xxxxxxxxx;password=xxxxxxxx”

5. As we are connecting to MySql now, we need to add package using NuGet Package manager :

MySql.Data.EntityFrameworkCore
MySql.Data.EntityFrameworkCore.Design

6. change the highlighted code in Startup.cs file to use MySql.

public void ConfigureServices(IServiceCollection services){        services.AddDbContext<EmpApplicationContext>(options =>        options.UseMySQL(Configuration.GetConnectionString("cs")));        services.AddControllers();}

7. To host API on Heroku , Go to Heroku website and Sign In or Sign Up

8. Create new app as follows:

9. In your machine install latest version of Heroku CLI here

10. In your project folder, create manually a file ‘Dockerfile’ without any extension and paste below lines: (Change , highlighted ApiStudentDB.dll based on your Solution name)

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS buildWORKDIR /appCOPY ./ ./RUN dotnet publish -c Release -o outFROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slimWORKDIR /appCOPY --from=build /app/out .CMD ASPNETCORE_URLS=http://*:$PORT dotnet ApiStudentDB.dll

11. Install Docker on your local PC, and create your account.

12. Run the following commands in the project folder replacing <MyApp> by the app’s name created in Heroku.

docker build -t <MyApp> .

13. Need to authenticate Heroku account using below command: This will need to press any key to open web browser window containing login button.

heroku login

14. Return to terminal and run below command:

heroku container:login

15. Tag the image

docker tag <MyApp> registry.heroku.com/<MyApp>/web

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

heroku container:push web -a <MyApp>

17. Release the image on Heroku to be deployed:

heroku container:release web -a <MyApp>

18. Access the URL of your app on heroku web url as shown below:

https://<MyApp>.herokuapp.com/index.html

for this demo, I have used testmywebapi as MyApp

--

--

Yogita Kumar

Google Developer Expert Flutter| Cloud Enthusiast | Full Stack Developer | .NET Developer |Coach and Trainer