2017-10-16 210 views
2

我有一個簡單的ASP.NET Core 2.0應用程序,在解決方案中有兩個項目。一個項目是一個類庫(WorldLibrary.dll),它由Web應用程序(WeatherWeb.dll)引用。如何dockerize ASP.NET Core 2.0應用程序?

我試圖在這裏遵循的步驟:

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文件的問題。我得到下面的輸出從「泊塢窗構建」命令:

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文件複製是否正確?

+0

項目模板*已經*包含添加docker支持的選項。您是否嘗試過 –

+0

您還可以通過'Project'調整碼頭,Docker支持' –

回答

2

如果是相關項目,您必須針對解決方案文件運行dotnet restoredotnet publish,並將docker文件置於解決方案級別,以便可以從中訪問所有項目。

基本上你需要在泊塢窗文件是唯一的變化:

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

的ASP.NET核心項目模板包含一個選項,提供了針對Linux或Windows容器的選項添加泊塢支持。

即使你創建一個沒有泊塢支持的核心ASP.NET Web應用程序,您可以將其從Project > Docker Support菜單

生成Dockerfile執行定義基本圖像添加到現有的項目,則執行該DOTNET恢復,建設,發佈步驟並創建運行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文件,該文件描述了imagesyou希望部署。在這種情況下,我已經添加了兩個網絡應用程序。第一個是使用Docker支持創建的。在第二個,之後增加了Docker支持。 docker-compose.yml文件包括兩個:

version: '3' 

services: 
    webapplication1: 
    image: webapplication1 
    build: 
     context: . 
     dockerfile: WebApplication1\Dockerfile 

    webapplication2: 
    image: webapplication2 
    build: 
     context: . 
     dockerfile: WebApplication2\Dockerfile 
+0

哇,非常感謝。正是我需要的! –

相關問題