Containerization & Ruby on Rails (ROR)

Okan Sungur
5 min readFeb 17, 2022

Containerization is a packaged software, with all the necessary components that will make it work properly. The components can be listed as frameworks, dependencies, and related configuration files. The container is not a virtual machine as the virtual machine has a complete operating system. The advantage of hardware virtualization is that you can run many virtual machines with different operating systems on a single host whereas with containers both host and containers share the same kernel.

Docker is an open-source containerization platform that is used to package an application into containers, with all necessary components to run it seamlessly for environments development, test or production. It has capabilities of packaging, distribution, and the ability to start-stop, pause the software in a reliable and repeatable way . It also provides orchestration and scaling. Docker is supported by MacOS, Windows, and Linux.
The environment when creating the container is known as image. We can think of the images as read-only templates that build containers. Containers are lightweight environments for running the templates. Images can exist without a container but we need images to run a container. Docker Hub is a service that allows finding and sharing container images.
When using Docker for development we don’t need to install any programs, other than Docker itself. If our application uses Apache Tomcat we don’t have to install it on our workstation.
Docker is built on Linux containerization technologies. But windows and Macs don’t have it so to run docker on these platforms, we need a lightweight Linux virtual machine. By the way, Docker can lessen the configuration management but that doesn’t mean that it replaces the traditional configuration management.
For managing containers deployed across multiple machines, we need a container orchestration tool. Containers are managed in an automated way; for tasks like creating, scaling, upgrading, availability of container security and configuration. Container orchestration works with tools like Kubernetes and Docker Swarm. We can use Docker Swarm for smaller workloads. It is easy to install and use. Kubernetes is more powerful and flexible and is battle-tested by big players.

Now we are going to demonstrate a containerization sample by creating a Rails application. Rails is a server-side web development framework written in Ruby. It is used for developing a database-backed application. It allows developers to write less code and minimize the time consumed in application production. Rails use the Model-View-Controller (MVC) architectural pattern. When compared with Django (Python-based web framework) Django has an MVT (Model-View-Template) design and it is more popular than Rails. But Rails is a little faster than Django. And Rails is a good solution for handling smaller projects with smaller teams.

When we compare Ruby with Java, they share the same object-oriented principles. Ruby is more flexible than java. With Ruby by fewer lines of coding, we can accomplish tasks. Because of these advantages, its speed of development is better than java. For fewer traffic sites we can use Ruby but for high traffic sites definitely, java should be used. For more information please check the website. Before we start, we need to install Docker and the Ruby distribution for your platform and install Rails.

Before we start we need to clarify some terms. RubyGems software makes it possible to download and install ruby software packages. The software package is called a gem. And it is a collection of Ruby codes. Bundler provides a consistent environment for Ruby projects. Bundler records the exact gems and versions that were installed. A bundler is important to manage dependencies. Running bundle install will look at the “gemfile.lock” to install the exact same versions. If it is not present, it will fetch the latest dependencies. Now we are ready to check the installed applications from the command prompt starting with docker ”docker -v” . And move on with the others.

Versions Installed

When you complete the installations, open the command line, create a folder named RubyRails and go inside that folder by the command line. Create a new project helloRuby.
rails new helloRuby

The Output

Make sure that all necessary files are installed without any errors. Go to folder helloRuby we just created.

cd helloRuby

Folder helloRuby

Now we are going to also create a controller.
rails generate controller sayhi

Output

Go to directory helloRuby\app\views\sayhi and create a file main.html.erb. Open the page and paste the contents below.

Html Page

We need to route our controller, to the html page, so from the config directory change helloRuby\config\Routes.rb uncomment and change the line as :

root “sayhi#main”

After saving the file go to the command line and just type “rails server” .

Our Web Application Output

Now we are ready to dockerize our web application. First, create a Dockerfile inside the folder helloRuby. If you want to exclude unnecessary files from the context please add a .dockerignore file.

Dockerfile

What this file tell us is that

  • 1-Image will be based on ruby version 3.0.3.
  • 2- Simple key-value pairs for adding meta-data
  • 3-Download latest package information and install in quiet mode (yqq) and yes to any prompts
  • 4- Copy Gemfile and Gemfile.lock into our image
  • 5-Select the working directory
  • 6-Install the gems needed for our Rails project
  • 7-Copy all the files from our local
  • 8-Start Rails server by command

We bind 0.0.0.0 which is a special address and it means all IPv4 addresses on this machine. In this way, our Rail server listens to requests from other IPs. (Not only localhost). Now it is time to build our docker image

docker build -t “greenredblue/helloruby:v1” .

docker run -p 3000:3000 greenredblue/myruby:v1

Now we are ready to test our application.

Docker Output

Some useful docker commands:

Some Useful DockerCommands

Thanks for reading. The source files for the tutorial can be found on GitHub.

--

--