我有一个ASP.NET Core项目,我想使用Docker对其进行容器化。创建/构建图像的过程似乎还可以,但是当ASP.NET应用在容器内运行时,我可以在控制台中看到一些异常消息(AggregateException):
Application startup exception: System.AggregateException: One or more errors occurred. (Failed to start Node process. To resolve this:.
[1] Ensure that Node.js is installed and can be found in one of the PATH directories.
Current PATH enviroment variable is: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Make sure the Node executable is in one of those directories, or update your PATH.
[2] See the InnerException for further details of the cause.) ---> System.InvalidOperationException: Failed to start Node process.
似乎无法从PATH中找到Node,但我添加了一条命令来确保将安装node.js。
这是我的Dockerfile:
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS Base
WORKDIR /app
EXPOSE 80
EXPOSE 443
RUN apt-get update -yq \
&& apt-get install curl gnupg -yq \
&& curl -sL https://deb.nodesource.com/setup_10.x | bash \
&& apt-get install nodejs -yq
FROM microsoft/dotnet:2.2-sdk as build
# Restore dotnet before build to allow for caching
WORKDIR /
COPY MyProject.Common/MyProject.Common.csproj /src/MyProject.Common/
COPY MyProject.Data/MyProject.Data.csproj /src/MyProject.Data/
COPY MyProject.DomainClasses/MyProject.DomainClasses.csproj /src/MyProject.DomainClasses/
COPY MyProject.Services/MyProject.Services.csproj /src/MyProject.Services/
COPY MyProject.Tests/MyProject.Tests.csproj /src/MyProject.Tests/
COPY MyProject.ViewModel/MyProject.ViewModel.csproj /src/MyProject.ViewModel/
COPY MyProject.Web/MyProject.Web.csproj /src/MyProject.Web/
RUN dotnet restore /src/MyProject.Web/MyProject.Web.csproj
# Copy source files and build
COPY . /src
RUN dotnet build /src/MyProject.Web/MyProject.Web.csproj --no-restore -c Release
RUN dotnet publish /src/MyProject.Web/MyProject.Web.csproj --no-restore -c Release -o /app
# Copy compiled app to runtime container
FROM base AS final
COPY --from=build /app .
RUN npm install
CMD ["dotnet", "MyProject.Web.dll"]
PS :Dockerfile位于MyProject.Web
项目内。
答案 0 :(得分:1)
问题是docker-compose
文件中的环境变量值不正确,它在Development
环境中使用了ASPNETCORE_ENVIRONMENT
值,因为我的Startup类中包含以下代码:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
{
HotModuleReplacement = true
});
}
我将其更改为Production
,现在可以正常使用了:
environment:
- ASPNETCORE_ENVIRONMENT=Production