簡體   English   中英

使用 Docker 構建 Rails 失敗。 當我使用 ruby​​:alpine 圖像時,Nokogiri 導致錯誤

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

我有一個裸機 Rails 5.x,我正在嘗試使用 docker 進行工作。

我正在使用谷歌雲構建來構建我的圖像。 當我在 Ubuntu 18.x 上執行 docker build 時,它工作正常。

我的 Dockerfile 看起來像這樣:

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?

我很困惑,是 Dockerfile 的問題還是構建 docker 映像的環境(即 google cloud build)的問題?

我的日志文件輸出說我需要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 映像的系統庫安裝 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM