Deploying Multiple ASP.NET Core Apps To Docker And 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 a controller with CRUD operations for a movies database. It uses a class library Data, which contains repositories. The 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 
.csprojfirst 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 the image using 
docker tag command as discussed in the previous post.

Login to Docker Hub,

Run docker push to upload the 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

Up Next
    Ebook Download
    View all
    Learn
    View all