简体   繁体   English

docker如何安装nvm?

[英]How to install nvm in docker?

I am in the process of building a new Docker image and I'm looking to get NVM installed so I can manage nodejs.我正在构建一个新的 Docker 图像,我正在寻找安装 NVM 以便我可以管理 nodejs。

Reading the docs on how to install NVM they mention that you need to source your.bashrc file in order to start using NVM.阅读有关如何安装 NVM 的文档,他们提到您需要获取您的 .bashrc 文件才能开始使用 NVM。

I've tried to set this up in a Dockerfile, but so far building fails with the error:我试图在 Dockerfile 中进行设置,但到目前为止构建失败并出现以下错误:

"bash: nvm: command not found" “bash:nvm:找不到命令”

Here are the relevant lines from my Dockerfile:以下是我的 Dockerfile 中的相关内容:

ADD files/nvm_install.sh /root/
RUN chmod a+x  /root/nvm_install.sh
RUN bash -c "/root/nvm_install.sh"
RUN bash -l -c "source /root/.bashrc"
RUN cd /root
RUN bash -l -c "nvm install 0.10.31"

Here is the output from trying to build:这是尝试构建的 output:

docker build -t nginx_dock. docker 构建-t nginx_dock。

Step 0 : FROM ubuntu
---> 826544226fdc
Step 1 : MAINTAINER dficociello
---> Using cache
---> da3bc340fbb3
Step 2 : RUN apt-get update
---> Using cache
---> 6b6b611feb4f
Step 3 : RUN apt-get install nginx curl -y
---> Using cache
---> 159eb0b16d23
Step 4 : RUN touch /root/.bashrc
---> Using cache
---> 5e9e8216191b
Step 5 : ADD files/nginx.conf /etc/nginx/
---> Using cache
---> c4a4a11296a2
Step 6 : ADD files/nvm_install.sh /root/
---> Using cache
---> b37cba2a18ca
Step 7 : RUN chmod a+x  /root/nvm_install.sh
---> Using cache
---> bb13e2a2893d
Step 8 : RUN bash -c "/root/nvm_install.sh"
---> Using cache
---> 149b49a8fc71
Step 9 : RUN bash -l -c "source /root/.bashrc"
---> Running in 75f353ed0d53
---> 0eae8eae7874
Removing intermediate container 75f353ed0d53
Step 10 : RUN cd /root
---> Running in feacbd998dd0
---> 284293ef46b0
Removing intermediate container feacbd998dd0
Step 11 : RUN bash -l -c "nvm install 0.10.31"
---> Running in 388514d11067
bash: nvm: command not found
2014/09/17 13:15:11 The command [/bin/sh -c bash -l -c "nvm install 0.10.31"] returned a non-zero         code: 127

I'm pretty new to Docker so I may be missing something fundamental to writing Dockerfiles, but so far all the reading I've done hasn't shown me a good solution.我是 Docker 的新手,所以我可能缺少编写 Dockerfile 的一些基础知识,但到目前为止,我所做的所有阅读都没有给我一个好的解决方案。

When you RUN bash... each time that runs in a separate process, anything set in the environment is not maintained.当您RUN bash...每次在单独的进程中运行时,环境中设置的任何内容都不会得到维护。 Here's how I install nvm :这是我安装nvm

# Replace shell with bash so we can source files
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

# Set debconf to run non-interactively
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections

