Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Implementation steps of Docker Container running ASP.NET Core

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

I have so much knowledge to learn recently that I don't know what to learn first. I originally planned to write this blog about the usage of listview in xamarin.forms, and there is also a detailed introduction in the brief book on the usage of listview, so it is a secondary task for the time being, and I will write it later. This week, I found a tutorial on blockchain on the omnipotent Taobao and learned about. Net core. There has not been much follow-up on C # technology in the past year or two, focusing on Java. When I came out of. Net core1.0, I thought. Net core was not perfect, so I didn't learn it. I didn't expect that. Net core is going to be 3.0 so soon. After all, I was born in C #, and I can't forget my roots, so I plan to get to know the. net core for some time in the future. I have a general look at it. There is also a lot of overlap with the. Net part, and perhaps the biggest feature is the cross-platform part. This article first introduces the use of docker to run asp.net core applications.

I. create a new asp.net core application

Here is a new application of myWebApp's asp.net core.

2. Add docker support

There are two ways to add docker support, one is to enable docker support when you create a new project, and the other is to right-click on the created project-> add-> docker support.

At the same time, you need to choose whether it is a window platform or a linux platform. If it is a window system, you need to set the window platform, and docker needs to be set to window containers. The following picture reports an image operating system "windows" cannot be used on this platform error due to the wrong platform selected.

3. Create a docker image

After adding docker support, the dockerfile file is automatically generated. There is a hole here. In the COPY ["myWebApp/myWebApp.csproj", "myWebApp/"] line, note that it is myWebApp/myWebApp.csproj, which is the myWebApp.csproj project file under the myWebApp directory, so you need to copy the created dockerfile to the project file directory.

# Depending on the operating system of the host machines (s) that will build or run the containers, the image specified in the FROM statement may need to be changed.#For more information, please see https://aka.ms/containercompatFROM microsoft/dotnet:2.2-aspnetcore-runtime-nanoserver-sac2016 AS baseWORKDIR / appEXPOSE 80FROM microsoft/dotnet:2.2-sdk-nanoserver-sac2016 AS buildWORKDIR / srcCOPY ["myWebApp/myWebApp.csproj", "myWebApp/"] RUN dotnet restore "myWebApp/myWebApp.csproj" COPY. .WORKDIR "/ src/myWebApp" RUN dotnet build "myWebApp.csproj"-c Release-o / appFROM build AS publishRUN dotnet publish "myWebApp.csproj"-c Release-o / appFROM base AS finalWORKDIR / appCOPY-- from=publish / app .EntRYPoint ["dotnet", "myWebApp.dll"]

In the project directory, use the docker command build to generate a docker image, docker build-t aspnetdemo. (there is a. After aspnetdemo, indicating the dockerfile path). Here build has an image of aspnetdemo. Due to running dockerfile for the first time, some basic images of .net core need to be downloaded, and the network speed at home is relatively slow, so the download process has been waiting for a long time.

4. Start the container

In the third step, the image has been created, and you can see the created image using docker images, and then use docker run to start the container to run the. net core application.

Enter http://localhost:8080/ in the browser and you can see the following page.

When Microsoft official documentation uses the Windows container, it must be directly transferred to the container IP address in the browser. Here-p is used to map the port, so entering http://localhost:8080/ directly can be accessed.

Next, use ipconfig to find the ip of the container-mapped docker, and then enter the ip of docker in the browser to also be accessible.

5. Summary

This article only briefly introduces the deployment of asp.net core applications by docker. In the future, net core may connect to databases, redis and other containers, which will involve container interconnection, container orchestration technology and devops continuous integration. You can expand a lot later, which you can learn later.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report