Wednesday, July 21, 2021

.NET Core Hello World Web App and Docker

 1. Create a new web app from command line using the command: 

dotnet new web -o myWebApp


2.  Open the myWebApp.csproj file. It will have following text: 

<Project Sdk="Microsoft.NET.Sdk.Web">

     <PropertyGroup>

    <TargetFramework>net5.0</TargetFramework>

  </PropertyGroup>

</Project>


Change the target framework to netcoreapp3.1 and save the file. 

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>

    <TargetFramework>netcoreapp3.1</TargetFramework>

  </PropertyGroup>

</Project>

3. Create a dockerfile in the project in the same folder as the .csproj file. 
Enter the following text in it and save the file. 

FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build

WORKDIR /src
COPY ["myWebApp.csproj", "."]  #captalisation important!!
RUN dotnet restore "./myWebApp.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet publish "myWebApp.csproj" -c Release -o /app/publish

FROM base
WORKDIR /app
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "myWebApp.dll"]


Note 1 : The version of .NET core is taken as 3.1, which is matching to the one in .csproj file. 
2. Take care of capitalisation of file names. In the above file, if you spell "myWepApp.csproj" as "mywebapp.csproj", docker will not be able to find the file. 

4. Check that docker is runing. If not, run the Docker QuickStart Terminal .  ( I am talking this about Windows machine below 10, mine is Windows 8.1 and I require docker tool box insalled). 

If your docker is running you should get "Running" as a response to "docker-machine status default" command. 

5. Build the docker image: 
docker build -t mywebapptag . 

Here mywebapptag is the image tag and "." is the path of docker file /context. 

You should get an output like the following: 
E:\RajeshWorks\myWebApp>docker build -t mywebapptag .
Sending build context to Docker daemon  20.48kB
Step 1/12 : FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
 ---> dd0cb488d08f
Step 2/12 : FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
 ---> 5dbfe879d2f7
Step 3/12 : WORKDIR /src
 ---> Using cache
 ---> 8aa0dcb0d5b1
Step 4/12 : COPY ["myWebApp.csproj", "."]  #captalisation important!!
 ---> Using cache
 ---> 4cfe0fcb97e9
Step 5/12 : RUN dotnet restore "./myWebApp.csproj"
 ---> Running in faacadf31de2
  Determining projects to restore...
  Restored /src/myWebApp.csproj (in 219 ms).
Removing intermediate container faacadf31de2
 ---> a4c8792fa8b1
Step 6/12 : COPY . .
 ---> 066f423bf5cd
Step 7/12 : WORKDIR "/src/."
 ---> Running in 2db699fe1c13
Removing intermediate container 2db699fe1c13
 ---> 04d5b634c493
Step 8/12 : RUN dotnet publish "myWebApp.csproj" -c Release -o /app/publish
 ---> Running in fe6064693b56
Microsoft (R) Build Engine version 16.7.2+b60ddb6f4 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  Restored /src/myWebApp.csproj (in 236 ms).
  myWebApp -> /src/bin/Release/netcoreapp3.1/myWebApp.dll
  myWebApp -> /app/publish/
Removing intermediate container fe6064693b56
 ---> dec97175c592
Step 9/12 : FROM base
 ---> dd0cb488d08f
Step 10/12 : WORKDIR /app
 ---> Using cache
 ---> 9af8e3a6945b
Step 11/12 : COPY --from=build /app/publish .
 ---> 03eba5ecb2f0
Step 12/12 : ENTRYPOINT ["dotnet", "myWebApp.dll"]
 ---> Running in 7a375a435725
Removing intermediate container 7a375a435725
 ---> 461cbdc4bee4
Successfully built 461cbdc4bee4
Successfully tagged mywebapptag:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Win
dows Docker host. All files and directories added to build context will have '-r
wxr-xr-x' permissions. It is recommended to double check and reset permissions f
or sensitive files and directories.



6. The image is now ready, you can confirm it with the command "docker ps -a" 

REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
mywebapptag                       latest              27ee7997303a        53 seconds ago      208MB
<none>                            <none>              44b18da96e75        53 seconds ago      711MB
mcr.microsoft.com/dotnet/aspnet   3.1                 dd0cb488d08f        8 days ago          208MB
mcr.microsoft.com/dotnet/sdk      3.1                 5dbfe879d2f7        8 days ago          710MB

7. Check the ip of the docker machine, so that you may use it to browse your web site. 
"docker-machine ip default" 
=> 192.168.99.100

8. Run the docker image : 
docker run -d -p 5000:80 --name mywebappcontainer mywebapptag 

This will return you a large hex number. 

"docker container ls" command will return the following : 


CONTAINER ID   IMAGE                  COMMAND                    CREATED             STATUS              
6237a2fecded        mywebapptag         "dotnet myWebApp.dll"   2 minutes ago       Up 2 minutes

PORTS                         NAMES
0.0.0.0:5000->80/tcp   mywebappcontainer


9. Browse to the url  http://192.168.99.100:5000

You should be able to see "Hello World!". 

Note that the IP address is the one we got in step 7. 





No comments:

Post a Comment

How to check local and global angular versions

 Use the command ng version (or ng v ) to find the version of Angular CLI in the current folder. Run it outside of the Angular project, to f...