在Dockerfile中安装特定的node.js版本

y4ekin9u  于 2023-01-01  发布在  Node.js
关注(0)|答案(1)|浏览(765)

我试图安装Node.js版本14.17.3与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

但是,它失败了,日志如下:

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
l7wslrjt

l7wslrjt1#

building a Docker image with nvm有一个解决方案,包括在登录shell下运行安装命令。
无论如何,请注意,在安装nvm-sh之前,您需要确保安装目录($NVM_DIR)存在。由于Base docker run under a non-admin user,您无法将其设置为root用户。
请参见下面的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

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

相关问题