Rails 使用 Docker 构建失败。当我使用 ruby:alpine 图像时,Nokogiri 导致错误

Rails build is failing using Docker. Nokogiri is causing the error when I use ruby:alpine image

我有一个基本的 Rails 5.x,我正在尝试使用 docker。

我正在使用 google 云构建来构建我的图像。当我在 Ubuntu 18.x 上执行 docker 构建时,它工作正常。

我的 Docker 文件如下所示:

FROM ruby:2.5-alpine

RUN apk update && apk --update add \
 build-base \
 nodejs \
 postgresql-dev \
 tzdata \
 imagemagick 

#RUN apt-get install -y build-essential

# yarn
ENV PATH=/root/.yarn/bin:$PATH
RUN apk add --virtual build-yarn curl && \
    touch ~/.bashrc && \
    curl -o- -L https://yarnpkg.com/install.sh | sh && \
    apk del build-yarn


RUN mkdir /app
WORKDIR /app

COPY Gemfile ./
RUN gem install bundler -v 2.0.1
RUN gem uninstall bundler --version '<2.0.0'
RUN gem install nokogiri -v '1.10.1' --source 'https://rubygems.org/'
RUN bundle install --binstubs

当我构建我的 Docker 图像时,出现此错误:

Successfully installed mini_portile2-2.4.0 Building native extensions.
This could take a while... ERROR:  Error installing nokogiri:   ERROR:
Failed to build gem native extension.

    current directory: /usr/local/bundle/gems/nokogiri-1.10.1/ext/nokogiri
/usr/local/bin/ruby -I /usr/local/lib/ruby/site_ruby/2.5.0 -r
./siteconf20190303-1-7m260v.rb extconf.rb checking if the C compiler
accepts ... *** extconf.rb failed *** Could not create Makefile due to
some reason, probably lack of necessary libraries and/or headers. 
Check the mkmf.log file for more details.  You may need configuration
options.

Provided configuration options:     --with-opt-dir  --without-opt-dir
    --with-opt-include  --without-opt-include=${opt-dir}/include
    --with-opt-lib  --without-opt-lib=${opt-dir}/lib    --with-make-prog
    --without-make-prog     --srcdir=.  --curdir
    --ruby=/usr/local/bin/$(RUBY_BASE_NAME)     --help  --clean
/usr/local/lib/ruby/2.5.0/mkmf.rb:456:in `try_do': The compiler failed
to generate an executable file. (RuntimeError) You have to install
development tools first.    from
/usr/local/lib/ruby/2.5.0/mkmf.rb:574:in `block in try_compile'     from
/usr/local/lib/ruby/2.5.0/mkmf.rb:521:in `with_werror'  from
/usr/local/lib/ruby/2.5.0/mkmf.rb:574:in `try_compile'  from
extconf.rb:138:in `nokogiri_try_compile'    from extconf.rb:162:in
`block in add_cflags'   from /usr/local/lib/ruby/2.5.0/mkmf.rb:632:in
`with_cflags'   from extconf.rb:161:in `add_cflags'     from
extconf.rb:416:in `<main>'

To see why this extension failed to compile, please check the mkmf.log
which can be found here:


/usr/local/bundle/extensions/x86_64-linux/2.5.0/nokogiri-1.10.1/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in
/usr/local/bundle/gems/nokogiri-1.10.1 for inspection. Results logged
to
/usr/local/bundle/extensions/x86_64-linux/2.5.0/nokogiri-1.10.1/gem_make.out
The command '/bin/sh -c gem install nokogiri -v '1.10.1' --source
'https://rubygems.org/'' returned a non-zero code: 1 ERROR ERROR:
build step 0 "gcr.io/cloud-builders/docker" failed: exit status 1

我是否需要使用其他支持 build-essential 的图像类型? 还有其他安装 nokogiri 的方法吗?

我很困惑,是 Docker 文件的问题还是构建 docker 图像的环境问题,即 google 云构建?

我的日志文件输出说我需要 gmp-dev

这是我的 docker alpine 配置的最终输出,适用于我的情况:

RUN apk add --no-cache --update \
    build-base \
    linux-headers \
    git \
    postgresql-dev \
    nodejs \
    yarn \
    tzdata \
    graphviz \
    gmp-dev

这就是我为 ruby:2.6.6-alpine docker image:

使用系统库安装 nokogiri gem 所做的工作

Dockerfile 添加内容

RUN apk add --update --no-cache build-base git nodejs tzdata libxml2-dev libxslt-dev

RUN gem install bundler
RUN bundle config build.nokogiri --use-system-libraries
RUN bundle install