创建新的ASP.NET Core MVC应用程序时使用的正确Docker镜像是什么,特别是使用React / Redux(或其他需要Node.js)模板?如果不是特定的图像,那么在Docker文件中应该为ASP.NET Core MVC支持的Node.js应用程序遵循哪些命令或进程?
除了运行支持MVC网站之外,我不需要框架的SDK版本。
dotnet new reactredux
运行时映像没有安装Node.js,并且在尝试运行容器时会出错。
Dockerfile:
FROM microsoft/aspnetcore:latest
ARG source=./bin/Debug/netcoreapp2.0/publish/
WORKDIR /app
COPY $source .
EXPOSE 80
ENTRYPOINT ["dotnet", "Project.dll"]
错误:
Unhandled 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.
我正在使用的项目正在从ASP.NET MVC for .NET Standard 1.1(独立版)升级到新的.NET Standard 2.0 React / Redux项目。
答案 0 :(得分:14)
问题是dockerfile(microsoft / aspnetcore:latest)中的基本映像没有安装节点。
所以你必须安装节点才能运行项目。这是我提出的dockerfile:
FROM microsoft/aspnetcore:2.0
ARG source
EXPOSE 80 5102
ENV ASPNETCORE_URLS http://*:80
RUN apt-get -qq update && apt-get -qqy --no-install-recommends install wget gnupg \
git \
unzip
RUN curl -sL https://deb.nodesource.com/setup_6.x | bash -
RUN apt-get install -y nodejs
WORKDIR /app
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "Project.dll"]
注意dockerfile的第5行我是如何运行命令来更新apt-get的。然后在第8-9行节点安装到docker镜像
仍有问题,从webpack更换热模块不起作用。甚至没有完全刷新工作。我还在调查它。
<强>更新强> 所以我查看了热模块更换问题,它似乎是limitation of docker for windows。
解决方法是配置webpack,以便它可以告诉浏览器在确定的时间内轮询更改。 See this link to see how to configure it
<强>更新强> 做了一点研究我发现microsoft有一个可以用来构建项目的图像,它被称为:microsoft / aspnetcore-build。此图像具有构建所需的所有依赖项(包括nodejs)。
所以最后,我做的是保留我的Dockerfile(使用microsoft / aspnetcore:2.0作为基本映像),并创建了一个新的Dockerfile用于开发,它引用了我之前提到的构建映像。在docker compose的帮助下,我根据环境切换Dockerfiles。
这种方法看起来更方便,因为当图像部署到生产环境时,它们应该准备好所有的javascript代码(在带有角度2的spa应用程序的情况下,做出反应等),换句话说,它们不应该有一个nodejs依赖性,使它们的规模不那么重。
答案 1 :(得分:2)
基于上面的@Daniels回答,在Docker上运行Visual Studio 2017 v15.4和ASP.NET Core 2.0,以下是为了允许SPA应用程序的正确生产和开发行为而需要进行的更改(在我的情况下我和# 39; m使用Angular):
在项目中添加一个新的Dockerfile,它是原始文件的副本。让我们称之为Dockerfile.Development。修改如下:
FROM microsoft/aspnetcore:2.0
ARG source
# BEGIN MODIFICATION - Node is needed for development (but not production)
RUN curl -sL https://deb.nodesource.com/setup_6.x | bash -
RUN apt-get install --assume-yes nodejs
# END MODIFICATION
WORKDIR /app
EXPOSE 80
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "MyService.dll"]
修改解决方案中的docker-compose.override.yml文件,以便在开发中使用这个新的dockerfile。它看起来像这样:
version: '3'
services:
myservice:
environment:
- ASPNETCORE_ENVIRONMENT=Development
ports:
- "80"
build:
dockerfile: Dockerfile.Development
修改项目中的webpack.config.js文件以启用观察更改,如下所示:
const clientBundleConfig = merge(sharedConfig, {
entry: { 'main-client': './ClientApp/boot.browser.ts' },
output: { path: path.join(__dirname, clientBundleOutputDir) },
// BEGIN MODIFICATION
watch: isDevBuild,
watchOptions: {
poll: isDevBuild ? 1000 : false
},
// END MODIFICATION
plugins: [
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./wwwroot/dist/vendor-manifest.json')
})
].concat(isDevBuild ? [
答案 2 :(得分:1)
如果您只需要将node
二进制文件添加到图像中,则可以复制microsoft/aspnetcore-build
dockerfile所执行的操作:
ENV NODE_VERSION 6.10.3
RUN set -ex \
&& for key in \
9554F04D7259F04124DE6B476D5A82AC7E37093B \
94AE36675C464D64BAFA68DD7434390BDBE9B9C5 \
0034A06D9D9B0064CE8ADF6BF1747F4AD2306D93 \
FD3A5288F042B6850C66B31F09FE44734EB7990E \
71DCFD284A79C3B38668286BC97EC7A07EDE3FC1 \
DD8F2338BAE7501E3DD5AC78C273792F7D83545D \
B9AE9905FFD7803F25714661B63B535A4C206CA9 \
C4F0DFFF4E8C1A8236409D08E73BC641CC11F4C8 \
; do \
gpg --keyserver pgp.mit.edu --recv-keys "$key" || \
gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key" || \
gpg --keyserver keyserver.pgp.com --recv-keys "$key" ; \
done
# set up node
RUN buildDeps='xz-utils' \
&& set -x \
&& apt-get update && apt-get install -y $buildDeps --no-install-recommends \
&& rm -rf /var/lib/apt/lists/* \
&& curl -SLO "https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.xz" \
&& curl -SLO "https://nodejs.org/dist/v$NODE_VERSION/SHASUMS256.txt.asc" \
&& gpg --batch --decrypt --output SHASUMS256.txt SHASUMS256.txt.asc \
&& grep " node-v$NODE_VERSION-linux-x64.tar.xz\$" SHASUMS256.txt | sha256sum -c - \
&& tar -xJf "node-v$NODE_VERSION-linux-x64.tar.xz" -C /usr/local --strip-components=1 \
&& rm "node-v$NODE_VERSION-linux-x64.tar.xz" SHASUMS256.txt.asc SHASUMS256.txt \
&& apt-get purge -y --auto-remove $buildDeps \
&& ln -s /usr/local/bin/node /usr/local/bin/nodejs
这会为您的图片增加约42 MB,使用apt-get
增加约157 MB。
答案 3 :(得分:1)
I got the Angular example template to work simply by installing nodejs into the base:
FROM microsoft/aspnetcore:2.0 AS base
RUN apt-get update && \
apt-get install -y wget && \
apt-get install -y gnupg2 && \
wget -qO- https://deb.nodesource.com/setup_6.x | bash - && \
apt-get install -y build-essential nodejs
WORKDIR /app
EXPOSE 80
... rest of Dockerfile ...
Everything else (Webpack Hot swap) didn't throw an error.
答案 4 :(得分:0)
即使您正在运行ASP.NET Core 3.0,Microsoft的基础映像仍然不包含NodeJS。您必须自己安装NodeJS。幸运的是,那并不困难。
这是我最近解决的方法。请注意,Dockerfile更像是伪代码,而不是功能齐全的代码,我消除了大多数噪音。它正在安装NodeJS的版本10,您当然可以更改它。 Here are different node distributions you can install。
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
# Install NodeJS 10
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash -
RUN apt-get install -y nodejs
RUN npm install
RUN npm run test-docker
RUN npm run build
RUN dotnet restore...
RUN dotnet test...
FROM build AS publish
RUN dotnet publish "Collector.Forms.Autogiro.Web.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Your.Web.dll"]
答案 5 :(得分:0)
这是容器化SPA应用程序的简便方法:
首先,我们必须在您的应用程序的csproj文件中注释这两行,因为它迫使我们在Docker容器中安装NodeJS。
<Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build" />
接下来,在容器内,您只需要ASP.NET Core运行时即可运行您的应用程序。但是,Node Docker映像用于编译React客户端应用程序。因此,您需要这样的Dockerfile:
FROM node as build-node
WORKDIR /ClientApp
# ClientApp folder contains all your react code
COPY MyApp/ClientApp/package.json .
COPY MyApp/ClientApp/package-lock.json .
RUN npm install
COPY MyApp/ClientApp/ .
# This command will generate build folder
RUN npm run build
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /app
ENV ASPNETCORE_ENVIRONMENT=Production
COPY *.sln .
# In next step you'll get an error if you didn't comment two lines mentioned above in csproj file
COPY MyApp/MyApp.csproj ./MyApp
RUN dotnet restore
COPY MyApp/. ./MyApp
WORKDIR /app/MyApp
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS runtime
WORKDIR /app
# Here we get compile result from .net core application
COPY --from=build /app/MyApp/out ./
# Here we get compile result for React app
COPY --from=build-node /ClientApp/build ./ClientApp/build
# Application port
EXPOSE 5000
ENTRYPOINT ["dotnet", "MyApp.dll"]