Deploying Multiple ASP.NET Core Apps to Docker & Azure

Note: Please read the previous post before continuing with this one, I discuss the basics of Docker and show how to deploy a simple application.

Problem

How to containerize multiple ASP.NET Core applications and deploy to Azure.

Scenario

Below is a screenshot of an example solution with two applications, Web and API. You could download the source code from GitHub.

API: has controller with CRUD operations for a movies database. It uses a class library Data, which contains repositories. Database is hosted in Azure and connection string stored in application settings (appsettings.json).

Web: has Razor Pages for CRUD operations and calls the API using HttpClient.

Solution

Create a Dockerfile for API project (Dockerfile_api):

Note: in order to optimise the process of building images (using cache in Docker) I am copying .csproj first to restore packages and then rest of the files. Also note that API project uses the class library so we’ll need to copy this too.

Create a Dockerfile for Web project (Dockerfile_web):

Create a docker-compose.yml file:

Build images for API and Web applications using either docker build command:

Or Run docker-compose up to test locally, which will also build the images of not already present:

Note: in order to push images to Docker Hub they need to be in username/image format. In case you’ve named your images differently than this, you could create a copy of image using docker tag command as discussed in the previous post.

Login to Docker Hub:

Run docker push to upload images to Docker Hub:

Next you can deploy to Azure using Azure “Web App for Containers” as discussed in the previous post. You’ll need to change the Application Settings for your applications in Azure; Web application needs a URL to API:

And API application needs a connection string:

You could now browse the application:

Further Resources

For an excellent and in-depth look at Docker, check out Wes Higbee courses on Pluralsight.

Source Code

GitHub: https://github.com/TahirNaushad/Fiver.Asp.Docker.Vs

Leave a Reply