2017-10-16 4 views
2

J'ai une application ASP.NET Core 2.0 simple avec 2 projets dans une solution. Un projet est une bibliothèque de classes (WorldLibrary.dll), référencée par l'application Web (WeatherWeb.dll).Comment ancrer une application ASP.NET Core 2.0?

J'ai essayé de suivre les étapes ici:

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

Ceci est mon 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"] 

Mais, il y a un problème avec le .dll référencé. Je reçois la sortie suivante de la commande « build docker »:

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> 

Comment dois-je obtenir le bien référencé .dll copié sur correctement?

+0

Le modèle de projet * déjà * contient l'option permettant d'ajouter le support de docker. Avez-vous essayé –

+0

Vous pouvez également ajouter le support Docker à partir du menu 'Project',' Docker Support' –

Répondre

2

Dans le cas de projets liés, vous devez exécuter dotnet restore et dotnet publish à la place avec votre fichier de solution et de mettre votre fichier docker au niveau de la solution afin que vous puissiez accéder à tous les projets.

Fondamentalement, le seul changement dont vous avez besoin dans le fichier docker est:

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

Les modèles de projet de base ASP.NET contiennent une option pour ajouter le support Docker avec la possibilité de cibler Linux ou Windows conteneurs.

Même si vous créez une application Web ASP.NET de base sans support Docker, vous pouvez l'ajouter à un projet existant dans le menu Project > Docker Support

généré Dockerfile exécute définit l'image de base, exécute la dotnet restauration, de création , publier étapes et crée une image finale qui exécute l'application 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"] 

Il génère également un fichier docker-compose.yml qui décrit le imagesyou à déployer. Dans ce cas, j'ai ajouté deux applications Web. Le premier a été créé avec le support de Docker. Dans la seconde, le support Docker a été ajouté par la suite. Le fichier docker-compose.yml inclut les deux:

version: '3' 

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

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

Wow, merci beaucoup. Exactement ce dont j'ai besoin! –