在Docker上使用RVM安装时,Ruby和Rails“找不到路径”

时间:2017-06-16 16:31:40

标签: docker rvm dockerfile codenvy

我正在使用docker文件在Codenvy上创建自定义工作区。当我创建工作区时,一切似乎都正确安装,但当工作区打开并输入ruby -v时,我得到:

bash: ruby: command not found

rails相同:

user@de98ec223612:/projects$ rails -v
bash: rails: command not found

如果我输入rvm列表,我会

user@de98ec223612:/projects$ rvm info

ruby-2.4.0:

  system:
    uname:        "Linux de98ec223612 3.10.0-514.10.2.el7.x86_64 #1 SMP Fri Mar 3 00:04:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux"
    name:         "Ubuntu"
    version:      "16.04"
    architecture: "x86_64"
    bash:         "/bin/bash => GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)"
    zsh:          " => not installed"
    remote path:  "ubuntu/16.04/x86_64"

  rvm:
    version:      "rvm 1.29.1 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io/]"
    updated:      "5 minutes 31 seconds ago"
    path:         "/home/user/.rvm"
    autolibs:     "[4] Allow RVM to use package manager if found, install missing dependencies, install package manager (only OS X)."

  ruby:
    interpreter:  "ruby"
    version:      "2.4.0p0"
    date:         "2016-12-24"
    platform:     "x86_64-linux"
    patchlevel:   "2016-12-24 revision 57164"
    full_version: "ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]"

  homes:
    gem:          "/home/user/.rvm/gems/ruby-2.4.0"
    ruby:         "/home/user/.rvm/rubies/ruby-2.4.0"

binaries:
    ruby:         "/home/user/.rvm/rubies/ruby-2.4.0/bin/ruby"
    irb:          "/home/user/.rvm/rubies/ruby-2.4.0/bin/irb"
    gem:          "/home/user/.rvm/rubies/ruby-2.4.0/bin/gem"
    rake:         "/home/user/.rvm/rubies/ruby-2.4.0/bin/rake"

  environment:
    PATH:         "/home/user/.rvm/gems/ruby-2.4.0/bin:/home/user/.rvm/gems/ruby-2.4.0@global/bin:/home/user/.rvm/rubies/ruby-2.4.0/bin:/usr/lib/jvm/java-1.8.0-open
jdk-amd64/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bundle/bin:/usr/lib/postgresql/9.6/bin:/home/user/.rvm/bin"
    GEM_HOME:     "/home/user/.rvm/gems/ruby-2.4.0"
    GEM_PATH:     "/home/user/.rvm/gems/ruby-2.4.0:/home/user/.rvm/gems/ruby-2.4.0@global"
    MY_RUBY_HOME: "/home/user/.rvm/rubies/ruby-2.4.0"
    IRBRC:        "/home/user/.rvm/rubies/ruby-2.4.0/.irbrc"
    RUBYOPT:      ""
    gemset:       ""

在我看来,事情是正确安装的。

如果我输入/home/user/.rvm/rubies/ruby-2.4.0/bin/ruby -v,它会显示正确的ruby版本。 /home/user/.rvm/rubies/ruby-2.4.0/bin在PATH中为什么不被搜索?

如果有帮助,这是我的docker文件的相关部分。

# Install RVM
RUN gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB &&\
    echo progress-bar >> ~/.curlrc &&\
    \curl -sSL https://get.rvm.io | bash -s stable &&\
    /bin/bash -l -c 'source ~/.rvm/scripts/rvm'

# Install Ruby
RUN /bin/bash -l -c 'rvm requirements' &&\
    /bin/bash -l -c 'rvm install ${RUBY_VERSION}' &&\
    /bin/bash -l -c 'rvm use ${RUBY_VERSION} --default' &&\
    /bin/bash -l -c 'rvm rubygems current'

# Install bundler
RUN /bin/bash -l -c 'gem install bundler'

# Install Rails
RUN /bin/bash -l -c 'gem install rails'

RUN echo $PATH

EXPOSE 3000
WORKDIR /projects    

CMD /bin/bash -l -c 'source ~/.rvm/scripts/rvm' && sudo service postgresql start && tail -f /dev/null

如何在不使用/home/user/.rvm/rubies/ruby-2.4.0/bin的情况下使这些命令正常运行?

2 个答案:

答案 0 :(得分:0)

如果你的团队可以接受,那么从Eclipse Che为RoR提供的基础dockerfile开始可能更容易:https://github.com/eclipse/che-dockerfiles/blob/master/recipes/ruby_rails/Dockerfile

这应该为您提供基础知识,并由社区保持最新状态。然后,您可以使用您需要的任何其他工具,库,deps扩展它。

答案 1 :(得分:0)

我遇到了同样的问题,最后通过上传以

结尾的/root/.bashrc文件修复了它
source /opt/rvm/scripts/rvm
source /etc/profile.d/ruby.sh

export PATH=$PATH:/opt/rvm/bin

带有源的行,由我添加。 RVM在安装时添加了导出。我的bashrc文件的其余部分是Debian安装的内容。我刚刚添加了这些行。

我的ruby.sh文件包含

#!/bin/sh
export RUBY_HOME=/opt/rvm/rubies/default

对我来说,这就是诀窍。当我使用/ bin / bash(非shell)或通过/ bin / bash -l -c命令登录时,我的命令rvm和ruby都响应。另外,我的RUBY_HOME和GEM_PATH ......都已填满。

我目前的红宝石码头安装可以在这里找到: https://github.com/vindevoy/dockerhub-debian-ruby(来源) https://hub.docker.com/r/vindevoy/debian-ruby/(图片)