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.
ProblemHow 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.
SolutionCreate a Dockerfile for API project (Dockerfile_api),
- # Build Stage
- FROM microsoft/aspnetcore-build AS build-env
- WORKDIR /src
- # Copy only .csproj and restore
- COPY ./Fiver.Asp.Docker.Data/Fiver.Asp.Docker.Data.csproj ./Fiver.Asp.Docker.Data/
- RUN dotnet restore ./Fiver.Asp.Docker.Data/
- COPY ./Fiver.Asp.Docker.Api/Fiver.Asp.Docker.Api.csproj ./Fiver.Asp.Docker.Api/
- RUN dotnet restore ./Fiver.Asp.Docker.Api/
- # Copy everything else and build
- COPY ./Fiver.Asp.Docker.Data/ ./Fiver.Asp.Docker.Data/
- RUN dotnet build ./Fiver.Asp.Docker.Data/
- COPY ./Fiver.Asp.Docker.Api/ ./Fiver.Asp.Docker.Api/
- RUN dotnet build ./Fiver.Asp.Docker.Api/
- # publish
- RUN dotnet publish ./Fiver.Asp.Docker.Api/ -o /publish --configuration Release
- # Publish Stage
- FROM microsoft/aspnetcore
- ENV MOVIES_DB_CONN=
- WORKDIR /app
- COPY --from=build-env /publish .
- ENTRYPOINT ["dotnet", "Fiver.Asp.Docker.Api.dll"]
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),
- # Build Stage
- FROM microsoft/aspnetcore-build AS build-env
- WORKDIR /src
- # Copy only .csproj and restore
- COPY ./Fiver.Asp.Docker.Web/Fiver.Asp.Docker.Web.csproj ./Fiver.Asp.Docker.Web/
- RUN dotnet restore ./Fiver.Asp.Docker.Web/
- # Copy everything else and build
- COPY ./Fiver.Asp.Docker.Web/ ./Fiver.Asp.Docker.Web/
- RUN dotnet build ./Fiver.Asp.Docker.Web/
- # publish
- RUN dotnet publish ./Fiver.Asp.Docker.Web/ -o /publish --configuration Release
- # Publish Stage
- FROM microsoft/aspnetcore
- ENV API_URL=
- WORKDIR /app
- COPY --from=build-env /publish .
- ENTRYPOINT ["dotnet", "Fiver.Asp.Docker.Web.dll"]
Create a docker-compose.yml file.
- version: '3.3'
- services:
- api:
- build:
- context: .
- dockerfile: Dockerfile_api
- image: naushadt25/api
- ports:
- - "8080:80"
- environment:
- - "MOVIES_DB_CONN=Server=..."
- web:
- build:
- context: .
- dockerfile: Dockerfile_web
- image: naushadt25/web
- ports:
- - "8090:80"
- environment:
- - "API_URL=http://api/movies"
- depends_on:
- - api
Build images for API and Web applications using either docker build command.






ajay kumarPosted Apr 18, 2018, 1:57 AM
What is difference b/w traditional deployement and deployment with docker
Naresh SinghalPosted Jan 22, 2018, 11:33 PM
Very good sir.....