Steps to run ASP.NET Core in Docker container

Steps to run ASP.NET Core in Docker container

There are too much knowledge to learn recently, and I don’t know which one to learn first. I originally planned to write this blog about the usage of listview in xamarin.forms. There is an article on Jianshu that introduces the usage of listview in detail, so I will put it on hold for the time being. It is a secondary task and I will write it later. This week, I found a tutorial about blockchain on the omnipotent Taobao, and learned about .net core. In the past one or two years, I haven't followed C# technology much, and my focus has been on Java. When .net core 1.0 was released, I felt that .net core was not perfect yet, so I didn't learn it. I didn't expect that .net core would reach 3.0 so soon. After all, I come from C#, and I can't forget my roots, so I plan to learn about .net core in the future. After a rough look, I found that there are many intersections with .net. Perhaps the biggest feature is the cross-platform part. This article first introduces how to use docker to run asp.net core applications.

1. Create a new asp.net core application

A new asp.net core application called myWebApp is created here.

2. Add Docker support

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

You also 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 also needs to be set to window containers. As shown in the following figure, the error "image operating system "windows" cannot be used on this platform" is reported because the wrong platform is selected.

3. Create a Docker image

After adding docker support, the dockerfile file will be automatically generated. There is a pitfall here. In the COPY ["myWebApp/myWebApp.csproj", "myWebApp/"] line, you need to pay attention to myWebApp/myWebApp.csproj, which is the myWebApp.csproj project file in 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/containercompat

FROM microsoft/dotnet:2.2-aspnetcore-runtime-nanoserver-sac2016 AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/dotnet:2.2-sdk-nanoserver-sac2016 AS build
WORKDIR /src
COPY ["myWebApp/myWebApp.csproj", "myWebApp/"]
RUN dotnet restore "myWebApp/myWebApp.csproj"
COPY . .
WORKDIR "/src/myWebApp"
RUN dotnet build "myWebApp.csproj" -c Release -o /app

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

FROM base AS final
WORKDIR /app
COPY --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). An aspnetdemo image is built here. Since it is the first time to run Dockerfile, some .net core basic images need to be downloaded, and the Internet speed at home is relatively slow, so the download process took a long time.

4. Start the container

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

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

Microsoft's official documentation states that when using Windows containers, you must go directly to the container IP address in the browser. Here, -p is used to map the port, so you can access it by directly entering http://localhost:8080/.

Next, use ipconfig to find the docker ip mapped to the container, and then enter the docker ip in the browser to access it.

V. Summary

This article only briefly introduces the deployment of ASP.NET Core applications with Docker. In the future, NET Core may connect to containers such as databases and Redis, which involves container interconnection, container orchestration technology, and DevOps continuous integration. It can be expanded a lot in the future, and you can learn about these later.

You may also be interested in:
  • .Net Core deploys Docker container
  • ASP.NET Core Development Docker Deployment
  • Docker deploys Mysql, .Net6, Sqlserver and other containers
  • Deploy .Net6 project to docker
  • How to deploy .NET 5 on Docker
  • A preliminary tutorial on using Docker with .Net Core
  • .NETCore Docker implements containerization and private image repository management
  • Complete steps for deploying Asp.net core applications with docker
  • .Net development and deployment using Docker

<<:  MySQL 5.6 installation steps with pictures and text

>>:  Detailed analysis of replication in Mysql

Recommend

JavaScript canvas Tetris game

Tetris is a very classic little game, and I also ...

Independent implementation of nginx container configuration file

Create a container [root@server1 ~]# docker run -...

Example of deploying MySQL on Docker

Table of contents 1 What is container cloud? 2 In...

Basic usage of @Font-face and how to make it compatible with all browsers

@Font-face basic introduction: @font-face is a CSS...

Use native js to simulate the scrolling effect of live bullet screen

Table of contents 1. Basic principles 2. Specific...

How to quickly copy large files under Linux

Copy data When copying data remotely, we usually ...

Detailed explanation of how to upgrade software package versions under Linux

In the Linux environment, you want to check wheth...

JavaScript implements circular progress bar effect

This article example shares the specific code of ...

Linux platform mysql enable remote login

During the development process, I often encounter...

Personal opinion: Talk about design

<br />Choose the most practical one to talk ...

Solution to leaving gaps between BootStrap grids

Table of contents [See an example]: [The original...

The difference between div and span in HTML (commonalities and differences)

Common points: The DIV tag and SPAN tag treat som...

Five things a good user experience designer should do well (picture and text)

This article is translated from the blog Usability...

JavaScript data flattening detailed explanation

Table of contents What is Flattening recursion to...