簡體   English   中英

Docker + Node.js + Windows

[英]Docker + Node.js + Windows

我想要什么: dockerize Node.js web 應用程序(我在 Windows 上)

Windows 集裝箱

docker-compose up讓我得到這個錯誤:

Service 'webapp' failed to build: no matching manifest for windows/amd64 in the manifest list entries

據我了解,這是因為 windows 沒有 Node.js 圖像,解決方法是切換到 Linux 容器。

不夠 memory

當我嘗試切換到 linux 容器時,Docker 告訴我我沒有足夠的 memory。通過設置更改分配的 memory 的數量並沒有解決它。

編輯:文件

docker-compose

version: '3'

services:
  webapp:
    build: ./Front
    volumes:
      - ./Front:./dockerized
    ports:
     - 5001:8080

Dockerfile :

FROM node:alpine

RUN mkdir -p ../dockerized

WORKDIR ../dockerized

COPY package*.json ../dockerized

RUN npm install

COPY . ../dockerized

EXPOSE 8080
CMD [ "npm", "start" ]

我知道最初的問題已經很老了,但是由於我前幾天遇到了類似的問題並且在一個地方找不到好的解決方案,我決定分享我解決這個問題的經驗。

因此,假設您想在 Windows 上運行基於 Windows 的 Docker 容器並在其中使用 Node.JS。

以下是您的選擇:

  1. 切換到基於 Linux 的 Docker 容器,它也可以在 Windows 中運行。 docker 文件的第一行可能如下所示:

    FROM node:latest

讓我們假設遷移到基於 Linux 的容器不適合您。 這可能有多種原因(例如,在我的情況下,我嘗試將基於 Linux 的 Docker 容器中的 Angular 應用程序部署到 Windows 10 上的本地 Azure Service Fabric 集群,但它僅支持基於 Windows 的映像)。

在這種情況下,您必須轉移到基於 Windows 的容器,並且還有兩個選項。

  1. 使用任何已安裝 Node.JS 的基於 Windows 的自定義 Docker 映像(Kush Grover 提出的選項)

  2. 創建您自己的基於 Windows 的 Docker 映像並在其中安裝 Node.JS。 最后一個選項是我最終想到的,因為我不想依賴一些非官方的公共自定義圖像。

下面是一個安裝了 Node.JS 的基於 Windows 的 Docker 文件示例:

FROM mcr.microsoft.com/windows/servercore:1803 as installer

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop';$ProgressPreference='silentlyContinue';"]

RUN Invoke-WebRequest -OutFile nodejs.zip -UseBasicParsing "https://nodejs.org/dist/v12.4.0/node-v12.4.0-win-x64.zip"; `
Expand-Archive nodejs.zip -DestinationPath C:\; `
Rename-Item "C:\\node-v12.4.0-win-x64" c:\nodejs

FROM mcr.microsoft.com/windows/nanoserver:1803

WORKDIR C:\nodejs
COPY --from=installer C:\nodejs\ .
RUN SETX PATH C:\nodejs
RUN npm config set registry https://registry.npmjs.org/

WORKDIR /app

# install and cache app dependencies
COPY src/WebSpa/package.json /app/src/WebSpa/package.json

WORKDIR /app/src/WebSpa
RUN npm install
RUN npm install -g @angular/cli@latest

# add app
COPY . /app

# start app
CMD cd /app/src/WebSpa && ng serve --host 0.0.0.0

對此文件的簡短說明。 我使用基於 Windows 的官方鏡像( FROM ...servercore:1803... )然后下載 Node.JS 二進制文件( RUN Invoke-WebRequest... )並向注冊表添加一些必需的東西( RUN npm config set registry... )。 后來我使用 Node.JS NPM 命令為我的 Angular 應用程序安裝所需的包( RUN npm install )並安裝 Angular CLI( RUN npm install -g @angular/cli@latest )以便能夠在容器上運行 Angular( ...ng serve... )。

請注意,我下載了 12.4.0 版的 Node.JS(目前可用的最新穩定版),您可能想要使用不同的版本。

我希望這足夠清楚,有人會發現這很有用。

我能夠通過如下更改 Dockerfile 來成功部署和運行它...

FROM mcr.microsoft.com/windows/servercore:1803 as installer

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop';$ProgressPreference='silentlyContinue';"]

RUN Invoke-WebRequest -OutFile nodejs.zip -UseBasicParsing "https://nodejs.org/dist/v12.4.0/node-v12.4.0-win-x64.zip"; Expand-Archive nodejs.zip -DestinationPath C:\; Rename-Item "C:\\node-v12.4.0-win-x64" c:\nodejs

FROM mcr.microsoft.com/windows/nanoserver:1803

WORKDIR C:/nodejs
COPY --from=installer C:/nodejs/ .
RUN SETX PATH C:\nodejs
RUN npm config set registry https://registry.npmjs.org/

在使用 node:8 在 Windows 10 上構建我的 Dockerfile 時遇到了同樣的問題。 我在這里更改為自定義節點圖像: https : //hub.docker.com/r/stefanscherer/node-windows/

或者如果您更喜歡使用官方的,請嘗試切換到 Linux 容器。

Kush Grover 的響應很好,但是這個 repo 已經過時(最新版本的節點是 12.18.3 到stefanscherer/node-windows:latest )。

我借用了上面的另一個GREAT 響應(來自 Witcher)並創建了下面的新圖像:

  • henriqueholtz/node-win:16.15.1
  • henriqueholtz/node-win:14.19.0

看看我是否在這里創建了更多標簽/版本

要嘗試這個新圖像,您可以簡單地執行:

docker run -d -t --name=node-win-16-15-1 henriqueholtz/node-win:16.15.1
docker exec -it node-win-16-15-1 cmd
node --version
npm --version

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM