In this post we shall see how to create a basic Asp.Net core Hello World App and package it into a Docker image. We will deploy the image as a Docker container and execute the Asp.Net Core application from Docker.
First let us start by creating an Asp.Net Core application using Visual Studio 2019The .dockerignore file
has the following exclusions which will not be included in the Docker image.
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
Let’s make a small update to the message displayed in the Home page.
We will update the Home/Index view to display a welcome message as follows
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h3 class="display-4">Welcome to Asp.Net Core Docker</h3>
</div>
When we build the docker image, it will try to restore the NuGet packages for
which it needs to download the packages from internet. If you are in a
corporate firewall behind the proxy then we need to set the proxy congifuration
so that it can download and restore the packages.
Add the below 2 lines to configure proxy for http & https with the proxy
server and credentials
WORKDIR /src
COPY ["HelloCore.csproj", ""]
ARG HTTP_PROXY="http://<username>:<password>@<proxyserver>:8080"
ARG HTTPS_PROXY="http://<username>:<password>@<proxyserver>:8080"
RUN dotnet restore "./HelloCore.csproj"
We can build the docker image for the Hello World Asp.Net Core project as
follows
>docker build -t hellocore .
Sending build context to Docker daemon 4.395MB
Step 1/16 : FROM
mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1803 AS base
3.1-nanoserver-1803: Pulling from dotnet/core/aspnet
e46172273a4e: Pull complete
4e0aad55be14: Pull complete
84b221f7a453: Pull complete
1fe099edc738: Pull complete
a091df200146: Pull complete
92ed030ab670: Pull complete
fc1b71d39b32: Pull complete
773dcb962d4f: Pull complete
Digest:
sha256:3b4383b72b53a00895267571d08e9591eab1b914f35b2c2c4a2413d178eca089
Status: Downloaded newer image for
mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1803
---> 22613f6353d7
Step 2/16 : WORKDIR /app
---> Running in deeef4b1b75a
Removing intermediate container deeef4b1b75a
---> 37555b9de604
Step 3/16 : EXPOSE 80
---> Running in c8ff0e885975
Removing intermediate container c8ff0e885975
---> 74425e01227b
Step 4/16 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1803
AS build
3.1-nanoserver-1803: Pulling from dotnet/core/sdk
e46172273a4e: Already exists
4e0aad55be14: Already exists
d4ab8b1aa41b: Pull complete
6a78bc115e4e: Pull complete
b17f9e12a86e: Pull complete
f54f0fc0b6ad: Pull complete
d04f75b18a67: Pull complete
4d403afe1106: Pull complete
1b87aeb9ca18: Pull complete
Digest: sha256:32929dc0b6d20a038c4bc082ccce9dcd1e87f3150fcabdc2f9430853c4a040d0
Status: Downloaded newer image for
mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1803
---> 72e582217dbf
Step 5/16 : WORKDIR /src
---> Running in d9077abc552c
Removing intermediate container d9077abc552c
---> 18c5c795d2ec
Step 6/16 : COPY ["HelloCore.csproj", ""]
---> 1bc261605621
Step 7/16 : RUN dotnet restore "./HelloCore.csproj"
---> Running in 5cb3675173b4
Restore completed in 740.01 ms for C:\src\HelloCore.csproj.
Removing intermediate container 5cb3675173b4
---> d98146917164
Step 8/16 : COPY . .
---> ae9f3bbe1b66
Step 9/16 : WORKDIR "/src/."
---> Running in 8c7f8aa6b6b7
Removing intermediate container 8c7f8aa6b6b7
---> 483b78d9da18
Step 10/16 : RUN dotnet build "HelloCore.csproj" -c Release
-o /app/build
---> Running in 740cb7897afb
Microsoft (R) Build Engine version 16.4.0-preview-19529-02+0c507a29b
for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restore completed in 46.26 ms for C:\src\HelloCore.csproj.
You are using a preview version of .NET Core. See:
https://aka.ms/dotnet-core-preview
HelloCore -> C:\app\build\HelloCore.dll
HelloCore -> C:\app\build\HelloCore.Views.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:08.23
Removing intermediate container 740cb7897afb
---> d4a1042d579c
Step 11/16 : FROM build AS publish
---> d4a1042d579c
Step 12/16 : RUN dotnet publish "HelloCore.csproj" -c Release
-o /app/publish
---> Running in 9bdcd9164c5f
Microsoft (R) Build Engine version 16.4.0-preview-19529-02+0c507a29b
for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restore completed in 46.36 ms for C:\src\HelloCore.csproj.
You are using a preview version of .NET Core. See:
https://aka.ms/dotnet-core-preview
HelloCore -> C:\src\bin\Release\netcoreapp3.1\HelloCore.dll
HelloCore ->
C:\src\bin\Release\netcoreapp3.1\HelloCore.Views.dll
HelloCore -> C:\app\publish\
Removing intermediate container 9bdcd9164c5f
---> d80159b14f36
Step 13/16 : FROM base AS final
---> 74425e01227b
Step 14/16 : WORKDIR /app
---> Running in 352109c49f01
Removing intermediate container 352109c49f01
---> a2776878bd59
Step 15/16 : COPY --from=publish /app/publish .
---> 8f6a898955a7
Step 16/16 : ENTRYPOINT ["dotnet", "HelloCore.dll"]
---> Running in 5b8ae4f5c5d6
Removing intermediate container 5b8ae4f5c5d6
---> c6d171b63436
Successfully built c6d171b63436
Successfully tagged hellocore:latest
Once the build is completed run the image ls command to make sure that the image is created.
>docker image ls
REPOSITORY
TAG
IMAGE ID
CREATED SIZE
hellocore
latest
c6d171b63436 2 minutes
ago 472MB
mcr.microsoft.com/dotnet/core/sdk
3.1-nanoserver-1803 72e582217dbf 9
months ago 854MB
mcr.microsoft.com/dotnet/core/aspnet 3.1-nanoserver-1803
22613f6353d7 9 months ago
467MB
Next run the docker image and export port#7000. Map port 80 from the
Docker image to port 7000 in the machine.
>docker run -d -p 7000:80 hellocore
8e73c87223090b1f36d8e5088701fcb567d8dd44d8312808270b426489917646
Run the container ls command to make sure that the Docker image is live
and running
>docker container ls
CONTAINER ID IMAGE
COMMAND
CREATED STATUS
PORTS
NAMES
8e73c8722309 hellocore "dotnet HelloCore.dll" 9 seconds ago Up 7 seconds 0.0.0.0:7000->80/tcp loving_hypatia
To view details of the running container run the docker ps command
>docker ps
CONTAINER ID IMAGE
COMMAND
CREATED STATUS
PORTS
NAMES
8e73c8722309 hellocore "dotnet HelloCore.dll" 17 seconds ago Up 14 seconds 0.0.0.0:7000->80/tcp loving_hypatia
Open a browser and enter the address http://localhost:7000
No comments:
Post a Comment