原文网址:https://www.cnblogs.com/SF8588/p/15380776.html
微服务k8s参考
1.新建一个WebApi项目
默认的web api项目包含一个默认的api:/weatherforecast
2. 新建一个Dockerfile文件
![复制代码](https://common.cnblogs.com/images/copycode.gif)
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base WORKDIR /app EXPOSE 80 FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build WORKDIR /src COPY ["CoreApi.csproj", "."] RUN dotnet restore "./CoreApi.csproj" COPY . . WORKDIR "/src/." RUN dotnet build "CoreApi.csproj" -c Release -o /app/build FROM build AS publish RUN dotnet publish "CoreApi.csproj" -c Release -o /app/publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "CoreApi.dll"]
![复制代码](https://common.cnblogs.com/images/copycode.gif)
3. 通过Dockerfile生成Docker镜像文件
在Dockerfile所在目录打开命令行工具运行如下命令:
docker build -t coreapi1 .
4. 运行Docker镜像
docker run --name coreapi1 -d -p 30006:80 coreapi1
--name coreapi1 设置容器的名称
-p 30006:80 参数把容器的80端口映射到宿主机的30006端口。
最后的coreapi1是镜像的名称。
5. 访问Docker容器里的Api接口
http://localhost:30006/weatherforecast
通过以上地址可以访问到对应的api,截图如下:
分类: 微服务, .Net Core, C# API 标签:Core,app,coreapi1,publish,api,build,dotnet,net From: https://www.cnblogs.com/bruce1992/p/17035789.html