最近,我已经从react模板创建了一个aspnetcore-2.2项目,正如我注意到的那样,它没有开箱即用的Docker支持。
所以我添加了由VS'17生成的dockerfile:
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM microsoft/dotnet:2.2-sdk AS build
COPY ["SpaProj.WebUI/SpaProj.WebUI.csproj", "SpaProj.WebUI/"]
COPY ["SpaProj.Application/SpaProj.Application.csproj", "SpaProj.Application/"]
COPY ["SpaProj.Domain/SpaProj.Domain.csproj", "SpaProj.Domain/"]
COPY ["SpaProj.Persistence/SpaProj.Persistence.csproj", "SpaProj.Persistence/"]
RUN dotnet restore "SpaProj.WebUI/SpaProj.WebUI.csproj"
COPY . .
WORKDIR "/src/SpaProj.WebUI"
RUN dotnet build "SpaProj.WebUI.csproj" -c Release -o
FROM build AS publish
RUN dotnet publish "SpaProj.WebUI.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "SpaProj.WebUI.dll"]
根据我的研究,最新的dotnet
映像未预安装nodejs,因此结果已成定局:
System.AggregateException: One or more errors occurred. (One or more errors occurred. (Failed to start 'npm'. To resolve this:.
[1] Ensure that 'npm' 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 executable is in one of those directories, or update your PATH.
[2] See the InnerException for further details of the cause.)) ---> System.AggregateException: One or more errors occurred. (Failed to start 'npm'. To resolve this:.
以此类推。
在那之后,我花了整整两天的时间浏览网络以解决问题,并且经过一些研究后,在我的dockerfile中添加了几行(据我认为,这将帮助我安装/启动nodejs):
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS Base
WORKDIR /app
EXPOSE 80 443
FROM microsoft/dotnet:2.2-sdk AS build
# install node and npm
ENV NODE_VERSION 10.16.0
ENV NODE_DOWNLOAD_SHA 2e2cddf805112bd0b5769290bf2d1bc4bdd55ee44327e826fa94c459835a9d9a
ENV NODE_DOWNLOAD_URL https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.gz
RUN wget "$NODE_DOWNLOAD_URL" -O nodejs.tar.gz \
&& echo "$NODE_DOWNLOAD_SHA nodejs.tar.gz" | sha256sum -c - \
&& tar -xzf "nodejs.tar.gz" -C /usr/local --strip-components=1 \
&& rm nodejs.tar.gz \
&& ln -s /usr/local/bin/node /usr/local/bin/nodejs
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash -
RUN apt-get install -y nodejs
# install npm packages first, this is slow so we want to minimise the number of un-cached runs
WORKDIR /src/SpaProj.WebUI/ClientApp/
COPY SpaProj.WebUI/ClientApp/package.json .
COPY SpaProj.WebUI/ClientApp/package-lock.json .
RUN npm install
RUN npm audit fix
# restore dotnet before build to allow it sit to cache
WORKDIR /
COPY ["SpaProj.Application/SpaProj.Application.csproj", "src/SpaProj.Application/"]
COPY ["SpaProj.Domain/SpaProj.Domain.csproj", "src/SpaProj.Domain/"]
COPY ["SpaProj.Persistence/SpaProj.Persistence.csproj", "src/SpaProj.Persistence/"]
COPY ["SpaProj.WebUI/SpaProj.WebUI.csproj", "src/SpaProj.WebUI/"]
RUN dotnet restore src/SpaProj.WebUI/SpaProj.WebUI.csproj
# copy source files and build
COPY . /src
RUN dotnet build /src/SpaProj.WebUI/SpaProj.WebUI.csproj --no-restore -c Release
RUN dotnet publish /src/SpaProj.WebUI/SpaProj.WebUI.csproj --no-restore -c Release -o /app
# Copy compiled app to runtime container
FROM base AS final
COPY --from=build /app .
CMD ["dotnet", "SpaProj.WebUI.dll"]
但是它根本没有帮助。 我还尝试了多阶段构建来收集来自节点映像的依赖关系,如下所示:
FROM 10.16.1-jessie as node-build
WORKDIR /src
COPY SpaProj.WebUI/ClientApp .
RUN npm install
RUN npm run build
该应用仍在引发相同的异常。
有人有同样的问题吗?还有另一种方法要对这种应用进行Docker化吗?