Problem Trying to Dockerize a Rails App with the Rugged Gem
This is a pretty specific post that I hope helps somebody out.
I’ve Dockerized a few Rails apps and it has usually been pretty easy. If I run into any problems it’s usually around a gem that uses a native C extension and the lack of some dependency that requires.
This time around I was bitten by the failure of the Rugged gem to compile. This gem requires
libgit2 and will try to build it. The documentation says it requires
cmake version 2.8 or greater. It also requires
pkg-config. My image included both of these but I still received this error:
Installing rugged 0.27.0 with native extensions Gem::Ext::BuildError: ERROR: Failed to build gem native extension. current directory: /usr/local/bundle/gems/rugged-0.27.0/ext/rugged /usr/local/bin/ruby -I /usr/local/lib/ruby/site_ruby/2.3.0 -r ./siteconf20190108-6-sa6jzw.rb extconf.rb checking for gmake... no checking for make... yes checking for cmake... yes checking for pkg-config... yes -- cmake .. -DBUILD_CLAR=OFF -DTHREADSAFE=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_FLAGS=-fPIC -DCMAKE_BUILD_TYPE=RelWithDebInfo *** 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.
By comitting and running the failed Docker image layer (bundle install) I was able to check
mkmf.log — as the error message suggests — and determine that I was missing
The following Dockerfile ended up working for me:
FROM ruby:2.3.7 RUN apt-get update && apt-get install -y \ build-essential \ cmake \ software-properties-common \ pkg-config \ openssl \ libssl-dev \ nodejs COPY Gemfile Gemfile.lock ./ RUN gem install bundler && bundle install --jobs 20 --retry 5 COPY . / CMD ["rm", "-f", "/rails/tmp/pids/server.pid"] EXPOSE 3000