Configuring Dockerfile for an ASP.NET Core 2.1 Angular Project

点点圈 提交于 2019-11-29 21:26:22

问题


I'm a complete newbie on Docker and currently trying to create an ASP.NET Core 2.1 with Angular project. I'm using a Linux Container on Docker for Windows and my IDE is VS2017 community edition.

Currently, I'm receiving this error:

[1] Ensure that 'npm' 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 executable is in one of those directories, or update your PATH.

I believe I need to install Node.js in my Docker instance or whatever you call that, and it should be through Dockerfile. Please point out my mistakes in my assumption. And how to ensure that the dependencies will be installed regardless on what kind of container I'm using? I'm expecting that my future projects will be installed on different platforms.


回答1:


In the microsoft/dotnet:2.1-aspnetcore-runtime container image npm/nodejs is not installed. To install this in the container update the docker file

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base

# Setup NodeJs
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
# End setup

WORKDIR /app
EXPOSE 80

Only Angular CLI live reload is not working.




回答2:


This is the similar solution to the existing problem. Please followed the below link AggregateException: One or more errors occurred. (One or more errors occurred. (Failed to start 'npm'. To resolve this:




回答3:


Because of publish happens on based microsoft/dotnet:2.1-sdk image. So, this base image contains only dotnet sdk but not npm installed this base image. There are 2 steps for solving this problem:

1) Adding installation nodejs to existing dotnet:2.1-sdk as written in answer by @Marcel.

or

2) You may add additional step to Dockerfile for building frontend side based on NODE image and build your Angular. After success build you will get generated frontend files and will copy them to runtime image on final stage. So you will not add any installation to dotnet sdk with this option and with one Dockerfile with multiple stages you will build backend side seperately and frontend site seperately.

If you would like to make it as second option, please follow below link for building frontend on Dockerfile seperately with ASP.NET Core on same docker file:

Dockerize ASP.NET Core Angular application (Solving error: The command “npm install” exited with code 127.)




回答4:


Instead of install NPM and Node.js to the base image, you can also choose a base image that has the frameworks preinstalled. You can use my dotnet-angular image which is publicly available on hub.docker.com. Just replace

FROM microsoft/dotnet:2.2-sdk AS build

with

FROM mjibrandl/dotnetcore-angular:latest AS build

Check also my article: Setup Azure DevOps YAML pipeline to publish a .NET Core Angular 7 docker container to Azure Container Registry



来源:https://stackoverflow.com/questions/50859980/configuring-dockerfile-for-an-asp-net-core-2-1-angular-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!