2

I am new to Docker. We have a node based REST service and I am trying to create a docker image so that I can easily deploy the service on build agents. I have CD to the folder where we have the package.json of the service and added below docker file. I am on Win 10 build 14xxxxx and using Docker windows container. Do I need to pull a node image and install it on nanoserver first ?

FROM microsoft/nanoserver

# Create app directory
RUN powershell New-Item -ItemType directory -Path \usr\src\app
WORKDIR \usr\src\app

# Install app dependencies
COPY package.json \usr\src\app
RUN npm install

# Bundle app source
COPY . \usr\src\app

EXPOSE 8080

CMD [ "npm", "start" ]

When I run

docker build -t mycompany/node-engine

I am getting below error

'npm' is not recognized as an internal or external command,
    operable program or batch file.

Below is the full output

Sending build context to Docker daemon 24.67 MB
Step 1/8 : FROM microsoft/nanoserver
 ---> a943c29f0046
Step 2/8 : RUN powershell New-Item -ItemType directory -Path \usr\src\app
 ---> Using cache
 ---> f1df2109ddd6
Step 3/8 : WORKDIR \usr\src\app
 ---> Using cache
 ---> 66d552a76612
Step 4/8 : COPY package.json \usr\src\app
 ---> Using cache
 ---> fcf9663854c3
Step 5/8 : RUN npm install
 ---> Running in b47b47ad1439
'npm' is not recognized as an internal or external command,
operable program or batch file.
The command 'cmd /S /C npm install' returned a non-zero code: 1
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Rasika
  • 312
  • 1
  • 7
  • 19
  • What is "npm" is it part of default microsoft/nanoserver image? – Gregory Suvalian May 04 '17 at 00:36
  • npm is node package manager, its not part of nanoserver image. Its a separate application that we need to install and set the PATH variable. – Rasika May 04 '17 at 00:39
  • then you need to install it into image. Not sure what you are asking here since you are trying to call something inside image which is not there yet – Gregory Suvalian May 04 '17 at 00:40
  • Oki, looks like I need to copy the node files into the nano server from my host machine and the set the path variable. My next challenge is to copy files to nanoserver. I pulled the image and ran the nanoserver using docker run -t microsoft/nanoserver cmd and found the IP address using ipconfig. Then used $ip = "172.23.198.86" $s = New-PSSession -ComputerName $ip -Credential ~\Administrator Enter-PSSession -Session $s to connect and copy files , I am getting an authentication error. How do I configure nanoserver credentials ?? – Rasika May 04 '17 at 02:17
  • 1. You nee to use Enter-PSSession -RunAdministrator to login with admin priviliges to container. Bigger picture here is that I think you are not fully understanding how containers work. Even if you copy it to container, it's not going to stay in that running image unless you comit it which you don;t want to do. See my walkthrough here which might be easier to follow https://github.com/artisticcheese/artisticcheesecontainer/wiki – Gregory Suvalian May 04 '17 at 02:42
  • Tank, I can log in to running container, How do I copy files from my host machine to container ? – Rasika May 04 '17 at 04:23
  • Thanks a lot, I was able to copy using $ip = "172.23.201.86" $s = New-PSSession -ContainerId (Get-Container hungry_keller).ID -RunAsAdministrator Copy-Item -ToSession $s -Path "C:\Program Files\nodejs" -Destination “C:\” -Force -Recurse – Rasika May 04 '17 at 04:55
  • Do you know how to set path variable on nanoserver ? I connected to running nano server instance using docker exec -it...... and set the path variable using $env:path = "all the pre existed path vars; plus the one I need which is node". When I execute node --version, I could see its working. I stopped it and committed my new image. Then I started the my new image with node , connected to it and found out, all the files I coped were there, but my ENV variable is gone and therefore the command in my Docker File RUN npm install would not work – Rasika May 04 '17 at 05:45
  • You do NOT want to build your images using `commit` since once you do it you will never be able to properly patch it. All your logic has to be in `dockerfile`. I suggest create thread on docker forums or join slack channel for docker. It will be much faster way to help you then here. – Gregory Suvalian May 04 '17 at 11:38
  • Will do, Thanks. – Rasika May 04 '17 at 11:44

3 Answers3

3

It looks like you could take advantage of the new commands they added to the Nano image:

curl.exe and tar.exe (Thank you Unix)

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

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

#Add node to PATH
ENV PATH “C:\\Program Files\\node:%PATH%”

#Start Node
#CMD [ “node.exe” ]
  • 'curl.exe' is not recognized as an internal or external command, operable program or batch file. – Atul Aug 26 '22 at 19:32
1

I know I am late to the party here, but I came here looking for an answer to the same question.

A bit of research later and I found that there are a variety of third party node + server nano docker images that people have built.

For example, this looks like a nice clean one: https://github.com/a11smiles/docker-nano-nodejs/blob/master/Dockerfile

If you have issues with using somewhat random / unsupported docker images rather than official images (many commercial organisations do) you can at least look at the Dockerfile and learn how it was done (and maybe attribute the source :)

Simon Clough
  • 2,323
  • 1
  • 13
  • 14
-1

There is an image for this purpose in docker hub and you just need to use this image which was built on top of nano server:

https://hub.docker.com/r/compulim/nanoserver-node/

Ehsan
  • 141
  • 1
  • 8