How to install nvm in docker?

后端 未结 14 959
陌清茗
陌清茗 2020-12-04 05:53

I am in the process of building a new Docker image and I\'m looking to get NVM installed so I can manage nodejs.

Reading the docs on how to install NVM they mention

14条回答
  •  感动是毒
    2020-12-04 06:58

    Update 20/02/2020: This solution works if you're using a debian base image. If you're using ubuntu, see this answer.

    Here is the cleanest way to install nvm that I have found:

    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. 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.

    • The second line installs nvm with bash. When the script is run with bash, it appends to the ~/.bashrc file.

    • The third line installs a particular version of nodejs and uses it. The nvm, npm, and node commands are available because they are run via a bash login shell (see line 1).

提交回复
热议问题