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

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

问题描述

在创建新的 ASP.NET Core MVC 应用程序时要使用的正确 Docker 映像是什么,特别是使用 React/Redux(或其他需要的 Node.js)模板?如果不是特定的图像,对于由 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 new 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.

我正在处理的项目正在从 .NET Standard 1.1(独立版)的 ASP.NET MVC 升级到新的 .NET Standard 2.0 React/Redux 项目.

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.

所以你必须安装 node 才能运行项目.这是我想出的 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"]

注意我如何在 dockerfile 的第 5 行运行一个命令来更新 apt-get.然后在第 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 的泊坞窗.

UPDATE: so I looked into the hot module replacement problem, and it appears to be a limitation of docker for windows.

解决方法是配置 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/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天全站免登陆