如何使用Dockerfile安装Ruby和Bundler?

3

我是一名初学者,对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用户? - Brad Werth
这是一个 Docker 容器,所以我认为不可能。 - David
我也不知道。为什么不直接忽略这个警告呢? - Brad Werth
来自该容器的完整日志如下:不要以root身份运行Bundler。如果需要,Bundler可以请求sudo,并且以root身份安装bundle将会破坏此应用程序对于此机器上所有非root用户的使用。 无法找到Gemfile。你知道关于“无法找到Gemfile”有什么信息吗? - David
运行 bundle 命令需要在目录中存在 Gemfile 文件,可能是因为没有 Gemfile 文件或者你所在的目录不正确。 - Brad Werth
我该怎么处理这种类型的故障?... 我没有部署任何 Ruby 应用程序,我只想将 Ruby 和 Bundler 安装为此 Docker 镜像的一部分,仅此而已... - David
1个回答

4
有预先构建的Ruby镜像(例如Alpine 3.11 Ruby 2.7),其中包括bundler。使用它们更容易入手,因为它们通常使用当前的“最佳实践”进行构建。
请注意,它们使用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”的问题。 - David
2.4-stretch 镜像将非常接近于 Ubuntu。 在运行 bundle 前,应该使用 COPYADDGemfile 和您的应用程序添加到镜像中。 - Matt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接