0

I am installing node v8.17.0(lts/carbon) through nvm, inside a container having java as base image. Im unable to find node inside it when I run any commands through it

FROM openjdk:8-alpine


*some java logic*

RUN apk update && apk add bash curl

WORKDIR ./ui

#nvm is installed here
RUN mkdir /usr/local/nvm
ENV NVM_DIR /usr/local/nvm

#installing nvm as mentioned in official website
RUN wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash

RUN . /usr/local/nvm/nvm.sh && nvm install lts/carbon

ENV PATH "/usr/local/nvm/versions/node/v8.17.0/bin:$PATH"

lts/carbon is the version I require to run my other node application. But once I build the image and run shell inside container and try to do node -v, I get node not found. Can anyone tell me where I am going wrong ?

FYI -> I took reference from this https://gist.github.com/remarkablemark/aacf14c29b3f01d6900d13137b21db3a

Below also does not seems to work for me

https://medium.com/@levsoroka/installing-node-js-via-nvm-inside-of-a-docker-container-that-is-based-on-amazon-linux-2-or-others-6e59c7dac5

Ishaan Kanwar
  • 399
  • 1
  • 4
  • 16
  • Have you tried to run `nvm list`? – Alvaro Inckot Dec 06 '22 at 16:20
  • also says nvm not found :(, but the installation runs fine without the docker build failing anywhere – Ishaan Kanwar Dec 06 '22 at 16:24
  • You posted a reference from a Debian image, and you are using Alpine, and looks like there are missing environment variables. You can check this guide to see the missing points: https://github.com/nvm-sh/nvm/blob/master/README.md#installing-nvm-on-alpine-linux – Alvaro Inckot Dec 06 '22 at 16:29
  • Also, depending on the version of the Alpine image you are using, I fear that node v8 may not be available. "[...] Note: Alpine 3.5 can only install NodeJS versions up to v6.9.5, Alpine 3.6 can only install versions up to v6.10.3, Alpine 3.7 installs versions up to v8.9.3, Alpine 3.8 installs versions up to v8.14.0, Alpine 3.9 installs versions up to v10.19.0, Alpine 3.10 installs versions up to v10.24.1, Alpine 3.11 installs versions up to v12.22.6 [...]" – Alvaro Inckot Dec 06 '22 at 16:30
  • There is a `openjdk:8-alpine3.9`, that I can change no issues. Still not able to figure out why `nvm` or `node` are not showing up, even after I put it in env path – Ishaan Kanwar Dec 06 '22 at 16:40
  • Normally a Docker container only runs one program. Can you use a separate image built `FROM node`, rather than trying to shoehorn it into a Java image? Or if not, can you install Node directly from a tarball without using `nvm`? – David Maze Dec 06 '22 at 21:23

1 Answers1

0

You have a typo in ENV PATH "usr/local/nvm/versions/node/v8.17.0/bin:$PATH" add / before usr and check the PATH variable inside the container

You can check the environment variables inside the container with export command.

FedeG
  • 106
  • 6
  • Does not fixes the issue `export PATH='/usr/local/nvm/versions/node/v8.17.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-1.8-openjdk/jre/bin:/usr/lib/jvm/java-1.8-openjdk/bin'` – Ishaan Kanwar Dec 06 '22 at 16:59
  • Check the files in that paths, you need to find `node` binary file inside a folder and add this folder to the PATH. Probably, the node binary file is in another path. If you check the `/usr/local/nvm/versions/node/` and see the folder (`ls`), you will see the correct folder and you can use it in the PATH env – FedeG Dec 06 '22 at 17:04
  • I'm giving the correct path, I've cross-verified it – Ishaan Kanwar Dec 06 '22 at 17:07
  • I try to build this Dockerfile and have the error `/bin/sh: bash: not found` but I could build with not alpine image and in this, the node was installed correctly and the PATH is correct too. how are you building and running the container? – FedeG Dec 06 '22 at 17:21
  • `RUN apk update && apk add bash curl`. Forgot to add this to the question, Updated it – Ishaan Kanwar Dec 06 '22 at 17:23
  • The problem is alpine: https://stackoverflow.com/a/45987284/3930612 – FedeG Dec 06 '22 at 20:54