Docker image with multiple versions of ruby


This can generate Docker image which is installed multiple versions of ruby by rbenv.

The image is pushed at, tcnksm/rbenv, so you can use it soon.

$ docker pull tcnksm/rbenv

or in Dockerfile,

FROM tcnksm/rbenv


I will describe this Dockerfile and how to edit it for your own image.

FROM base

MAINTAINER tcnksm ""

# Install packages for building ruby
RUN apt-get update
RUN apt-get install -y --force-yes build-essential curl git
RUN apt-get install -y --force-yes zlib1g-dev libssl-dev libreadline-dev libyaml-dev libxml2-dev libxslt-dev
RUN apt-get clean

# Install rbenv and ruby-build
RUN git clone /root/.rbenv
RUN git clone /root/.rbenv/plugins/ruby-build
RUN ./root/.rbenv/plugins/ruby-build/
ENV PATH /root/.rbenv/bin:$PATH
RUN echo 'eval "$(rbenv init -)"' >> /etc/profile.d/ # or /etc/profile

# Install multiple versions of ruby
ENV CONFIGURE_OPTS --disable-install-doc
ADD ./versions.txt /root/versions.txt
RUN xargs -L 1 rbenv install < /root/versions.txt

# Install Bundler for each version of ruby
RUN echo 'gem: --no-rdoc --no-ri' >> /.gemrc
RUN bash -l -c 'for v in $(cat /root/versions.txt); do rbenv global $v; gem install bundler; done'

Basically, this Dockerfile does what you do when you install multiple versions of ruby by yourself in Ubuntu environment.

  1. Pull the base ubuntu image from (FROM base)
  2. Install packages for building ruby (RUN apt-get ...)
  3. Clone rbenv
  4. Clone ruby-build
  5. Set environmental variable for rbenv (ENV PATH /root/.rbenv/bin:$PATH)
  6. Set bash login command to read rbenv setting (RUN echo 'eval "$(rbenv init -)"' >> /etc/profile.d/
  7. Add versions.txt which is defined ruby version which you want to install
  8. Install ruby with rbenv install
  9. Install bundler for each versions

If you want to install another version (this time, only 1.8.7, 1.9.3, 2.0.0), just edit version.txt.

Furthermore, if you want to install basic rubygems by Gemfile, add below,

ADD ./Gemfile /root/Gemfile
RUN bash -l -c 'cd /root/; for v in $(cat rubies.txt); do rbenv global $v; bundle install; done'

To build image,

docker build -t USERNAME/IMAGENAME .

To push it to,

docker login