【问题标题】:gem command not found when install rbenv in debian在 debian 中安装 rbenv 时找不到 gem 命令
【发布时间】:2021-01-03 23:11:04
【问题描述】:

我正在创建一个 Dockerfile 来运行 truffleruby。尝试安装捆绑程序和工头时出现错误。错误是/bin/sh: 1: gem: not found

Dockerfile

FROM debian:buster-slim

# Install packages for building ruby
RUN apt update -y && apt install -y git curl libssl-dev libpq-dev libreadline-dev zlib1g-dev \
  autoconf bison build-essential libyaml-dev \
  libreadline-dev libncurses5-dev libffi-dev libgdbm-dev
RUN apt clean

# Install rbenv and ruby-build
RUN git clone https://github.com/sstephenson/rbenv.git /root/.rbenv
RUN git clone https://github.com/sstephenson/ruby-build.git /root/.rbenv/plugins/ruby-build
RUN /root/.rbenv/plugins/ruby-build/install.sh
ENV PATH /root/.rbenv/bin:$PATH
RUN echo 'eval "$(rbenv init -)"' >> /etc/profile.d/rbenv.sh # or /etc/profile
RUN echo 'eval "$(rbenv init -)"' >> .bashrc
RUN . ~/.bashrc

RUN rbenv install truffleruby-20.3.0
RUN rbenv global truffleruby-20.3.0
RUN rbenv rehash

ENV BUNDLER_VERSION=2.2.4 NODE_ENV=production RAILS_ENV=production RAILS_SERVE_STATIC_FILES=true RAILS_LOG_TO_STDOUT=true PORT=3000
ENV CONFIGURE_OPTS --disable-install-doc

RUN apt-get install -y curl -sL https://deb.nodesource.com/setup_14.x | bash - && \
  apt-get update && apt-get install -y nodejs && \
  apt-get clean

RUN rbenv versions
RUN gem install bundler:2.2.4 foreman

RUN mkdir /app
WORKDIR /app
COPY Gemfile Gemfile.lock ./
RUN bundle config set --local deployment 'true'
RUN bundle config set --local without 'development test'
RUN bundle install
COPY . .


EXPOSE 3000
CMD ["foreman", "start"]

构建的尾部

Removing intermediate container 1a445fde7fc0
 ---> 43c3d72b7eb6
Step 17/27 : RUN rbenv versions
 ---> Running in feb5bb9361cc
* truffleruby-20.3.0 (set by /root/.rbenv/version)
Removing intermediate container feb5bb9361cc
 ---> c7d1a5826af5
Step 18/27 : RUN gem install bundler:2.2.4 foreman
 ---> Running in 998461afc89c
/bin/sh: 1: gem: not found
The command '/bin/sh -c gem install bundler:2.2.4 foreman' returned a non-zero code: 127

【问题讨论】:

标签: ruby linux docker debian truffleruby


【解决方案1】:

您通常不会在 Docker 中使用像 rbenv 这样的版本管理器。这有几个原因。一是图像通常只包含一个应用程序及其单个运行时,因此图像中永远不会有多个 Ruby,因此无需切换。第二个是运行容器的最常见路径(包括docker run 和Dockerfile RUN 指令)不查看.bashrc/etc/profile 之类的shell 点文件,因此版本管理器设置将永远无法运行。

TruffleRuby 以standalone tar file 的形式分发(以及其他方式),因此您可以将其安装在您的 Dockerfile 中。我会让 Dockerfile 看起来大致如下:

FROM debian:buster-slim
# Install the specific dependency packages TruffleRuby recommends
# (build-essential is much larger but might actually be necessary)
RUN apt-get update \
 && DEBIAN_FRONTEND=noninteractive \
    apt-get install --no-install-recommends --assume-yes \
      curl \
      gcc \
      libssl-dev \
      libz-dev \
      make

# Download and unpack TruffleRuby
ARG TRUFFLERUBY_VERSION=20.3.0
ENV PATH /opt/truffleruby-$TRUFFLERUBY_VERSION-linux-amd64/bin:$PATH
RUN cd /opt \
 && curl -L https://github.com/oracle/truffleruby/releases/download/vm-$TRUFFLERUBY_VERSION/truffleruby-$TRUFFLERUBY_VERSION-linux-amd64.tar.gz | tar xz \
 && /opt/truffleruby-$TRUFFLERUBY_VERSION-linux-amd64/lib/truffle/post_install_hook.sh

# Now build and install your application
RUN gem install bundler:2.2.4
WORKDIR /app
COPY Gemfile Gemfile.lock ./
RUN bundle config set --local deployment 'true'
RUN bundle config set --local without 'development test'
RUN bundle install
COPY . .
ENTRYPOINT ["bundle", "exec"]
EXPOSE 3000
CMD ["rails", "start"]

您可以合理地将其拆分为两个单独的 Dockerfile。在“构建并安装您的应用程序”注释之前结束第一个,并使用docker build -t myname/truffleruby:20.3.0 -f Dockerfile.truffleruby . 构建它。然后第二个可以像标准 Docker Hub ruby 镜像一样以FROM myname/truffleruby:20.3.0 开头。

【讨论】:

【解决方案2】:

CRuby 也同样适用吗?

我怀疑RUN 不读取shell 文件,所以PATH 需要修改。

RUN git clone https://github.com/sstephenson/rbenv.git /root/.rbenv
...
RUN /root/.rbenv/plugins/ruby-build/install.sh
ENV PATH /root/.rbenv/bin:$PATH

好像有点奇怪,是rbenv克隆安装在同一个地方吗?

我会跳过 Docker 中的 rbenv,而只是:

RUN ruby-build truffleruby ~/.rubies/truffleruby
ENV PATH $HOME/.rubies/truffleruby/bin:$PATH

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    相关资源
    最近更新 更多