Using Ruby 2.5.0 with Docker

  

Docker.jpg

Ruby 2.5.0 was released last Christmas and it's available through the usual places -- rvm, rbenv, chruby, homebrew. One tool I like to use is Docker.

Docker makes it easy to run containers. It's used in production, testing, and development. This tutorial focuses on development. I particularly like using Docker when a new version of Ruby is released to quickly try the new features. I can also check if my app's gems will install correctly or if I need to upgrade them.

By using Docker, I can remove the image when I'm done. Not everyone can move to the latest Ruby version right away. With this method, I can try Ruby 2.5.0 without installing it permanently.

First, you need Docker. I use Docker for Mac. I also previously used Docker Machine. Docker is also available on Windows and Linux.

The official ruby image is available on Docker Hub. To use it, run

docker run -it --rm ruby:2.5

If this is the first time you run the command, Docker will pull the image remotely. When it's done, it will start the default program. In this case, irb.

Unable to find image 'ruby:2.5' locally
2.5: Pulling from library/ruby
723254a2c089: Pull complete 
abe15a44e12f: Pull complete 
409a28e3cc3d: Pull complete 
503166935590: Pull complete 
0f46f97746e4: Pull complete 
9d641d922d2f: Pull complete 
891f591c9621: Pull complete 
825fab4554b7: Pull complete 
Digest: sha256:090577ac73868da6c74a2c5d716e67189e8f6c597b1d4bfbe61e8a68c829c02e
Status: Downloaded newer image for ruby:2.5
irb(main):001:0> RUBY_VERSION
=> "2.5.0"

You can now try the new features included in Ruby 2.5.0. I wrote about some of the highlights here.

For example, yield_self is a new method.

irb(main):002:0> 3.yield_self{ |x| x*x }
=> 9

If you want to check how your code behaved in an older Ruby version, you can also use Docker for that. Instead of pulling the image ruby:2.5 like we did above, we'll pull ruby:2.4.

$ docker run -it --rm ruby:2.4
irb(main):001:0> RUBY_VERSION
=> "2.4.3"
irb(main):002:0> 3.yield_self{ |x| x*x }
NoMethodError: undefined method `yield_self' for 3:Integer
    from (irb):2
    from /usr/local/bin/irb:11:in `<main>'

Since yield_self was only added to Ruby 2.5.0, you get NoMethodError: undefined method when running it on Ruby 2.4.3.

Another thing I try with new Ruby versions is check if the gems for my Rails app can be installed. Here I'm using Engine Yard's todo app.

docker run -it --rm -v /Users/crigor/projects/todo:/todo ruby:2.5 bash

This command uses -v which creates a volume. The contents of my local directory /Users/crigor/projects/todo will be available inside the container's /todo.

Inside the container we can install the gems using bundle install.

root@7c0b216b97c3:/# cd todo
root@7c0b216b97c3:/todo# bundle install
Fetching gem metadata from https://rubygems.org/..............
.
.
.

An error occurred while installing puma (2.10.2), and Bundler cannot continue.
Make sure that `gem install puma -v '2.10.2'` succeeds before bundling.

We can see that puma 2.10.2 failed to install on Ruby 2.5.0. Looking at the available puma versions, we can see that 2.10.2 is an old version. I tried updating to the latest version and it installed successfully.

root@7c0b216b97c3:/todo# bundle update puma

While puma 2.10.2 is old, note that it can still be installed without issues on Ruby 2.4.3. It is problems like this that can easily be checked using Docker. Not everyone can update to the latest Ruby version, especially in production. You usually want to give it time for some problems to surface before migrating to a new version. But you should start checking how much work needs to be done so you have an idea when the time comes.

If you have non-critical applications, you should consider using the Ruby 2.5.0 sooner rather than later. The community will thank you for that.

 Are you ready to Deploy??
Start a 500 Hours Free Trial
START MY TRIAL!

logo.svg Deploy | Run | Scale

Related posts

RubyConf Round Up

November 27, 2018

The City: Los Angeles, California

Read More

It's Almost Time For Ruby Conf 2018!

November 6, 2018

It’s autumn and November is right around the corner! We all know what this means…

Read More

Jekyll and Engine Yard: A Match Made in The Clouds

October 29, 2018

Blogging is without a doubt the most important way to get your ideas out to the world, whether

Read More

Christopher Rigor

 
Christopher Rigor is a Senior Technical Evangelist at Engine Yard. He’s a long time Rails user, system administrator, and recently became a contributor of RailsInstaller. Previously, he was the DevOps Support Manager for Asia-Pacific at Engine Yard.
Find me on:

Comments

Subscribe Here!