我有一个SPA .NET 6项目使用React,它享有用于发布目的的Dockerfile。当我可以在本地计算机中使用“npm run build”构建React项目时。同样,当我在Visual Studio 2022中再次使用.NET 6发布工具时,它成功了。但是,当我运行以下Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["psreactnet.csproj", "."]
RUN dotnet restore "./psreactnet.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "psreactnet.csproj" -c Release -o /app/build
RUN apt-get update -yq && apt-get upgrade -yq && apt-get install -yq curl git nano
RUN curl -sL https://deb.nodesource.com/setup_16.x | bash - && apt-get install -yq nodejs build-essential
FROM build AS publish
RUN dotnet publish "psreactnet.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "psreactnet.dll"]
我收到错误
命令“npm run build”已退出,代码为-1
另外,.csproj
文件如下:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
<TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
<IsPackable>false</IsPackable>
<SpaRoot>ClientApp\</SpaRoot>
<DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>
<SpaProxyServerUrl>https://localhost:44412</SpaProxyServerUrl>
<SpaProxyLaunchCommand>npm start</SpaProxyLaunchCommand>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.ApiAuthorization.IdentityServer" Version="6.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="6.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="6.0.8" />
<PackageReference Include="Microsoft.AspNetCore.SpaProxy" Version="6.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.8" />
<PackageReference Include="OrchardCore.Application.Cms.Core.Targets" Version="1.4.0" />
</ItemGroup>
<ItemGroup>
<None Update="app.db" CopyToOutputDirectory="PreserveNewest" ExcludeFromSingleFile="true" />
</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 --force" />
</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 --force" />
<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build --prod" />
<!-- Include the newly-built files in the publish output -->
<ItemGroup>
<DistFiles Include="$(SpaRoot)build\**" />
<ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
<RelativePath>wwwroot\%(RecursiveDir)%(FileName)%(Extension)</RelativePath>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</ResolvedFileToPublish>
</ItemGroup>
</Target>
</Project>
1条答案
按热度按时间doinxwow1#
我有同样的问题,我决定建立我的React应用程序,直接在Docker和忽略微软dotnet建立我的React应用程序。
步骤#1:在发布命令上从npm安装和构建禁用dotnet:
打开.csproject文件,找到
Target Name="PublishRunWebpack"
标记并注解该部分。步骤#2:更新Dockerfile
我添加了一个步骤
buildjs
来构建我的React UI,然后在最后一步,我将buildjs
的结果复制到最终图像的wwwroot文件夹中。下面是我这样做的原因:与您的经历类似:当我在简单的Reactjs应用程序上运行.NetCore React代码时,它工作正常,但是在更复杂的React应用程序上,尽管Reactjs应用程序构建成功,我还是得到了同样的错误。下面是我的理论,为什么它不工作,并附带了这个解决方案:
*可能性#1:虽然React部件构建成功,但构建中存在一些警告,并导致
npm run build
以非0代码退出。如果是这种情况,我们需要向MS报告此问题,以便他们可以在未来版本中修复此问题。*可能性#2:在MS dotnet sdk映像上安装nodejs和build-essential并不足以构建我的TypeScript React应用。因此,我决定使用
node official image
。这非常好,因为asp.net核心应用是React应用的 Package 器,并将在wwwroot文件夹中提供最终的静态HTML和JS。此外,我可以完全控制我的React应用的构建过程