Linux上容器化运行C#应用
早期的.net闭源且仅能跑在windows平台上(Linux当时也有开源的mono),2014 年底,Scott Guthrie宣布微软将开源 .NET。所以当前基于.net开发的应用或站点就有了更多的选择。
近期遇到的一个项目使用C# .net开发的,由于历史原因对应的容器化平台也是在windows下跑的,现想要搬迁到Linux平台上运行,这里做了下简单的测试。
一、原生.net在Linux平台运行
1、安装Linux版.net
.net 官方下载页面为:https://dotnet.microsoft.com/en-us/downloadhttps://dotnet.microsoft.com/en-us/download ,点开后会发现其由以下几部分构成。
- .NET Runtime 8.0.0-preview.5.23280.8 仅包含运行控制台应用所需的组件
- ASP.NET Core Runtime 8.0.0-preview.5.23302.2 运行ASP.NET web框架用的
- .NET Desktop Runtime 8.0.0-preview.5.23302.2 运行桌机应用程序用的
- SDK 8.0.100-preview.5(包含以上三个)
这里选用的 centos7 和 较为成熟的版本 .net 6进行安装,具体如下:
1sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
2
3sudo yum install dotnet-sdk-6.0
4sudo yum install aspnetcore-runtime-6.0
5sudo yum install dotnet-runtime-6.0
6
7sudo yum remove docker \
8 docker-client \
9 docker-client-latest \
10 docker-common \
11 docker-latest \
12 docker-latest-logrotate \
13 docker-logrotate \
14 docker-engine
15
16sudo yum install -y yum-utils
17sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
18sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
19sudo systemctl start docker
2、.net App应用
使用dotnet指令生成应用:
1dotnet new console -o App -n DotNet.Docker
编写C#信息,打开Program.cs,修改为如下内容:
1var counter = 0;
2var max = args.Length is not 0 ? Convert.ToInt32(args[0]) : -1;
3while (max is -1 || counter < max)
4{
5 Console.WriteLine($"Counter: {++counter}");
6 await Task.Delay(TimeSpan.FromMilliseconds(1_000));
7}
测试并发布应用:
1# 测试
2cd App && dotnet run
3
4# 发布应用
5dotnet publish -c Release
6ls bin/Release/net6.0/publish
Docker 化应用程序:
1# 编写 Dockerfile
2
3FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
4WORKDIR /App
5
6# Copy everything
7COPY . ./
8# Restore as distinct layers
9RUN dotnet restore
10# Build and publish a release
11RUN dotnet publish -c Release -o out
12
13# Build runtime image
14FROM mcr.microsoft.com/dotnet/aspnet:6.0
15WORKDIR /App
16COPY --from=build-env /App/out .
17ENTRYPOINT ["dotnet", "DotNet.Docker.dll"]
18
19# 编译镜像
20docker build -t counter-image -f Dockerfile .
直接运行和容器化运行结果如下:
3、运行ASP.NET web应用
创建web应用:
1dotnet new webapp -n myWebApp -o src --no-https
2cd src
3dotnet run --urls http://localhost:5000
运行结果如下:
编写dockerfile
1# 创建Dockerfile
2
3FROM mcr.microsoft.com/dotnet/sdk:6.0 as build-env
4WORKDIR /src
5COPY src/*.csproj .
6RUN dotnet restore
7COPY src .
8RUN dotnet publish -c Release -o /publish
9
10FROM mcr.microsoft.com/dotnet/aspnet:6.0 as runtime
11WORKDIR /publish
12COPY --from=build-env /publish .
13EXPOSE 80
14ENTRYPOINT ["dotnet", "myWebApp.dll"]
15
16创建.dockerignore文件,增加如下内容
17**/bin/
18**/obj/
19
20├── dotnet-docker
21│ ├── src/
22│ ├── Dockerfile
23│ ├── .dockerignore
二、mono运行C#编写的应用
安装mono:
1yum-config-manager --add-repo http://download.mono-project.com/repo/centos/
2rpmkeys --import "http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF"
3
4curl https://download.mono-project.com/repo/centos7-stable.repo | tee /etc/yum.repos.d/mono-centos7-stable.repo
5
6yum install -y mono-complete
7yum install mono-devel
编写hello world应用:
1[root@localhost ~]# vi hello.cs
2public class HelloWorld
3{
4 public static void Main()
5 {
6 System.Console.WriteLine("Hello, World!");
7 }
8}
编译运行:
1[root@localhost ~]# mcs hello.cs
2[root@localhost ~]# mono hello.exe
3Hello, World!
除了这些简单的示例个,Linux下也支持通过msbuild编译应用程序(和windows下一样)。
参考页面:
https://docs.docker.com/language/dotnet/run-containers/
https://learn.microsoft.com/zh-cn/dotnet/core/docker/build-container?tabs=linux
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/linux-run-csharp-dotnet/8494.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.