# Install base dependencies
RUN apt-get update && apt-get install -y -q --no-install-recommends \
        apt-transport-https \
        build-essential \
        ca-certificates \
        curl \
        git \
        libssl-dev \
        wget \
    && rm -rf /var/lib/apt/lists/*

ENV NVM_DIR /usr/local/nvm # or ~/.nvm , depending
ENV NODE_VERSION 0.10.33

# Install nvm with node and npm
RUN curl https://raw.githubusercontent.com/creationix/nvm/v0.20.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

To help everyone that are looking for a way to install the Node.js with NVM on Ubuntu (last version), I made the dockerfile below.为了帮助正在寻找在 Ubuntu(最新版本)上使用 NVM 安装 Node.js 的每个人,我制作了下面的 dockerfile。 I'm using the last version of Docker, Ubuntu, Node.js and the NVM is working properly (the $PATH was fixed).我使用的是最新版本的 Docker、Ubuntu、Node.js,并且 NVM 工作正常($PATH 已修复)。 I'm using this in a production environment.我在生产环境中使用它。

$ docker info \
Server Version: 1.9.1
Kernel Version: 4.1.13-boot2docker
Operating System: Boot2Docker 1.9.1 (TCL 6.4.1); master : cef800b - Fri Nov 20 19:33:59 UTC 2015

Node.js Version: stable 4.2.4 LTS
Ubuntu Version: 14.04.3


dockerfile:码头档案:

FROM ubuntu:14.04.3

# Replace shell with bash so we can source files
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

# make sure apt is up to date
RUN apt-get update --fix-missing
RUN apt-get install -y curl
RUN apt-get install -y build-essential libssl-dev

ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 4.2.4

# Install nvm with node and npm
RUN curl https://raw.githubusercontent.com/creationix/nvm/v0.30.1/install.sh | bash \
    && source $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/versions/node/v$NODE_VERSION/bin:$PATH

RUN mkdir /usr/app
RUN mkdir /usr/app/log

WORKDIR /usr/app

# log dir
VOLUME /usr/app/log

# Bundle app source
COPY . /usr/app
# Install app dependencies
RUN npm install

EXPOSE  3000
CMD ["node", "server.js"]

Update 20/02/2020 : This solution works if you're using a debian base image. 2020 年 2 月 20 日更新:如果您使用的是debian基础映像,则此解决方案有效。 If you're using ubuntu , see this answer .如果您使用的是ubuntu ,请参阅此答案

Here is the cleanest way to install nvm that I have found:这是我发现的安装nvm的最干净的方法:

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

RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
RUN nvm install 10.15.3

Explanation解释

  • The first line sets the Dockerfile's default shell to a bash login shell .第一行将 Dockerfile 的默认 shell 设置为bash login shell Note: this means that every subsequent RUN , CMD , and ENTRYPOINT will be run under the current user (usually root), and source the ~/.bashrc file if run in the shell form .注意:这意味着每个后续的RUNCMDENTRYPOINT将在当前用户(通常是 root)下运行,如果以shell 形式运行,则源~/.bashrc文件。

  • The second line installs nvm with bash.第二行使用 bash 安装nvm When the script is run with bash, it appends to the ~/.bashrc file.当脚本使用 bash 运行时,它会附加到~/.bashrc文件中。

  • The third line installs a particular version of nodejs and uses it.第三行安装特定版本的 nodejs 并使用它。 The nvm , npm , and node commands are available because they are run via a bash login shell (see line 1). nvmnpmnode命令可用,因为它们是通过 bash 登录 shell 运行的(参见第 1 行)。

Nvm paths have changed since the accepted answer, so if you want to use a more up-to-date nvm version, you need to make a few changes. Nvm 路径自接受的答案以来已更改,因此如果您想使用更新的 nvm 版本,则需要进行一些更改。 Also, it is not necessary to remap sh to make it work:此外,没有必要重新映射sh以使其工作:

ENV NVM_DIR /usr/local/nvm
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash
ENV NODE_VERSION v7.9.0
RUN /bin/bash -c "source $NVM_DIR/nvm.sh && nvm install $NODE_VERSION && nvm use --delete-prefix $NODE_VERSION"

ENV NODE_PATH $NVM_DIR/versions/node/$NODE_VERSION/lib/node_modules
ENV PATH      $NVM_DIR/versions/node/$NODE_VERSION/bin:$PATH

Not sure if you will need the --delete-prefix option on the nvm use - I did, but that may be something strange about my base image.不确定您是否需要nvm use--delete-prefix选项 - 我做了,但这可能对我的基本图像有些奇怪。

Each RUN in a Dockerfile is executed in a different container. Dockerfile 中的每个RUN在不同的容器中执行。 So if you source a file in a container, its content will not be available in the next one.因此,如果您在容器中获取文件,则其内容在下一个容器中将不可用。

That is why when you install an application and you need to do several steps, you must do it in the same container.这就是为什么当您安装应用程序并且需要执行多个步骤时,必须在同一个容器中执行。

With your example:以你的例子:

ADD files/nvm_install.sh /root/
RUN chmod a+x /root/nvm_install.sh && \
  /root/nvm_install.sh && \
  source /root/.bashrc && \
  cd /root && \
  nvm install 0.10.31

This is based on the top answer and works in 2018:这是基于最佳答案并在 2018 年有效:

# Replace shell with bash so we can source files
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

# Install base dependencies
RUN apt-get update && apt-get install -y -q --no-install-recommends \
        apt-transport-https \
        build-essential \
        ca-certificates \
        curl \
        git \
        libssl-dev \
        wget

ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 8.11.3

WORKDIR $NVM_DIR

RUN curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash \
    && . $NVM_DIR/nvm.sh \
    && nvm install $NODE_VERSION \
    && nvm alias default $NODE_VERSION \
    && nvm use default

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

Note that nvm is not a bash command, it is an alias.请注意, nvm不是 bash 命令,而是别名。 This can screw you up if you're relying on $PATH .如果您依赖$PATH这可能会搞砸。

Took me an hour or two to figure out the cleanest way to do it.我花了一两个小时才找到最干净的方法。 --login doesn't seem to execute .bashrc so you have to supply -i to launch it in interactive mode. --login 似乎没有执行 .bashrc 所以你必须提供 -i 以交互模式启动它。 This causes Docker to yell at you for a bit so I only launch this way for the installation, then reset to my standard shell.这会导致 Docker 对您大喊大叫,所以我只以这种方式启动安装,然后重置为我的标准 shell。

# Installing Node
SHELL ["/bin/bash", "--login", "-i", "-c"]
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash
RUN source /root/.bashrc && nvm install 12.14.1
SHELL ["/bin/bash", "--login", "-c"]

Here is my working version这是我的工作版本

FROM ubuntu:14.04

# Declare constants
ENV NVM_VERSION v0.29.0
ENV NODE_VERSION v5.0.0

# Replace shell with bash so we can source files
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

# Install pre-reqs
RUN apt-get update
RUN apt-get -y install curl build-essential

# Install NVM
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/${NVM_VERSION}/install.sh | bash

# Install NODE
RUN source ~/.nvm/nvm.sh; \
    nvm install $NODE_VERSION; \
    nvm use --delete-prefix $NODE_VERSION;

Took help from @abdulljibali and @shamisis answers.从@abdulljibali 和@shamisis 的答案中获得帮助。

Based upon the suggestion in @Kuhess answer, I replaced the source command with the following in my Dockerfile根据@Kuhess 回答中的建议,我在 Dockerfile 中用以下内容替换了源命令

RUN cat ~/.nvm/nvm.sh >> installnode.sh
RUN echo "nvm install 0.10.35" >> installnode.sh
RUN sh installnode.sh

A key difference between the attempt to get the nvm command in the question:尝试获取问题中的 nvm 命令之间的主要区别:

RUN bash -l -c "source /root/.bashrc"

which doesn't work and the attempt to do the same in the accepted answer:这不起作用,并尝试在接受的答案中做同样的事情:

source $NVM_DIR/nvm.sh

Is that the second version sources the nvm.sh script directly, whereas the original tries to do it via the .bashrc file.是第二个版本直接获取 nvm.sh 脚本,而原始版本尝试通过 .bashrc 文件来实现。

The .bashrc file has a line in it early on which exits if it's being run in a non interactive shell: .bashrc 文件中有一行,如果它在非交互式 shell 中运行,它会在早期退出:

# If not running interactively, don't do anything
case $- in
    *i*) ;;
    *) return;;
esac

So it never gets to the bit where it would have sourced nvm.sh which actually puts the nvm command in your shell.所以它永远不会到达它会提供 nvm.sh 的地方,它实际上将 nvm 命令放在你的 shell 中。

I wouldn't be surprised if docker is running this stuff in a non interactive shell.如果 docker 在非交互式 shell 中运行这些东西,我不会感到惊讶。 This hadn't been explicitly pointed out, so I thought I would mention it as it's what caught me out when I was doing something similar with vagrant.没有明确指出这一点,所以我想我会提到它,因为当我用 vagrant 做类似的事情时,这就是让我注意到的地方。

I must begin with the fact that I searched all over to get a working example of nvm inside docker and I found none.我必须用我找遍了得到的工作示例的事实开始nvmdocker ,我没有发现。 Even the answers in this thread did not work.甚至这个线程中的答案也不起作用。

So, I spent quite some time and came up with one that works:所以,我花了很多时间,想出了一个有效的方法:

# install dependencies
RUN apt-get update && apt-get install -y \
      curl \
      npm \
      nodejs \
      git;

# compatibility fix for node on ubuntu
RUN ln -s /usr/bin/nodejs /usr/bin/node;

# install nvm
RUN curl https://raw.githubusercontent.com/creationix/nvm/v0.24.1/install.sh | sh;

# invoke nvm to install node
RUN cp -f ~/.nvm/nvm.sh ~/.nvm/nvm-tmp.sh; \
    echo "nvm install 0.12.2; nvm alias default 0.12.2" >> ~/.nvm/nvm-tmp.sh; \
    sh ~/.nvm/nvm-tmp.sh; \
    rm ~/.nvm/nvm-tmp.sh;

Notice how I have installed nodejs via apt-get as well.注意我是如何通过apt-get安装nodejs I found that some packages don't get installed inside docker unless this is done.我发现除非这样做,否则某些软件包不会安装在 docker 中。

Just one answer put the curl installation but does not work the entire Dockerfile只有一个答案将 curl 安装,但不适用于整个 Dockerfile

Here my Dockerfile ready to copy/paste in which I install latest nvm 2020 version with Ubuntu 18.04.3 LTS这里我的 Dockerfile 准备复制/粘贴,我在其中安装了最新的nvm 2020 版本Ubuntu 18.04.3 LTS

FROM ubuntu

RUN apt-get update
RUN echo "y" | apt-get install curl
ENV NVM_DIR /usr/local/nvm
RUN mkdir -p /usr/local/nvm
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
ENV NODE_VERSION v10
RUN /bin/bash -c "source $NVM_DIR/nvm.sh && nvm install $NODE_VERSION && nvm use --delete-prefix $NODE_VERSION"

ENV NODE_PATH $NVM_DIR/versions/node/$NODE_VERSION/lib/node_modules
ENV PATH      $NVM_DIR/versions/node/$NODE_VERSION/bin:$PATH

None of these worked for me, for my python3-onbuild container I had to force-create symbolic links to the nvm installation.这些都不适合我,对于我的python3-onbuild容器,我必须强制创建指向 nvm 安装的符号链接。

# Install npm and nodejs
RUN apt-get install -y build-essential libssl-dev

RUN mkdir /root/.nvm
ENV NVM_DIR /root/.nvm
ENV NODE_VERSION 8.9.4

RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.9/install.sh | bash
RUN chmod +x $HOME/.nvm/nvm.sh
RUN . $HOME/.nvm/nvm.sh && nvm install $NODE_VERSION && nvm alias default $NODE_VERSION && nvm use default && npm install -g npm

RUN ln -sf /root/.nvm/versions/node/v$NODE_VERSION/bin/node /usr/bin/nodejs
RUN ln -sf /root/.nvm/versions/node/v$NODE_VERSION/bin/node /usr/bin/node
RUN ln -sf /root/.nvm/versions/node/v$NODE_VERSION/bin/npm /usr/bin/npm

25-Feb-2021 The main problem is with use of the 'source' directive, which is bash shell specific. 2021 年 2 月 25 日的主要问题是使用 'source' 指令,这是 bash shell 特定的。 What worked for me was replacing 'source' with '.'对我有用的是用 '.' 替换 'source'。 for a Ubuntu 18 install.对于 Ubuntu 18 安装。 My Dockerfile:我的 Dockerfile:

FROM ubuntu:bionic

RUN \
  apt update && \
  apt upgrade -y && \
  apt install -y curl

ENV NVM_DIR /root/.nvm
ENV NODE_VERSION 14.16

# Install nvm with node and npm
RUN curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.35.3/install.sh | bash \
  && . $NVM_DIR/nvm.sh \ 
  && nvm install $NODE_VERSION

This is what worked for me (I'm using debian buster):这对我有用(我使用的是 debian buster):

RUN apt-get update
RUN apt-get install -y build-essential checkinstall libssl-dev
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.35.1/install.sh | bash
SHELL ["/bin/bash", "--login", "-c"]

You should now be able to do nvm install <version> .您现在应该可以执行nvm install <version>

nvm not found can result from it being installed for a different user than the one who is executing the container. nvm not found可能是因为它是为与执行容器的用户不同的用户安装的。 You may need to prefix the installation with the custom user who is executing the container.您可能需要使用正在执行容器的自定义用户作为安装前缀。 The last USER statement defines the container user.最后一个USER语句定义容器用户。

USER $USERNAME

RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Reason原因

Diving into a nvm install script, egv0.39.1 , one can see that is installed into $HOME of the current user.进入一个nvm安装脚本,例如v0.39.1 ,可以看到它安装到当前用户的$HOME中。 If you have not changed it, the default user of a ubuntu image is root .如果您没有更改它,则ubuntu映像的默认用户是root When starting the container with a different user however, nvm won't be found;但是,当使用其他用户启动容器时,将找不到nvm hence make sure user of installation and execution align.因此确保安装和执行的用户对齐。

nvm_default_install_dir() {
  [ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm"
}

After testing most information here as well as other posts, turned out in my case it was related to permission issues, that lead to weird bugs, like failing to install a npm project unless run as root user, my setup was to run VueJs along a PHP CMS, the final portion that worked was:在测试了这里的大部分信息以及其他帖子之后,在我的情况下发现它与权限问题有关,这会导致奇怪的错误,比如除非以 root 用户身份运行,否则无法安装 npm 项目,我的设置是沿着PHP CMS,最后工作的部分是:

ENV NVM_DIR $TMP_STORE/nvm
ENV NODE_VERSION 16.15.0

RUN chown -R www-data:www-data /var/www/

USER www-data
RUN export XDG_CONFIG_HOME=$TMP_STORE \
    && curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

#RUN chown -R www-data:www-data $NVM_DIR

RUN source $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/versions/node/v$NODE_VERSION/bin:$PATH

RUN npm install -g @vue/cli \
    && npm install -g vue

USER root

The whole docker configuration can be found here整个 docker 配置可以在这里找到

Also had an oddly hard time for my docker file extending the CircleCI runner image - this worked for me:我的 docker 文件扩展 CircleCI runner 图像也遇到了奇怪的困难 - 这对我有用:

FROM circleci/runner:launch-agent
SHELL ["/bin/bash", "--login", "-c"]

USER $USERNAME

RUN wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash;

ENV NODE_VERSION 18.12.1

ENV NVM_DIR $HOME/.nvm

RUN \
    . ~/.nvm/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/versions/node/v$NODE_VERSION/bin:$PATH

RUN npm -v
RUN node -v

This installs the lts-version of nodejs when extending image "php:7.4.15" (debian:buster-slim):这会在扩展图像“php:7.4.15”(debian:buster-slim)时安装 nodejs 的 lts 版本:

# Install nvm to install npm and node.js
ENV NVM_DIR /root/.nvm
ENV NODE_VERSION lts/*
RUN mkdir $HOME/.nvm && \
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash && \
    chmod +x $HOME/.nvm/nvm.sh && \
    . $HOME/.nvm/nvm.sh && \
    nvm install --latest-npm "$NODE_VERSION" && \
    nvm alias default "$NODE_VERSION" && \
    nvm use default && \
    DEFAULT_NODE_VERSION=$(nvm version default) && \
    ln -sf /root/.nvm/versions/node/$DEFAULT_NODE_VERSION/bin/node /usr/bin/nodejs && \
    ln -sf /root/.nvm/versions/node/$DEFAULT_NODE_VERSION/bin/node /usr/bin/node && \
    ln -sf /root/.nvm/versions/node/$DEFAULT_NODE_VERSION/bin/npm /usr/bin/npm

I had a really hard time getting NVM working properly on an alpine-based image.我很难让 NVM 在基于 alpine 的图像上正常工作。 I ultimately just copied over a bunch of the shared directories from an official Node alpine image.我最终只是从官方的 Node alpine 镜像中复制了一堆共享目录。 Seems to be working quite well so far.到目前为止似乎工作得很好。

# Dockerfile

###############################################################################
# https://docs.docker.com/develop/develop-images/multistage-build/
# Builder Image
# This image is intended to build the app source code, not to run it.
###############################################################################
FROM node:16-alpine as builder

WORKDIR /build-tmp

COPY ./dist/src/yarn.lock .
COPY ./dist/src/package.json .
RUN yarn install --production

###############################################################################
# Execution image
# Nothing from the builder image is included here unless explicitly copied
###############################################################################
FROM osgeo/gdal:alpine-normal-3.4.2

# It's seemingly very difficult to build a specific version of node in an Alpine linux
# image, so let's copy node from the builder image into this execution image!
COPY --from=builder /usr/lib /usr/lib
COPY --from=builder /usr/local/share /usr/local/share
COPY --from=builder /usr/local/lib /usr/local/lib
COPY --from=builder /usr/local/include /usr/local/include
COPY --from=builder /usr/local/bin /usr/local/bin

...

CMD ["node", "main"]

Here is a solution I recently used:这是我最近使用的一个解决方案:

# Install nvm/Node.js
ENV NVM_VERSION=0.39.1
ENV NODE_VERSION=16.17.1
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v$NVM_VERSION/install.sh | bash
RUN bash --login -c "nvm install $NODE_VERSION"

# Do whatever with nvm
RUN bash --login -c "nvm use $NODE_VERSION && npm [...]"

2022 update: 2022 年更新:

based off https://stackoverflow.com/a/60137919/2047472 I came up with:基于https://stackoverflow.com/a/60137919/2047472我想出了:

FROM python:3.10

RUN touch .profile

SHELL ["/bin/bash", "--login", "-i", "-c"]
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
SHELL ["/bin/bash", "--login", "-c"]

RUN nvm install
RUN node -v
RUN npm -v
  • if you use .nvmrc and use source to init nvm, beware of a bug in nvm.sh causing it to exit with return code 3 when .nvmrc is present in current or parent directory如果您使用.nvmrc并使用 source 来初始化 nvm,请注意当.nvmrc存在于当前目录或父目录中时,nvm.sh 中的一个错误会导致它退出并返回代码 3
  • I had to touch .profile as it didn't exist, otherwise nvm is not activated in subsequent RUN commands我不得不触摸.profile因为它不存在,否则 nvm 不会在后续的 RUN 命令中激活
    • touch.bashrc didn't work touch.bashrc没有工作

2023 to use as dev-container 2023 用作开发容器

I started to use the dev-contieners and to set up my enviroment I used the next Dockcerfile that works perfectly with the purpose I've described.我开始使用 dev-contieners 并设置我的环境,我使用了下一个Dockcerfile ,它可以完美地满足我所描述的目的。 I share this because spend a hard time to achive it.我分享这个是因为要花很多时间才能实现它。

FROM ubuntu:22.04
ENV HOME="/root"
ENV NVM_DIR="${HOME}/.nvm"
ENV NVM_VERSION=v0.39.3
ENV NODE_VERSION=18

RUN apt-get update \
    && apt-get install -y --no-install-recommends build-essential\
        libssl-dev \
        git \
        curl \
        ca-certificates \
    && git clone https://github.com/nvm-sh/nvm.git "${NVM_DIR}" 
WORKDIR  "${NVM_DIR}"
RUN git checkout ${NVM_VERSION} \
    && \. "./nvm.sh" \
    && nvm install "${NODE_VERSION}" \
    && echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> "${HOME}/.bashrc" \
    && echo '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"' >> "${HOME}/.bashrc"  

WORKDIR "${HOME}"

This is intended to work with bash (I don't have idea if works with another type of shell).这旨在与 bash 一起使用(我不知道是否可以与其他类型的 shell 一起使用)。 The command that I used to run the image was:我用来运行图像的命令是:

 docker run -ti --rm --name node_test <your-image-name | id-image> /bin/bash

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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