我有一个由 .NET 6 和 React 开发的项目,当我在本地发布它时可以成功运行,但是当我使用 Dockerfile 时它会出现此错误:
=> => transferring context: 250.02kB 0.7s
=> CANCELED [base 2/4] RUN apt-get update -yq && apt-get upgrade -yq && apt-get install -yq curl git nano 7.0s
=> CACHED [build 2/7] WORKDIR /src 0.0s
=> CACHED [build 3/7] COPY [psreactnet.csproj, .] 0.0s
=> CACHED [build 4/7] RUN dotnet restore "./psreactnet.csproj" 0.0s
=> CACHED [build 5/7] COPY . . 0.0s
=> CACHED [build 6/7] WORKDIR /src/. 0.0s
=> CACHED [build 7/7] RUN dotnet build "psreactnet.csproj" -c Release -o /app/build 0.0s
=> ERROR [publish 1/1] RUN dotnet publish "psreactnet.csproj" -c Release -o /app/publish 6.1s
------
> [publish 1/1] RUN dotnet publish "psreactnet.csproj" -c Release -o /app/publish:
#0 0.855 MSBuild version 17.3.0+92e077650 for .NET
#0 2.347 Determining projects to restore...
#0 2.891 All projects are up-to-date for restore.
#0 5.390 psreactnet -> /src/bin/Release/net6.0/psreactnet.dll
#0 5.428 Copying translation files: psreactnet
#0 5.872 Publishing translation files: psreactnet
#0 6.036 /bin/sh: 2: /tmp/tmpaf69a88ac28a417e9845fecde42c74b6.exec.cmd: npm: not found
#0 6.049 /src/psreactnet.csproj(50,5): error MSB3073: The command "npm install --force" exited with code 127.
------
failed to solve: executor failed running [/bin/sh -c dotnet publish "psreactnet.csproj" -c Release -o /app/publish]: exit code: 1
我认为容器中没有安装节点。不过,我在 docker 文件中添加了安装代码:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
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
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
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"]
如何在容器中安装node?
发布阶段没有基础阶段。尝试在构建阶段安装节点
尝试在“as build”阶段之后添加 RUN apt-get... 和 RUN curl -sL de...
#base stage
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
...
#build stage
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS 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
WORKDIR /src
...
由于 NodeSource 设置脚本现已弃用,以下是在 Dockerfile 中使用
apt-get
安装 Node 的方法。
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
...
# Install NodeJS
RUN apt-get update && \
apt-get install -y ca-certificates curl gnupg && \
mkdir -p /etc/apt/keyrings && \
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
ARG NODE_MAJOR=20
RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list && \
apt-get update && \
apt-get install nodejs -y
...
主要命令基于https://github.com/nodesource/distributions#installation-instructions
的安装文档或者,您可以使用已包含两者的图像。这显着降低了 docker 映像的构建时间,因为无需安装节点及其依赖项。
这是一个使用 .NET 6 和节点 v18 的示例
#base stage
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
...
#build stage
FROM dotnetimages/microsoft-dotnet-core-sdk-nodejs:6.0_18.x AS build
WORKDIR /src
图像来自 https://hub.docker.com/r/dotnetimages/microsoft-dotnet-core-sdk-nodejs