Bundle install error in ruby docker image - ruby

I am trying to create a docker image using a ruby app using the docker file:
FROM jruby:latest
# Install apt based dependencies required to run Rails as
# well as RubyGems. As the Ruby image itself is based on a
# Debian image, we use apt-get to install those.
RUN apt-get update && apt-get install -y \
build-essential \
nodejs \
git \
ruby-dev \
gcc \
libffi-dev \
make \
zlib1g-dev \
libssl-dev \
libreadline6-dev \
libyaml-dev
# Configure the main working directory. This is the base
# directory used in any further RUN, COPY, and ENTRYPOINT
# commands.
RUN mkdir -p /app
WORKDIR /app
# Copy the Gemfile as well as the Gemfile.lock and install
# the RubyGems. This is a separate step so the dependencies
# will be cached unless changes to one of those two files
# are made.
COPY Gemfile Gemfile.lock ./
RUN gem install bundler && bundle install --jobs 20 --retry 5
# Copy the main application.
COPY . ./
# Expose port 3000 to the Docker host, so we can access it
# from the outside.
EXPOSE 3000
but i get an error when i'm trying to build this docker image
the gem install RedCloth is giving an error saying that I need to install development tools first.
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /usr/local/bundle/gems/RedCloth-4.3.2/ext/redcloth_scan
/opt/jruby/bin/jruby -r ./siteconf20181127-31-1zsapr.rb extconf.rb
checking for main() in -lc... RuntimeError: The compiler failed to generate an
executable file.
You have to install development tools first.
How do I fix this?

