简体   繁体   中英

Docker + Node.js + Windows

What I want: dockerize a Node.js web app (I am on Windows)

Windows container

docker-compose up gets me this error:

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

As far as I understand that is because there is no Node.js image for windows, and a fix would be to switch to Linux container.

Not enough memory

When I try to switch to a linux container, Docker tells me that I don't have enough memory. Changing the amount of allocated memory through the settings does not fix it.

Edit: files

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" ]

I know the original question is pretty old but since I had similar issue last days and couldn't find good solution in single place I decided to share my experience in solving this.

So, let's assume you want to run Windows-based Docker container on Windows and use Node.JS inside.

Here are options you have:

  1. Switch to Linux-based Docker container which also can be run in Windows. First line of docker file might look like this one:

    FROM node:latest

Let's just assume that moving to Linux-based container isn't an option for you. There might be several reasons for this (for example, in my case I tried to deploy my Angular app in Linux-based Docker container to local Azure Service Fabric cluster on Windows 10 but it supports Windows-based images only).

In this case you have to move to Windows-based container and there are two more options.

  1. Use any custom Windows-based Docker image with Node.JS already installed (the option proposed by Kush Grover)

  2. Create your own Windows-based Docker image and install Node.JS inside. This last option is what I eventually came up with since I didn't want to rely on some non-official public custom image.

Here is an example of Windows-based Docker file with Node.JS installation:

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

A short explanation to this file. I use Windows-based official image ( FROM ...servercore:1803... ) then download Node.JS binaries ( RUN Invoke-WebRequest... ) and add some required stuff to registry ( RUN npm config set registry... ). Later I use Node.JS NPM commands to install required packages for my Angular app ( RUN npm install ) and install Angular CLI ( RUN npm install -g @angular/cli@latest ) to be able to run Angular on the container ( ...ng serve... ).

Note, that I download Node.JS of version 12.4.0 (latest stable available at the moment) and you might want to use different version.

I hope this was clear enough and somebody will find this useful.

I was able to successfully deploy and run this by changing the Dockerfile as follows...

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/

Got the same issue when building my Dockerfile on Windows 10 with node:8. I changed to custom node image here: https://hub.docker.com/r/stefanscherer/node-windows/

Or else If you prefer to use the official one, try switching to Linux containers.

The response from Kush Grover is great, but this repo is outdated (latest version of node is 12.18.3 into stefanscherer/node-windows:latest ).

I'm borrowing the another GREAT response above (from Witcher) and create the new images below:

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

See if I've create more tags/versions here

To try this new images you can simply execute:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM