220 Webapi in Docker - chempkovsky/CS82ANGULAR GitHub Wiki

Notes

  • We need to run two Webapi projects in Docker.
    • LpPhBkWebApp.csproj
    • PhBkWebApp.csproj

Steps required to accomplish the task

LpPhBkWebApp

Create Dockerfile for LpPhBkWebApp

  • right click LpPhBkWebApp.csproj select
    • Add/Docker Support menu item
  • Docker file options dialog will be shown
    • click Linux radio button
    • click Ok button
    • Dockerfile will be added to the folder of the project
Click to show the file
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["LpPhBkWebApp/LpPhBkWebApp.csproj", "LpPhBkWebApp/"]
COPY ["LpPhBkControllers/LpPhBkControllers.csproj", "LpPhBkControllers/"]
COPY ["LpPhBkContext/LpPhBkContext.csproj", "LpPhBkContext/"]
COPY ["LpPhBkEntity/LpPhBkEntity.csproj", "LpPhBkEntity/"]
COPY ["LpPhBkViews/LpPhBkViews.csproj", "LpPhBkViews/"]
COPY ["PhBkViews/PhBkViews.csproj", "PhBkViews/"]
RUN dotnet restore "LpPhBkWebApp/LpPhBkWebApp.csproj"
COPY . .
WORKDIR "/src/LpPhBkWebApp"
RUN dotnet build "LpPhBkWebApp.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "LpPhBkWebApp.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "LpPhBkWebApp.dll"]

Create Docker Image for LpPhBkWebApp

  • run the command
docker build . -f Dockerfile -t webapi-phone-book-lookup

Run LpPhBkWebApp in docker

  • run the command
Click to show the command
docker run -d -p 91:80 ^
 ^
-e "ConnectionStrings:LpPhnPhBkConnection": "Data Source=SVR2016SQL2017;Initial Catalog=LpPhnPhBkDbDef;Persist Security Info=True;User ID=sa;Password=Password_here" ^
-e "ConnectionStrings:LpEmpPhBkConnection": "Data Source=SVR2016SQL2017;Initial Catalog=LpEmpPhBkDbDef;Persist Security Info=True;User ID=sa;Password=Password_here" ^
-e "ConnectionStrings:LpPhBkConnection": "Data Source=SVR2016SQL2017;Initial Catalog=LpPhBkDbDef;Persist Security Info=True;User ID=sa;Password=Password_here" ^
-e "JWT:ValidAudience": "PhBkAudience" ^
-e "JWT:ValidIssuer": "PhBkIssuer" ^
-e "JWT:Secret": "JWTAuthenticationHIGHsecuredPasswordVVVp1OH7Xzyr" ^
 ^
-e "PhbkDivisionViewExtForLkUpConf:HostName": "192.168.100.3" ^
-e "PhbkDivisionViewExtForLkUpConf:Username": "admin" ^
-e "PhbkDivisionViewExtForLkUpConf:Password": "admin" ^
-e "PhbkDivisionViewExtForLkUpConf:VirtualHostName": "phbkhost" ^
-e "PhbkDivisionViewExtForLkUpConf:ClusterIpAddresses": [] ^
 ^
-e "PhbkEmployeeViewExtForLkUpConf:HostName": "192.168.100.3" ^
-e "PhbkEmployeeViewExtForLkUpConf:Username": "admin" ^
-e "PhbkEmployeeViewExtForLkUpConf:Password": "admin" ^
-e "PhbkEmployeeViewExtForLkUpConf:VirtualHostName": "phbkhost" ^
-e "PhbkEmployeeViewExtForLkUpConf:ClusterIpAddresses": [] ^
 ^
-e "PhbkPhoneViewExtForLkUpConf:HostName": "192.168.100.3" ^
-e "PhbkPhoneViewExtForLkUpConf:Username": "admin" ^
-e "PhbkPhoneViewExtForLkUpConf:Password": "admin" ^
-e "PhbkPhoneViewExtForLkUpConf:VirtualHostName": "phbkhost" ^
-e "PhbkPhoneViewExtForLkUpConf:ClusterIpAddresses": [] ^
 ^
webapi-phone-book-lookup 

PhBkWebApp

Create Dockerfile for PhBkWebApp

  • right click PhBkWebApp.csproj select
    • Add/Docker Support menu item
  • Docker file options dialog will be shown
    • click Linux radio button
    • click Ok button
    • Dockerfile will be added to the folder of the project
