mles
mles

Reputation: 3456

How to run a ruby gem in a centos or ruby docker container?

I'm trying to add badges to app icons in our builds. There is a ruby gem called badge, which does a pretty good job (https://github.com/HazAT/badge).

I've tried installing badge on our macos vm, which is used for building apps. It breaks other things, as it has lots of dependencies. So I've tried to run badge in the official ruby docker container

docker run -ti ruby

I wasn't able to install anything, as there is no wget nor gem preinstalled.

irb(main):001:0> gem
Traceback (most recent call last):
        3: from /usr/local/bin/irb:11:in `<main>'
        2: from (irb):1
        1: from /usr/local/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_gem.rb:44:in `gem'
ArgumentError (wrong number of arguments (given 0, expected 1+))
irb(main):002:0> wget
Traceback (most recent call last):
        2: from /usr/local/bin/irb:11:in `<main>'
        1: from (irb):2
NameError (undefined local variable or method `wget' for main:Object)
irb(main):003:0> 

Then I've tried the centos image

docker run -ti centos

I was able to install ruby and needed libraries and run gem

yum -y install ruby gcc ruby-devel rubygems libcurl libcurl-devel make

Now I'm stuck with this error when running gem install badge:

ERROR:  Error installing badge:
    ERROR: Failed to build gem native extension.

    /usr/bin/ruby extconf.rb
checking for main() in -lstdc++... no
creating Makefile

make "DESTDIR="
g++ -I. -I/usr/include -I/usr/include/ruby/backward -I/usr/include -I.   -fPIC -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -mtune=generic -m64 -o unf.o -c unf.cc
make: g++: Command not found
make: *** [unf.o] Error 127

What is the proper way to run a ruby gem in a docker container? Should I use the ruby docker container? If not, how do I get this to run in another container? Doesn't have to be centos. This was just the first image that came to my mind.

Upvotes: 1

Views: 1029

Answers (1)

Panic
Panic

Reputation: 2405

Docker is a great tool for this task.

Dockerfile

FROM ruby:2.4.2-alpine3.6

RUN apk add --no-cache curl-dev \
  && apk add --no-cache --virtual .builddeps build-base \
  && gem install badge -v 0.8.7 \
  && apk del .builddeps

Build the image

$ docker build . --tag badge:0.8.7

Run badge

$ docker run --rm badge:0.8.7 badge --help

Upvotes: 2

Related Questions