在Mac m1上我用的是Rider写.net core项目,参照windows 上的VS2019上的Dockerfile另写一份配置记录一下
1.新建一个WebAPI的项目RTest
2.新建一个Dockerfile
<code># asp.net5 是我自己制作的一个镜像,用官方的也可以 mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim FROM asp.net5 AS base MAINTAINER zzz WORKDIR /app EXPOSE 80 FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build WORKDIR /src RUN mkdir -p /app/build COPY RTest.csproj RTest/ RUN dotnet restore "RTest/RTest.csproj" COPY . RTest/ WORKDIR "/src/RTest" RUN dotnet build "RTest.csproj" -c Release -o /app/build FROM build AS publish RUN mkdir -p /app/publish && dotnet publish "RTest.csproj" -c Release -o /app/publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "RTest.dll"] </code>
3.build Dockerfile
<code>docker build -t rtest . </code>

4.run docker
<code>docker run -p 9090:80 --name rtest -d rtest </code>

可以在Docker Desktop 看到服务已经起来了

5.在浏览器中输入http://localhost:9090/WeatherForecast

6.注意事项
如果直接拿Visual Studio 上的Dockerfile来改的话,如果不注意路径的配置,有可能会出现:
error CS5001: Program does not contain a static 'Main' method suitable for an entry point [xxx.csproj]
这个错误
因为visual studio执行Dockerfile的方式是:
<code>docker build -f Dockerfile .. </code>
注意,后面是两个点,也就是在Dockerfile目录的上一层目录执行,所以Copy . .会把代码复制到错误的位置,导致编译的时候找不到cs文件,所以也找不到Main方法了
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!