angular - Docker 中的 ASP.NET Core Angular 模板监视文件更改
问题描述
我已经创建了 ASP.NET Core Angular 项目dotnet new angular
并托管在 docker 中。
如何在不重新启动 docker 容器的情况下查找文件更改?
Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /app
COPY "3_UI/MyAccounts.Web/MyAccounts.Web.csproj" ./myapp/
RUN dotnet restore "./myapp/MyAccounts.Web.csproj"
COPY "3_UI/MyAccounts.Web/." ./myapp/
WORKDIR /app/myapp
# Install Node, NPM and VS Debugger
RUN apt-get update && apt-get install -y curl sudo unzip procps
RUN apt-get install -y --no-install-recommends apt-utils
RUN curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l /publish/vsdbg;
RUN sudo apt-get install -y nodejs
RUN curl -L https://npmjs.org/install.sh | sudo sh
RUN echo "NODE Version:" && node --version
RUN echo "NPM Version:" && npm --version
RUN dotnet publish -c Debug -o out
FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS runtime
WORKDIR /app
EXPOSE 9000
# Install Node, NPM and VS Debugger
RUN apt-get update && apt-get install -y curl sudo unzip procps
RUN apt-get install -y --no-install-recommends apt-utils
RUN curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l /publish/vsdbg;
RUN sudo apt-get install -y nodejs
RUN curl -L https://npmjs.org/install.sh | sudo sh
RUN echo "NODE Version:" && node --version
RUN echo "NPM Version:" && npm --version
# ENV ASPNETCORE_ENVIRONMENT=development
ENV ASPNETCORE_URLS=http://+:9000
COPY --from=build /app/myapp/out .
ENTRYPOINT [ "dotnet", "MyAccounts.Web.dll"]
以为添加ENV ASPNETCORE_ENVIRONMENT=development
会监视客户端应用程序文件的更改,但是在容器启动后会引发以下错误。
app_web_service | npmfail: Microsoft.AspNetCore.SpaServices[0]
app_web_service | npm ERR! code ENOENT
app_web_service |
app_web_service | fail: Microsoft.AspNetCore.SpaServices[0]
app_web_service | npm ERR! syscall open
app_web_service |
app_web_service | fail: Microsoft.AspNetCore.SpaServices[0]
app_web_service | npm ERR! path /app/ClientApp/package.json
app_web_service |
app_web_service | fail: Microsoft.AspNetCore.SpaServices[0]
app_web_service | npm ERR! errno -2
app_web_service |
app_web_service | fail: Microsoft.AspNetCore.SpaServices[0]
app_web_service | npm ERR! enoent ENOENT: no such file or directory, open '/app/ClientApp/package.json'
app_web_service |
app_web_service | fail: Microsoft.AspNetCore.SpaServices[0]
app_web_service | npm ERR! enoent This is related to npm not being able to find a file.
app_web_service |
app_web_service | fail: Microsoft.AspNetCore.SpaServices[0]
app_web_service | npm ERR! enoent
app_web_service |
app_web_service | npm ERR!fail: Microsoft.AspNetCore.SpaServices[0]
app_web_service | npm ERR! A complete log of this run can be found in:
app_web_service |
app_web_service | fail: Microsoft.AspNetCore.SpaServices[0]
app_web_service | npm ERR! /root/.npm/_logs/2021-05-08T11_54_23_879Z-debug.log
是否缺少任何其他配置来监视文件更改?
解决方案
我有这个工作,但仍然不是优化的方式。我在自己的容器中拆分了 .NET Core 和 Angular。
Angular 客户端应用程序
# base image
FROM node:alpine
# set working directory
WORKDIR /app
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# install and cache app dependencies
COPY package*.json /app/
RUN npm install
RUN npm install -g @angular/cli@11.2.12
# add app
COPY . /app
# start app
CMD ng serve --host 0.0.0.0 --poll 500
.NET 核心应用
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim as build
ENV DOTNET_USE_POLLING_FILE_WATCHER 1
WORKDIR /app
COPY . .
RUN dotnet restore
RUN apt-get update && apt-get install -y curl sudo unzip procps
RUN apt-get install -y --no-install-recommends apt-utils
RUN curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l /publish/vsdbg;
ENTRYPOINT [ "dotnet", "watch", "run", "--urls", "http://+:9000"]
码头工人撰写
myaccounts.web:
container_name: app_web
build:
context: ./3_UI/MyAccounts.Web/ClientApp/
dockerfile: Dockerfile
volumes:
- ./3_UI/MyAccounts.Web/ClientApp/:/app
- '/app/node_modules'
ports:
- 4200:4200
depends_on:
- db
myaccounts.web.service:
container_name: api_gateway
build:
context: ./3_UI/MyAccounts.Web/
dockerfile: Dockerfile
volumes:
- ./3_UI/MyAccounts.Web/:/app
environment:
- ASPNETCORE_URLS=http://localhost:9000
- ASPNETCORE_ENVIRONMENT=development
ports:
- 9000:9000
depends_on:
- db
推荐阅读
- c# - Powershell调用无效的命令输出C#
- python - 熊猫列两侧的部分字符串匹配
- google-cloud-platform - GCP - 使用 nginx 入口控制器时的负载均衡器定价
- javascript - E11000 MongoDB 错误发生在我从模型中删除的字段中
- python - 文本和浮点到字符串部分的 Matplotlib 相同的 LaTeX 格式
- laravel - npm run watch 说一些问题
- python - nn.MultiheadAttention 的输入?
- mysql - MySQL:优化查询
- javascript - 编码我的 php 数组以显示在我的 javascript 代码中的问题
- python - AttributeError: 'list' 对象没有属性 'order_by' Python