Deploy an asp.net core application on Linux with docker

Docker file is like a recipe that tells how to build an image for our application. Below, we have created a simple asp.net core app.
# cat Dockerfile.txt
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY MyMicroservice.csproj .
RUN dotnet restore
COPY . .
RUN dotnet publish -c release -o /app

FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "MyMicroservice.dll"]
The docker file will start with FROM mcr.microsoft.com/dotnet/sdk:5.0, which is the latest version at the time, – sdk AS build. This tells Docker that we’re starting from the Microsoft .NET Core sdk base image. Then we’ll say WORKDIR/src to move into a virtual directory inside of the Docker image.

We want to copy all of our source files into the Docker image temporarily, so we can build the application. We’ll start with just the csproj file first. Copy .csproj into the Docker image, and then we’re going to RUN dotnet restore to pull down any packages that we need to build the application.

After that restore step, we’ll COPY the rest of the source files, and then RUN dotnet publish -c Release, and say that output should go into another virtual directory called /app.
When creating the image we also define a friendly name and tag and build our
image
# docker build -t first_tag .
Sending build context to Docker daemon 2.048kB
Step 1/11 : FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
5.0: Pulling from dotnet/sdk
f7ec5a41d630: Already exists
dbcaaf8b5596: Pull complete
9b8013b17b7e: Pull complete
d8d50a433506: Pull complete
1396b16d0d87: Pull complete
dd4da9d953fb: Pull complete
f83e9c616794: Pull complete
9887694812e5: Pull complete
Digest: sha256:85ea9832ae26c70618418cf7c699186776ad066d88770fd6fd1edea9b260379a
Status: Downloaded newer image for mcr.microsoft.com/dotnet/sdk:5.0
---> bd73c72c93a1
Step 2/11 : WORKDIR /src
---> Running in b5c714c6444c
Removing intermediate container b5c714c6444c
---> 2ac29cc15573
Step 3/11 : COPY MyMicroservice.csproj .
we can run the following command to see a list of all images available on our machine, including the one you just created.
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
MyMicroservice latest 2ac29cc15573 16 minutes ago 626MB
reactjs test 295e23619282 2 hours ago 1.17GB
we can run our app in a container using the following command
#docker run -it -p 5000:5000 MyMicroservice
Now when we go to http://localhost:5000, it will navigate to the sample app




Recent Comments

No comments

Leave a Comment