Can't install node in docker image microsoft/d

2019-05-30 08:16发布

问题:

I asked this question originally at: https://github.com/aspnet/aspnet-docker/issues/349 as a part of the deprecation announcement, and I am hoping the SO community may have a good answer for this:

I am trying to use the windows side for a SPA build using the microsoft/dotnet:2.1-sdk. I know, I may be the only one trying to stay on the windows side for an ASP.NET Core Application, but our swarm initially will only have windows servers running in the native OS mode and not Hyper-V mode.

As a consequence, I need to install node.js for windows (because node.js/grunt/gulp are no longer a part of the image like they were in the microsoft/aspnetcore:2.0 image) and I tried:

RUN msiexec.exe /a https://nodejs.org/dist/v8.11.3/node-v8.11.3-x64.msi /quiet

but msiexec.exe isn't in the c:\windows\system32 of this image or in any other directory for that matter.

curl also is not in this image so I can't use that to download anything, and even if I could how do I un-tar or unzip anything?

I can't run a powershell invocation of:

iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

to try to install chocolatey to install node from there as System.Net.WebClient is not available in this image.

I guess my question is is there any container native way to get node.js installed internally without having to download something outside the container, copying it in, and then executing it. Kinda defeats the purpose of a multistage build if I have to do that or at least in my opinion makes it an ugly solution.

回答1:

instead curl use powershell's Invoke-WebRequest

instead unzip use Expand-Archive

installing MSI in nanoserver is not possible. For solution see: Powershell Silent Install in Nano Server with Docker



回答2:

Working off the answer from Miq I was able to create an initial Dockerfile which uses the 2.1 SDK image and pulls in node manually, here is what it looks like:

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /app
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

ENV NODE_VERSION 8.11.3
ENV NODE_FULL_NAME node-v8.11.3-win-x64

#install node and npm as MS no longer does this in any .NET Core nanoserver based images since 2.1
RUN New-Item -ItemType directory -Path /build; \
    Invoke-WebRequest https://nodejs.org/dist/v${env:NODE_VERSION}/${env:NODE_FULL_NAME}.zip -OutFile /build/${env:NODE_FULL_NAME}.zip; \
    Expand-Archive -LiteralPath /build/${env:NODE_FULL_NAME}.zip /build; \
    $newPath = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path; \
    $nodeFullName = ${env:NODE_FULL_NAME}; \
    $newPath = $newPath + ';/build/' + $nodeFullName; \
    Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newPath;


回答3:

They added Tar and Curl to the base runtimes.

FROM microsoft/dotnet:2.2-aspnetcore-runtime-nanoserver-1803 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

#Download the package we want and unzip it to our destination
RUN curl.exe -o node.zip https://nodejs.org/dist/v10.15.3/node-v10.15.3-win-x64.zip && \
  mkdir "C:\\Program Files\\node" && \
  tar.exe -xf node.zip -C "C:\\Program Files\\node" --strip-components=1