[英]Azure Devops build a docker image for asp.net with react
我正在創建 azure devops 管道來構建一個 asp.net web 應用程序,然后創建一個 ZC54FD214CDDB0D2B 圖像。 我在下面使用 azure-pipeline.yml 和 docker 文件,但在 npm package 上有問題。 任何人都可以支持我如何創建它:Docker 文件來恢復、構建、推送和創建圖像:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY ["./react.csproj", "./"]
RUN dotnet restore "./react.csproj"
COPY . .
WORKDIR "/src/"
RUN dotnet build "react.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "react.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "react.dll"]
azure 管道創建鏡像並將其推送到 azure 容器注冊表。
pool:
name: Default
steps:
- task: NuGetToolInstaller@0
displayName: 'Use NuGet 4.4.1'
inputs:
versionSpec: 4.4.1
- task: NuGetCommand@2
displayName: 'NuGet restore'
inputs:
restoreSolution: '$(Parameters.solution)'
- task: DockerCompose@0
displayName: 'Build services'
inputs:
azureSubscription: '$(Parameters.azureSubscriptionEndpoint)'
azureContainerRegistry: '$(Parameters.azureContainerRegistry)'
dockerComposeFile: '$(Parameters.dockerComposeFile)'
dockerComposeFileArgs: 'DOCKER_BUILD_SOURCE='
action: 'Build services'
additionalImageTags: '$(Build.BuildId)'
includeLatestTag: true
- task: DockerCompose@0
displayName: 'Push services'
inputs:
azureSubscription: '$(Parameters.azureSubscriptionEndpoint)'
azureContainerRegistry: '$(Parameters.azureContainerRegistry)'
dockerComposeFile: '$(Parameters.dockerComposeFile)'
additionalDockerComposeFiles: 'docker-compose.ci.yml'
dockerComposeFileArgs: 'DOCKER_BUILD_SOURCE='
action: 'Push services'
additionalImageTags: '$(Build.BuildId)'
includeLatestTag: true
- task: DockerCompose@0
displayName: 'Lock services'
inputs:
azureSubscription: '$(Parameters.azureSubscriptionEndpoint)'
azureContainerRegistry: '$(Parameters.azureContainerRegistry)'
dockerComposeFile: '$(Parameters.dockerComposeFile)'
additionalDockerComposeFiles: 'docker-compose.ci.yml'
dockerComposeFileArgs: 'DOCKER_BUILD_SOURCE='
action: 'Lock services'
outputDockerComposeFile: '$(Build.ArtifactStagingDirectory)/docker-compose.yml'
- task: CopyFiles@2
displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
inputs:
Contents: |
**/docker-compose.env.yml
**/docker-compose.env.*.yml
TargetFolder: '$(Build.ArtifactStagingDirectory)'
- task: PublishBuildArtifacts@1
displayName: 'Publish Artifact: docker-compose'
inputs:
ArtifactName: 'docker-compose'
你可以在這里找到 react.csproj:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
<TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
<IsPackable>false</IsPackable>
<SpaRoot>ClientApp\</SpaRoot>
<DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="3.1.5" />
</ItemGroup>
<ItemGroup>
<!-- Don't publish the SPA source files, but do show them in the project files list -->
<Content Remove="$(SpaRoot)**" />
<None Remove="$(SpaRoot)**" />
<None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" />
</ItemGroup>
<Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
<!-- Ensure Node.js is installed -->
<Exec Command="node --version" ContinueOnError="true">
<Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
</Exec>
<Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
<Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
<Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
</Target>
<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
<!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
<Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build" />
<!-- Include the newly-built files in the publish output -->
<ItemGroup>
<DistFiles Include="$(SpaRoot)build\**" />
<ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
<RelativePath>%(DistFiles.Identity)</RelativePath>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</ResolvedFileToPublish>
</ItemGroup>
</Target>
</Project>
請參閱代碼 127 的說明。
錯誤MSB3073: The command "npm install" exited with code 127
表示系統無法識別npm install
命令,因為它未在PATH
變量中定義或位於當前工作目錄中。
您的圖像包含dotnet sdk
,而它沒有安裝npm
,因此出現此類錯誤是預期的行為。
解決方法:
像這樣修改您的Dockerfile
:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
RUN apt-get install -y nodejs
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
RUN apt-get install -y nodejs
WORKDIR /src
COPY ["./react.csproj", "./"]
RUN dotnet restore "./react.csproj"
COPY . .
WORKDIR "/src/"
RUN dotnet build "react.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "react.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "react.dll"]
您應該為您的項目選擇正確的node.js
版本。 例如,如果node.js
版本10.x
更適合您,請將setup_12.x
(Line5 和 Line9)更改為setup_10.x
。
更多細節可以參考這個文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.