bash 如何在 Docker 中正确安装 RVM?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43612927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:04:23  来源:igfitidea点击:

How to correctly install RVM in Docker?

rubybashdockerrvm

提问by yegor256

This is what I have in my Dockerfile:

这是我在我的Dockerfile

RUN gpg2 --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3
RUN curl -L https://get.rvm.io | bash -s stable
RUN /bin/bash -l -c "rvm requirements"
RUN /bin/bash -l -c "rvm install 2.3.3"

Works just fine, however, when I start the container, I see this:

工作得很好,但是,当我启动容器时,我看到:

$ docker -it --rm myimage /bin/bash
/root# ruby --version
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux]
/root# /bin/bash -l -c "ruby --version"
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]

Obviously, this is not what I want. As far as I understand, the problem is that bashdoesn't run /etc/profileby default. That's why Ruby is not coming from the RVM installation. How can I fix that?

显然,这不是我想要的。据我了解,问题是默认情况下bash不运行/etc/profile。这就是 Ruby 不是来自 RVM 安装的原因。我该如何解决?

回答by Зелёный

Long story short:

长话短说:

docker -it --rm myimage /bin/bashcommand does not start bash as a login shell.

docker -it --rm myimage /bin/bash命令不会将 bash 作为登录 shell 启动。

Explanation:

解释:

When you run $ docker -it --rm myimage /bin/bashit's invoke bash without the -loption which make bashact as if it had been invoked as a login shell, rvminitializations depends on the source-ing /path/to/.rvm/scripts/rvmor /etc/profile.d/rvm.shand that initialization is in .bash_profileor .bashrcor any other initialization scripts.

当您运行$ docker -it --rm myimage /bin/bash它时,它会在没有-l选项的情况下调用 bash,该选项bash就好像它已作为登录 shell 被调用一样,rvm初始化取决于source-ing /path/to/.rvm/scripts/rvmor/etc/profile.d/rvm.sh并且该初始化在.bash_profileor.bashrc或任何其他初始化脚本中。

How can I fix that?

我该如何解决?

If you won't, always have the rubyfrom rvmadd -loption.

如果您不这样做,请始终使用rubyfrom rvmadd-l选项。

Here is a Dockerfile with installed rubyby rvm:

这是一个 Dockerfile,安装ruby者为rvm

FROM Debian

ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update -q && \
    apt-get install -qy procps curl ca-certificates gnupg2 build-essential --no-install-recommends && apt-get clean

RUN gpg2 --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3
RUN curl -sSL https://get.rvm.io | bash -s
RUN /bin/bash -l -c ". /etc/profile.d/rvm.sh && rvm install 2.3.3"
# The entry point here is an initialization process, 
# it will be used as arguments for e.g.
# `docker run` command 
ENTRYPOINT ["/bin/bash", "-l", "-c"]

Run the container:

运行容器:

? docker_templates : docker run -ti --rm rvm 'ruby -v'
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]
? docker_templates : docker run -ti --rm rvm 'rvm -v'
rvm 1.29.1 (master) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io/]
? docker_templates : docker run -ti --rm rvm bash
root@efa1bf7cec62:/# rvm -v
rvm 1.29.1 (master) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io/]
root@efa1bf7cec62:/# ruby -v
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]
root@efa1bf7cec62:/# 

回答by ALev

Don't use RUN bash -l -c rvm install 2.3.3. It's very scruffy. You can set shell command by SHELL [ "/bin/bash", "-l", "-c" ]and simply call RUN rvm install 2.3.3.

不要使用RUN bash -l -c rvm install 2.3.3. 它非常邋遢。您可以通过SHELL [ "/bin/bash", "-l", "-c" ]简单地调用来设置 shell 命令RUN rvm install 2.3.3