无法在Docker中安装"rmagick"宝石

9

我有这个Dockerfile文件

FROM ruby:2.1.5

RUN apt-get update -qq && apt-get install -y build-essential

# for postgres
RUN apt-get install -y libpq-dev

# for nokogiri
RUN apt-get install -y libxml2-dev libxslt1-dev

# for capybara-webkit
RUN apt-get install -y libqt4-webkit libqt4-dev xvfb

# for a JS runtime
RUN apt-get install -y nodejs

# for rmagick
RUN apt-get install -y imagemagick libmagickwand-dev

ENV APP_HOME /lescollectionneurs
RUN mkdir $APP_HOME
WORKDIR $APP_HOME

ADD Gemfile* $APP_HOME/
RUN bundle install

ADD . $APP_HOME

同时这个Gemfile:

source 'https://rubygems.org'

ruby "2.1.5"

gem "rmagick", require: 'RMagick'

当我使用 docker build . 命令时:
sudo docker build .
Sending build context to Docker daemon 567.6 MB
Sending build context to Docker daemon
Step 0 : FROM ruby:2.1.5
 ---> afba1b22768e
Step 1 : RUN apt-get update -qq && apt-get install -y build-essential
 ---> Using cache
 ---> 542ba2520fd7
Step 2 : RUN apt-get install -y libpq-dev
 ---> Using cache
 ---> 242d31de9101
Step 3 : RUN apt-get install -y libxml2-dev libxslt1-dev
 ---> Using cache
 ---> 81fdba39c703
Step 4 : RUN apt-get install -y libqt4-webkit libqt4-dev xvfb
 ---> Using cache
 ---> e16c728f5879
Step 5 : RUN apt-get install -y nodejs
 ---> Using cache
 ---> 08a676ea038c
Step 6 : RUN apt-get install -y imagemagick libmagickwand-dev
 ---> Using cache
 ---> f6911106ff8d
Step 7 : ENV APP_HOME /lescollectionneurs
 ---> Using cache
 ---> d4740491742f
Step 8 : RUN mkdir $APP_HOME
 ---> Using cache
 ---> f439a8295600
Step 9 : WORKDIR $APP_HOME
 ---> Using cache
 ---> 0ffdb5e33df1
Step 10 : ADD Gemfile* $APP_HOME/
 ---> Using cache
 ---> 8867a3e40baf
Step 11 : RUN bundle install
 ---> Running in fd98e604631d
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.
Fetching gem metadata from https://rubygems.org/..
Fetching version metadata from https://rubygems.org/.
Resolving dependencies...

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /usr/local/bin/ruby extconf.rb
checking for Ruby version >= 1.8.5... yes
checking for gcc... yes
checking for Magick-config... no
Can't install RMagick 2.13.2. Can't find Magick-config in /usr/local/bundle/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

*** 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

extconf failed, exit code 1

Gem files will remain installed in /usr/local/bundle/gems/rmagick-2.13.2 for inspection.
Results logged to /usr/local/bundle/extensions/x86_64-linux/2.1.0-static/rmagick-2.13.2/gem_make.out
An error occurred while installing rmagick (2.13.2), and Bundler cannot
continue.
Make sure that `gem install rmagick -v '2.13.2'` succeeds before bundling.
INFO[0014] The command [/bin/sh -c bundle install] returned a non-zero code: 5

它无法正常工作,因为它找不到/usr/bin/。

当我运行sudo docker run -t -i lescollectionneurs /bin/bash时,我可以这样做:

root@4b015bacf80d:/# apt-get install -y imagemagick libmagickwand-dev
#..... Installing successfully
root@4b015bacf80d:/# /usr/bin/Magick
Magick-config      MagickCore-config  MagickWand-config

root@4b015bacf80d:/# /usr/bin/Magick-config
Usage: Magick-config [--cflags] [--cppflags] [--exec-prefix] [--ldflags] [--libs] [--prefix] [--version]
Example: gcc `Magick-config --cflags --cppflags` -o core core.c `Magick-config --ldflags --libs`

root@4b015bacf80d:/# gem install rmagick
Fetching: rmagick-2.15.0.gem (100%)
Building native extensions.  This could take a while...
Successfully installed rmagick-2.15.0
Parsing documentation for rmagick-2.15.0
Installing ri documentation for rmagick-2.15.0
Done installing documentation for rmagick after 7 seconds
1 gem installed

如您所见,我可以通过打开bash来安装imagemagick。安装后,可用的二进制文件就可用了,然后我可以安装rmagick。

为什么在Dockerfile中结果不同?如何使用Dockerfile安装rmagick?

2个回答

6

我认为你缺少libmagickcore-dev库,这会使您成功编译rmagick失败。尝试以下操作:

RUN apt-get install imagemagick libmagickcore-dev libmagickwand-dev

它不起作用。可能与此有关:http://stackoverflow.com/questions/30313717/i-cannot-install-applications-by-building-with-a-dockerfile。 - Dougui
虽然与您的问题无关,但您应该将所有的 RUN apt-get install -y xxx 分组在一个命令中,以使用更少的层,请参见 https://docs.docker.com/articles/dockerfile_best-practices/ 提取 "请按以下方式编写指令:RUN apt-get update && apt-get install -y package-bar package-foo package-baz这样编写指令不仅使其更易于阅读和维护,而且通过包含 apt-get update,确保缓存自然被清除,并且最新版本将被安装,无需进一步的编码或手动干预。" - user2915097

4

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