如何在 docker 中安装 nvm? [英] How to install nvm in docker?

查看:46
本文介绍了如何在 docker 中安装 nvm?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个新的 Docker 镜像,我希望安装 NVM,以便我可以管理 nodejs.

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

阅读有关如何安装 NVM 的文档,他们提到您需要获取 .bashrc 文件才能开始使用 NVM.

Reading the docs on how to install NVM they mention that you need to source your .bashrc file in order to start using NVM.

我尝试在 Dockerfile 中进行设置,但到目前为止构建失败并显示错误:

I've tried to set this up in a Dockerfile, but so far building fails with the error:

"bash: nvm: command not found"

"bash: nvm: command not found"

以下是我的 Dockerfile 中的相关行:

Here are the relevant lines from my 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"

这是尝试构建的输出:

docker build -t nginx_dock .

docker build -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

我对 Docker 还很陌生,所以我可能缺少一些编写 Dockerfiles 的基本知识,但到目前为止,我所做的所有阅读都没有向我展示一个好的解决方案.

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.

推荐答案

当您每次 RUN bash... 在单独的进程中运行时,环境中设置的任何内容都不会得到维护.下面是我安装 nvm 的方法:

When you RUN bash... each time that runs in a separate process, anything set in the environment is not maintained. Here's how I install 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

这篇关于如何在 docker 中安装 nvm?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