0

I was trying to install Node.js of version 14.17.3 with Dockerfile -

FROM jetbrains/teamcity-agent:2022.10.1-linux-sudo

RUN curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
RUN sudo sh -c 'echo deb https://apt.kubernetes.io/ kubernetes-xenial main > /etc/apt/sources.list.d/kubernetes.list'

RUN curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -

# https://github.com/AdoptOpenJDK/openjdk-docker/blob/master/12/jdk/ubuntu/Dockerfile.hotspot.releases.full
RUN sudo apt-get update && \
    sudo apt-get install -y ffmpeg gnupg2 git sudo kubectl \
    binfmt-support qemu-user-static mc jq
RUN sudo apt install -y cmake build-essential wget

ENV NODE_VERSION=14.17.3
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
ENV NVM_DIR=$HOME/root/.nvm
RUN sudo bash "$NVM_DIR/nvm.sh" && sudo nvm install ${NODE_VERSION}
RUN sudo bash "$NVM_DIR/nvm.sh" && sudo nvm use v${NODE_VERSION}
RUN sudo bash "$NVM_DIR/nvm.sh" && sudo nvm alias default v${NODE_VERSION}
ENV PATH="$HOME/.nvm/versions/node/v${NODE_VERSION}/bin/:${PATH}"
RUN node --version
RUN npm --version

However, it has failed with following logs:

20:05:25   Step 10/27 : RUN sudo bash "$NVM_DIR/nvm.sh" && sudo nvm install ${NODE_VERSION}
20:05:28    ---> Running in 533e65e26299
20:05:29   bash: /root/.nvm/nvm.sh: No such file or directory
20:05:29   The command '/bin/sh -c sudo bash "$NVM_DIR/nvm.sh" && sudo nvm install ${NODE_VERSION}' returned a non-zero code: 127
  • When you install nvm with `curl` you are running as current user when you pipe to bash. You need to run it as the sudo if you are going to do that with everything else. – Joe Dec 30 '22 at 03:36

1 Answers1

0

There's a solution for building a Docker image with nvm, which includes running the install commands under a login shell.

Regardless, note that you need to make sure that the installation directory ($NVM_DIR) exists before you install nvm-sh. Since the Base docker run under a non-admin user you cannot set it to be under the root user.

See an example for Dockerfile below which works based on provided solution:

FROM jetbrains/teamcity-agent:2022.10.1-linux-sudo

RUN curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
RUN sudo sh -c 'echo deb https://apt.kubernetes.io/ kubernetes-xenial main > /etc/apt/sources.list.d/kubernetes.list'

RUN curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -

# https://github.com/AdoptOpenJDK/openjdk-docker/blob/master/12/jdk/ubuntu/Dockerfile.hotspot.releases.full
RUN sudo apt-get update && \
    sudo apt-get install -y ffmpeg gnupg2 git sudo kubectl \
    binfmt-support qemu-user-static mc jq
RUN sudo apt install -y cmake build-essential wget

SHELL ["/bin/bash", "--login", "-c"]

ENV NODE_VERSION=14.17.3
ENV NVM_DIR /tmp/nvm
WORKDIR $NVM_DIR

RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash \
  && . $NVM_DIR/nvm.sh \
  && nvm install $NODE_VERSION \
  && nvm alias default $NODE_VERSION \
  && nvm use default

ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH      $NVM_DIR/v$NODE_VERSION/bin:$PATH

RUN node --version
RUN npm --version
TBA
  • 601
  • 5
  • 6