【问题标题】:How to install ruby and bundler with dockerfile?如何使用 dockerfile 安装 ruby​​ 和 bundler?
【发布时间】:2017-10-18 21:15:04
【问题描述】:

这里是 ruby​​ 和 bundler 的新手。 我正在使用此 docker 文件将它们安装在 docker 映像中:

FROM alpine:3.5

# Install Ruby, Ruby Bundler and other ruby dependencies

RUN apk add --update \
      ruby ruby-bigdecimal ruby-bundler \
      ca-certificates libressl \
      libressl-dev build-base ruby-dev \
      ruby-rdoc ruby-io-console ruby-irb; \
\
    && bundle config build.nokogiri --use-system-libraries; \
    && bundle config git.allow_insecure true; \
\
    && gem install json foreman --no-rdoc --no-ri; \
    && gem cleanup; \
    && rm -rf /usr/lib/ruby/gems/*/cache/*; \
    && apk del libressl-dev build-base ruby-dev; \
    && rm -rf /var/cache/apk/* /tmp;

CMD ["bundle"]

当我运行 docker run 时,我得到:

Don't run Bundler as root. Bundler can ask for sudo if it is needed,
and installing your bundle as root will break this application for all
non-root users on this machine.
Could not locate Gemfile or .bundle/ directory

我该如何解决这个问题?我只想安装 ruby​​ 和 ruby​​-bundle 并完成这个...

【问题讨论】:

  • 机器有非root用户吗?
  • 这是一个 docker 容器,所以我不这么认为。
  • 我也没有。为什么不直接忽略警告?
  • 来自该容器的完整日志是:不要以 root 身份运行 Bundler。如果需要,Bundler 可以请求 sudo,并且以 root 身份安装您的 bundle 将破坏此计算机上所有非 root 用户的应用程序。找不到 Gemfile。你知道这个“找不到 Gemfile”吗?
  • 运行bundle需要目录中有gemfile,要么没有gemfile,要么你在错误的目录

标签: ruby docker bundler dockerfile root


【解决方案1】:

有包含bundler 的预构建ruby 图像(例如Alpine 3.11 Ruby 2.7)。从它们开始更容易,因为它们通常使用当前的“最佳实践”来构建。

请注意,他们在映像构建中使用ENV 指令设置了BUNDLE_SILENCE_ROOT_WARNING 环境变量,以删除root 警告。

您通常也不会将bundler 作为容器的CMD 运行,但您可以在RUN 映像构建步骤中运行bundler

在任何情况下,以非 root 用户身份运行容器都不是一个坏主意。使用 USER 指令来改变它。

FROM ruby:2.7
WORKDIR /app
ADD . /app/
RUN set -uex; \
    bundle install; \
    adduser -D rubyapp; \
    mkdir -p /app/data; \
    chown rubyapp /app/data
USER rubyapp
CMD [ "ruby", "whatever.rb" ]

【讨论】:

  • 我决定从一个 ubuntu 镜像开始构建这个容器。其实现在我只处理“找不到 Gemfile”
  • 2.4-stretch 图像将非常接近 Ubuntu。 Gemfile 和您的应用程序应在运行 bundle 之前将 COPYd 或 ADDed 到图像中。
猜你喜欢
  • 2017-05-22
  • 1970-01-01
  • 2020-07-28
  • 1970-01-01
  • 2021-01-07
  • 1970-01-01
  • 2022-01-04
  • 2016-11-05
  • 2023-03-10
相关资源
最近更新 更多