Click to show the file
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["PhBkWebApp/PhBkWebApp.csproj", "PhBkWebApp/"]
COPY ["PhBkViews/PhBkViews.csproj", "PhBkViews/"]
COPY ["PhBkControllers/PhBkControllers.csproj", "PhBkControllers/"]
COPY ["PhBkEntity/PhBkEntity.csproj", "PhBkEntity/"]
COPY ["LpPhBkViews/LpPhBkViews.csproj", "LpPhBkViews/"]
COPY ["PhBkContext/PhBkContext.csproj", "PhBkContext/"]
RUN dotnet restore "PhBkWebApp/PhBkWebApp.csproj"
COPY . .
WORKDIR "/src/PhBkWebApp"
RUN dotnet build "PhBkWebApp.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "PhBkWebApp.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "PhBkWebApp.dll"]

Create Docker Image for PhBkWebApp

  • run the command
docker build . -f Dockerfile -t webapi-phone-book

Run PhBkWebApp in docker

  • run the command
Click to show the command
docker run -d -p 92:80 ^
 ^
-e "ConnectionStrings:PhBkConnection": "Data Source=SVR2016SQL2017;Initial Catalog=PhBkDbDef;Persist Security Info=True;User ID=sa;Password=Password_here" ^
-e "ConnectionStrings:AuthConnection": "Data Source=SVR2016SQL2017;Initial Catalog=PhBkDbAuth;Persist Security Info=True;User ID=sa;Password=Password_here" ^
-e "ConnectionStrings:AspNetRegConnection": "Data Source=SVR2016SQL2017;Initial Catalog=PhBkAspNet;Persist Security Info=True;User ID=sa;Password=Password_here" ^
-e "JWT:ValidAudience": "PhBkAudience" ^
-e "JWT:ValidIssuer": "PhBkIssuer" ^
-e "JWT:Secret": "JWTAuthenticationHIGHsecuredPasswordVVVp1OH7Xzyr" ^
 ^
-e "PhbkDivisionViewExtForLkUpConf:HostName": "192.168.100.3" ^
-e "PhbkDivisionViewExtForLkUpConf:Username": "admin" ^
-e "PhbkDivisionViewExtForLkUpConf:Password": "admin" ^
-e "PhbkDivisionViewExtForLkUpConf:VirtualHostName": "phbkhost" ^
-e "PhbkDivisionViewExtForLkUpConf:ClusterIpAddresses": [] ^
 ^
-e "PhbkEmployeeViewExtForLkUpConf:HostName": "192.168.100.3" ^
-e "PhbkEmployeeViewExtForLkUpConf:Username": "admin" ^
-e "PhbkEmployeeViewExtForLkUpConf:Password": "admin" ^
-e "PhbkEmployeeViewExtForLkUpConf:VirtualHostName": "phbkhost" ^
-e "PhbkEmployeeViewExtForLkUpConf:ClusterIpAddresses": [] ^
 ^
-e "PhbkPhoneViewExtForLkUpConf:HostName": "192.168.100.3" ^
-e "PhbkPhoneViewExtForLkUpConf:Username": "admin" ^
-e "PhbkPhoneViewExtForLkUpConf:Password": "admin" ^
-e "PhbkPhoneViewExtForLkUpConf:VirtualHostName": "phbkhost" ^
-e "PhbkPhoneViewExtForLkUpConf:ClusterIpAddresses": [] ^
 ^
webapi-phone-book

Error CTC1003 Visual Studio container tools require Docker to be running

  • After adding Docker support
    • Sometimes you need to debug your app without Docker
    • Stopping Docker is not enough
      • Visual Studio returns an error
Error CTC1003 Visual Studio container tools require Docker to be running
  • Here is how to switch back and forth between a Docker and a Non-Docker debug configuration

    • open Your_Project_Name.csproj.user file
      • In our case we have two files
        • PhonebookSolution\PhBkWebApp\PhBkWebApp.csproj.user
        • PhonebookSolution\LpPhBkWebApp\LpPhBkWebApp.csproj.user
  • the content of the files will be as follows

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <ActiveDebugProfile>Docker</ActiveDebugProfile>
  </PropertyGroup>
</Project>
  • we need to replace Docker with Debug
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <ActiveDebugProfile>Debug</ActiveDebugProfile>
  </PropertyGroup>
</Project>

Final Docker Images

  • Run windows terminal
  • in the windows terminal make the solution folder current
    • Note: It must be the solution folder and not the project folder!!!
      • Generated Docker files have the commands like COPY ["LpPhBkControllers/LpPhBkControllers.csproj", "LpPhBkControllers/"]
      • Directory.Build.props must be available for the build process
  • make sure Directory.Build.props file has NOTMODELING-value
<Project>
    <PropertyGroup>
        <DefineConstants>NOTMODELING</DefineConstants>
    </PropertyGroup>
</Project>  
  • in the windows terminal run the command
docker build . -f ./PhBkWebApp/Dockerfile -t webapi-phn-bk
  • in the windows terminal run the command
docker build . -f ./LpPhBkWebApp/Dockerfile -t webapi-phn-lkup
⚠️ **GitHub.com Fallback** ⚠️