SPA应用程序的.NET Core Docker映像 [英] .NET Core Docker Image for SPA Applications

查看:117
本文介绍了SPA应用程序的.NET Core Docker映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建新的ASP.NET Core MVC应用程序时,特别是与React / Redux(或其他必需的Node.js)模板一起使用时,正确使用的Docker映像是什么?如果不是特定的图像,那么在由ASP.NET Core MVC支持的Node.js应用程序的Dockerfile中应遵循什么命令或过程?

What is the correct Docker image to use when creating a new ASP.NET Core MVC app, specifically with the React/Redux (or other Node.js required) template? If not a specific image, what commands or process should be followed in the Dockerfile for a Node.js app backed by ASP.NET Core MVC?

我不需要除了运行支持MVC站点以外的其他任何版本,都使用该框架的SDK版本。

I don't require the SDK version of the framework for anything other than running the backing MVC site.

dotnet新reactredux

运行时映像未安装Node.js,并且在尝试运行容器时会出错。

The runtime image does not have Node.js installed, and will error when trying to run the container.

Dockerfile:

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升级(

The project I am working with is being upgraded from ASP.NET MVC for .NET Standard 1.1 (standalone), to a new .NET Standard 2.0 React/Redux project.

推荐答案

问题是您的dockerfile中的基本映像(microsoft / aspnetcore:latest)没有安装节点。

The problem is that the base image in your dockerfile (microsoft/aspnetcore:latest) does not have node installed.

所以必须安装节点才能运行项目。这是我想到的dockerfile:

So you have to install node so you can run the project. This is the dockerfile I came up with:

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"]

注意第5行的方式我正在运行命令以更新apt-get的dockerfile。然后在第8-9行中将节点安装到Docker映像

Notice how on line 5 of the dockerfile I'm running a command to update apt-get. And then in line 8-9 node is installed to the docker image

仍然存在问题,无法从webpack替换热模块。甚至没有完全刷新的效果。我仍在研究它。

There is still a problem, hot module replacement from webpack does not work. Not even a full refresh works. I'm still looking in to it.

更新:
,所以我调查了热模块更换问题,它似乎成为针对Windows的docker的限制

解决方法是配置webpack,以便它可以告诉浏览器在确定的时间内轮询更改。 查看此链接以了解如何配置

The workaround is to configure webpack so it can tell the browser to poll for changes on a determined amount of time. See this link to see how to configure it

更新:
做更多的研究,我发现microsoft具有可用于构建项目的映像,称为:microsoft / aspnetcore-build。此映像具有构建所需的所有依赖项(包括nodejs)。

UPDATE: Doing a little more research I found out that microsoft has an image you can use to build your project, it is called: microsoft/aspnetcore-build. This image has all the dependencies you need for building (including nodejs).

因此,最后,我所做的就是将Dockerfile保留为原样(使用microsoft / aspnetcore :2.0作为基础映像),并创建了一个新的Dockerfile进行开发,该文件引用了我之前提到的构建映像。借助于docker compose,我可以根据环境切换Dockerfile。

So at the end, what I did was leave my Dockerfile as it was (with microsoft/aspnetcore:2.0 as base image), and created a new Dockerfile for development which references the build image I mentioned before. With the help of docker compose I switch Dockerfiles depending on the environment.

这种方法似乎更方便,因为将映像部署到生产环境时,它们应该已经准备好所有的javascript代码。 (对于带有angular 2,react等的spa应用程序),换句话说,它们不应该具有nodejs依赖性,从而使其大小减轻了。

This approach seems more convenient because when images are deployed to production environment they should have all its javascript code ready (in the case of a spa application with angular 2, react, etc), in other words they should not have a nodejs dependency, making them less heavy in size.

这篇关于SPA应用程序的.NET Core Docker映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