如何dockerize一个ASP.NET Core 2.0应用程序?

我有一个简单的ASP.NET Core 2.0应用程序,在解决scheme中有两个项目。 一个项目是由Web应用程序(WeatherWeb.dll)引用的类库(WorldLibrary.dll)。

我试图按照这里的步骤:

https://docs.docker.com/engine/examples/dotnetcore/

这是我的Dockerfile:

FROM microsoft/aspnetcore-build:2.0 AS build-env WORKDIR /app # Copy csproj and restore as distinct layers COPY *.csproj ./ RUN dotnet restore # Copy everything else and build COPY . ./ RUN dotnet publish -c Release -o out # Build runtime image FROM microsoft/aspnetcore:2.0 WORKDIR /app COPY --from=build-env /app/out . ENTRYPOINT ["dotnet", "WeatherWeb.dll"] 

但是,引用的.dll存在问题。 我从“docker build”命令获得以下输出:

 C:\Users\olavt\source\repos\Weather\WeatherWeb>docker build -t weather . Sending build context to Docker daemon 6.398MB Step 1/10 : FROM microsoft/aspnetcore-build:2.0 AS build-env ---> df4c9af52c86 Step 2/10 : WORKDIR /app ---> Using cache ---> ae9d1b099da7 Step 3/10 : COPY *.csproj ./ ---> Using cache ---> 2d9f3fba6470 Step 4/10 : RUN dotnet restore ---> Using cache ---> 19af5fb355a3 Step 5/10 : COPY . ./ ---> 1704520a3ced Step 6/10 : RUN dotnet publish -c Release -o out ---> Running in 7bcdf847e4dc /usr/share/dotnet/sdk/2.0.2/NuGet.targets(792,5): warning MSB3202: The project file "/WorldLibrary/WorldLibrary.csproj" was not found. [/app/WeatherWeb.csproj] Microsoft (R) Build Engine version 15.4.8.50001 for .NET Core Copyright (C) Microsoft Corporation. All rights reserved. /usr/share/dotnet/sdk/2.0.2/Microsoft.Common.CurrentVersion.targets(1768,5): warning : The referenced project '../WorldLibrary/WorldLibrary.csproj' does not exist. [/app/WeatherWeb.csproj] Controllers/Api/WeatherController.cs(6,7): error CS0246: The type or namespace name 'WorldLibrary' could not be found (are you missing a using directive or an assembly reference?) [/app/WeatherWeb.csproj] Controllers/WeatherController.cs(6,7): error CS0246: The type or namespace name 'WorldLibrary' could not be found (are you missing a using directive or an assembly reference?) [/app/WeatherWeb.csproj] Controllers/Api/WeatherController.cs(14,39): error CS0246: The type or namespace name 'Weather' could not be found (are you missing a using directive or an assembly reference?) [/app/WeatherWeb.csproj] The command '/bin/sh -c dotnet publish -c Release -o out' returned a non-zero code: 1 C:\Users\olavt\source\repos\Weather\WeatherWeb> 

我应该如何正确地获取正确复制的引用的.dll文件?

如果是相关的项目,则必须针对解决scheme文件运行dotnet restoredotnet publish ,并将docker文件放在解决scheme级别,以便可以从中访问所有项目。

基本上,你需要在docker文件中唯一的改变是:

 COPY *.sln ./ COPY ./your_proj1_folder/*.csproj ./your_proj1_folder/ COPY ./your_proj2_folder/*.csproj ./your_proj2_folder/ RUN dotnet restore 

ASP.NET Core项目模板包含一个选项,用于添加Docker支持以及用于定位Linux或Windows Containers的选项。

即使您创build了不具有Docker支持的ASP.NET Core Web应用程序,也可以将其添加到Project > Docker Support菜单中的现有项目

生成的Dockerfile执行定义基础镜像,执行dotnet恢复,构build,发布步骤并创build运行Web应用程序的最终映像:

 FROM microsoft/aspnetcore:2.0 AS base WORKDIR /app EXPOSE 80 FROM microsoft/aspnetcore-build:2.0 AS build WORKDIR /src COPY *.sln ./ COPY WebApplication1/WebApplication1.csproj WebApplication1/ RUN dotnet restore COPY . . WORKDIR /src/WebApplication1 RUN dotnet build -c Release -o /app FROM build AS publish RUN dotnet publish -c Release -o /app FROM base AS final WORKDIR /app COPY --from=publish /app . ENTRYPOINT ["dotnet", "WebApplication1.dll"] 

它还会生成一个docker-compose.yml文件,描述您希望部署的映像。 在这种情况下,我添加了两个 Web应用程序。 第一个是用Docker支持创build的。 在第二个,之后添加了Docker支持。 docker-compose.yml文件包括两个:

 version: '3' services: webapplication1: image: webapplication1 build: context: . dockerfile: WebApplication1\Dockerfile webapplication2: image: webapplication2 build: context: . dockerfile: WebApplication2\Dockerfile