jruby has often has trouble with native extensions. In this case, the redcloth gem used to provide precompiled binaries for jruby but as of version 4.3 (the version you're attempting to install) they are no longer available:
Precompiled binary gems are provided for JRuby and Win32 platforms prior to version 4.3.
According to the README, you should be able to run rake compile to build the binaries. This may be a lot of extra work in your Docker image to make work, so I would recommend trying an older version of redcloth that has the binaries you need first. (keep in mind, the 4.2 series was released in 2011 and may not have the functionality you need)

Related

You don't have write permissions for the /usr/lib/ruby/gems/2.7.0 directory (Alpine linux docker image)

I can not install ruby's gems on alpine docker image. I've tried different approaches from other questions to solve ERROR: While executing gem ... (Gem::FilePermissionError), but there are solutions either for Ubuntu or for Mac OS.
Part of docker file code:
RUN set -ex \
&& apk add --no-cache --update ruby ruby-dev ruby-bundler \
&& gem install --no-document --source https://rubygems.org --version 3.6.6 inspec
OUTPUT:
+ apk add --no-cache --update ruby ruby-dev ruby-bundler
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/community/x86_64/APKINDEX.tar.gz
(1/11) Installing yaml (0.2.5-r0)
(2/11) Installing ruby-libs (2.7.3-r1)
(3/11) Installing ruby (2.7.3-r1)
(4/11) Installing ruby-etc (2.7.3-r1)
(5/11) Installing ruby-io-console (2.7.3-r1)
(6/11) Installing ruby-bundler (2.2.20-r0)
(7/11) Installing libgmpxx (6.2.1-r0)
(8/11) Installing gmp-dev (6.2.1-r0)
(9/11) Installing libucontext (1.1-r0)
(10/11) Installing libucontext-dev (1.1-r0)
(11/11) Installing ruby-dev (2.7.3-r1)
Executing busybox-1.33.1-r2.trigger
Executing glibc-bin-2.33-r0.trigger
/usr/glibc-compat/sbin/ldconfig: /usr/glibc-compat/lib/ld-linux-x86-64.so.2 is not a symbolic link
OK: 1409 MiB in 141 packages
+ gem install --no-document --source https://rubygems.org --version 3.6.6 inspec
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /usr/lib/ruby/gems/2.7.0 directory.
The command '/bin/sh -c set -ex && apk add --no-cache --update ruby ruby-dev ruby-bundler && gem install --no-document --source https://rubygems.org --version 3.6.6 inspec' returned a non-zero code: 1 ```
This seems to be a problem with docker versions earlier than 20.10.4, and at the time of writing this, DockerHub runs an older version.
Option 1:
If you have control over the version of your docker engine, upgrade it to the latest version (comments suggest at least version 20.10.4).
Option 2:
Use FROM alpine:3.13.
With the latest alpine (3.14) it breaks.
Option 3:
Use the official ruby alpine image. I tested with FROM ruby:3-alpine.
References:
The issue opened on alpine/aports
The relevant release notes section for alpine 3.14

Ruby gems with C extensions not building in Ubuntu

I'm trying to base a Dockerfile off of Heroku's new heroku/heroku:18 image (which uses Ubuntu 18.04) and running into some issues that I didn't have in their very old heroku/cedar:14 base image, I believe due to them having different preinstalled packages. (I updated the base image to heroku/heroku:18 because I want to use the webpacker gem and in Cedar-14 I was getting: Webpacker requires Node.js >= 6.0.0 and you are using 0.10.25. If there's an easy way to install a newer Node in the older base image I'd love to hear it!)
My problem is that no gems with C extensions (like puma, pg, msgpack, etc. etc.) install successfully with this new base Docker image. The error messages vary slightly from gem to gem but all generally say:
The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
Various GitHub issues with similar problems have had me try:
apt-get installing:
build-essential
libpq-dev
libssl-dev
openssl
libssl1.0-dev
gcc
gcc-5
automake
make
Using heroku/heroku:18-build instead of heroku/heroku:18
None of these solutions solves the issue.
For reference, here are the relevant parts of my Dockerfile:
FROM heroku/heroku:18
RUN apt-get update && apt-get install -y nodejs --no-recommends
RUN (mkdir -p /ruby && cd /ruby && curl https://s3-external-1.amazonaws.com/heroku-buildpack-ruby/heroku-18/ruby-2.5.0.tgz -s -o - | tar xzf - --no-same-owner)
ENV GEM_HOME=/ruby/gems GEM_PATH=/ruby/gems
RUN gem install bundler
RUN bundle install

How to require dependencies for ruby IronWorker

I put together a simple script to run in iron.io. I follow the steps in this tutorial but fail during testing.
$ docker run --rm -it -e "SLACK_API_TOKEN="xox-LALLALA"" -v "$PWD":/worker -w /worker iron/ruby ruby bubbebot.rb
bubbebot.rb:1:in `require_relative': cannot load such file -- /worker/slack-ruby-client (LoadError)
from bubbebot.rb:1:in `<main>'
It's failing whether I try require or relative_require. Any idea what I need to do to vendor this dependency?
That error message means you are trying to use something that is not in the iron/ruby image.
To understand how to fix this, let's take a look at the Dockerfile for that image:
FROM iron/base
RUN apk update && apk upgrade \
&& apk add libxml2 libxslt libevent libffi glib ncurses readline \
openssl yaml zlib curl mariadb-libs libpq ruby ruby-io-console \
ruby-bigdecimal \
&& rm -rf /var/cache/apk/*
If you want to use something that is not present on that build, you'll need to add it yourself. I would suggest making your own custom Docker image. Something along the lines of this should work:
FROM iron/ruby:2.3.1-dev
RUN apk add && apk update
RUN gem install slack --no-ri --no-rdoc
RUN rm -rf /var/cache/apk/*
Build that image and push it to Dockerhub and try running your code on that.

Bundler GemspecError when building inside Docker container

People were reporting this error and solved it by deleting a folder holding cached gems. Since I'm working inside a Docker container, this didn't seem like helpful advice for me. Indeed, the cache folder was not empty but did contain two items (I guess they are there from gem install bundle).
This is my (faulty) dockerfile
FROM nginx
RUN apt-get update -qqy && \
apt-get install -qqy \
build-essential \
ruby-full \
ruby-dev
RUN gem install bundle
RUN useradd -ms /bin/bash udo
USER udo
WORKDIR /home/udo
COPY . .
RUN bundle install
RUN bundle exec -- jekyll build
USER root
RUN cp -r _site/* /usr/share/nginx/html
I could fix the error by replacing RUN bundle install with
RUN bundle install --path=tmp
at the expense of having this local tmp directory that I then had to add to the exclude list in the _config.yml for Jekyll.
I'd prefer to know what was actually going on. I suspect there's a problem with the way I use the non-root user.
Note: bundle install --no-cache did not help.

How can I install Zephir on Ubuntu?

I would like to install Zephir on my Ubuntu machine.
The goal is to convert some of my existing code into a PHP extension, in order to get the benefits of faster execution.
Any pointers are more than appreciated.
Install zephir in Ubuntu..
You need download Zephir-master from https://github.com/phalcon/zephir
You nedd download json-c from https://github.com/json-c/json-c
1) sudo apt-get install gcc make re2c phpX phpX-json phpx-dev libpcre3-dev
2) Open folder Json-c
2.1) sh autogen.sh
2.2) ./configure
2.3) make && make install
3) Open folder zephir-master
3.1) ./install -c
4) zephir help for to test the correct installation
Please note Zephir is currently in Alpha stage and therefore bugs can be expected.
You need to have certain packages installed:
json-c
re2c
In the command line type:
sudo apt-get install libjson0 libjson0-dev libjson0-dbg
sudo apt-get install re2c
Once you have the required packages installed, you can generate the parser as follows:
./generate
Compile the extension (this is your code):
./zephir
The code produced is placed in ext/, there you can perform the standard compilation:
cd ext/
sudo ./install

Resources