【问题标题】:aspnetcore - angular universal in a docker containeraspnetcore - docker 容器中的通用角度
【发布时间】:2026-01-30 17:25:02
【问题描述】:

我正在尝试创建一个可以打包 aspnetcore2.0/angular-universal 应用程序的 docker 映像,但由于我的 docker 经验不足,我一直遇到问题。我真的可以使用一些帮助。

这里是dockerfile的内容:

FROM microsoft/aspnetcore-build:2.0 AS build-env
WORKDIR /app

COPY *.csproj ./
RUN npm cache clean --force
RUN npm install npm@latest
RUN npm install @angular/cli@latest
RUN npm install @ngtools/webpack@next
RUN node -v
RUN dotnet restore

COPY . ./
RUN dotnet publish -c Release -o out

FROM microsoft/microsoft/aspnetcore:2.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT [ "dotnet", "net-streetStyleCrew.dll" ]

由于 aspnetcore-build:2.0 带有一个太旧的 npm/node,它必须更新。它还没有进入 angular-cli 部分,但我认为当然也需要新鲜。 这是我现在遇到的问题,我不知道在尝试更新时如何解决容器内的网络问题:

Step 5/15 : RUN npm install npm@latest
 ---> Running in 72531196fc83
npm ERR! Windows_NT 10.0.16299
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "npm@latest"
npm ERR! node v6.13.0
npm ERR! npm  v3.10.10
npm ERR! code ENOTFOUND
npm ERR! errno ENOTFOUND
npm ERR! syscall getaddrinfo

npm ERR! network getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:443
npm ERR! network This is most likely not a problem with npm itself
npm ERR! network and is related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly.  See: 'npm help config'

npm ERR! Please include the following file with any support request:
npm ERR!     C:\app\npm-debug.log
The command 'cmd /S /C npm install npm@latest' returned a non-zero code: 1 

我正在尝试在 Windows 容器上运行它,因为我也没有大量使用 Linux 的经验。

我也绝对愿意接受任何可以改进我对基本概念的处理方法的建议。提前致谢。

【问题讨论】:

    标签: docker asp.net-core-2.0 asp.net-core-webapi angular-universal


    【解决方案1】:

    首先,我认为具体问题不是 Docker 相关问题。这是一个与网络相关的问题。也许this SO 线程会有所帮助。

    关于你的 Dockerfile:

    1. 官方最佳实践建议减少层数。每个RUN 命令都会创建一个层。同时,您可能希望拥有多个RUN 命令以提高可读性并利用缓存。所以,你需要在两者之间找到一个平衡点。在这种特殊情况下,我认为您应该将 npm 命令链接到单个 RUN 语句中。
    2. 另外,我认为您应该指定确切的版本,而不是使用latest 版本。 latest 将始终在创建映像时下载最新版本,您不知道这个新版本是否存在破坏您的应用程序的错误。因此,我们的想法是在特定版本中进行测试并在生产中使用相同的版本。如果您想稍后升级到更新版本,您需要先使用新版本测试您的应用,然后使用新版本更新您的 Dockerfile

    这是一个例子

    RUN mkdir /home/aus/.npm; \
    npm config set prefix /home/aus/.npm; \
    npm install --quiet --no-progress -g webpack@3.11.0; \
    npm install --quiet --no-progress -g @angular/cli@1.7.2; \
    npm install --quiet --no-progress;
    

    【讨论】: