How to build a Rails 6 application using Docker

How to build a Rails 6 application using Docker

In the Introduction to Docker article, we developed a good basic foundational knowledge about container technology. Now, we will take it one step further by creating and running a Rails 6 application exclusively using Docker. As of this writing, Rails 6 is the latest version of the Ruby on Rails web application framework.

Generating the Rails code

We learned previously that we can run Ruby code using Docker even without having Ruby in our local machine. We can also use the container’s shell and run commands directly in the container. Using these, let’s try to perform some commands in a container running Ruby 2.7:

docker run -it --rm -v ${PWD}:/usr/src/app ruby:2.7 bash

The -i and -t flags (combined as -it) is necessary to be able to use the container’s shell. In this case, we also use the -v flag (volume). What is it for?

Docker Volumes

Docker provides a mechanism for storing data without being coupled to a specific container. Containers should be treated as ephemeral, meaning they can be created and destroyed at any time. Thus, we should not store data inside a container as it will also get deleted once that container is destroyed.

Volumes work by mounting it into a container. In the example above, we set it as:

-v ${PWD}:/usr/src/app

This means that the current directory is going to be mounted into the container under the path /usr/src/app. You can mount it as a readonly volume or not. For our purposes, we need it to be writable (the default) as this is where our Rails application is going to run.

Installing the Rails gem

We need to prepare the project directory in our local machine. For example, we will create our Rails application under a projects folder:

mkdir -p ~/projects
cd ~/projects

Then we run the command to use the bash shell of a Ruby 2.7 container:

docker run -it --rm -v ${PWD}:/usr/src/app ruby:2.7 bash

Great! Now we can access the shell and run commands inside the container. Note that the default Docker user is root, so all commands here will run under the root user.

[email protected]_id:/# cd /usr/src/app
[email protected]_id:/# gem install rails

Since we are using a Ruby container, RubyGems is already installed by default and so we can use the gem command immediately.

Javascript Dependencies

Rails 6 comes with a gem called webpacker, which is basically a Ruby wrapper for the webpack library. Webpacker requires yarn (an alternative to npm) and most Javascript libraries require Node to be installed as well. Thus we will need to install these first in the container before we proceed with creating our project.

# Add the Node source
[email protected]_id:/# curl -sL https://deb.nodesource.com/setup_14.x | bash

# Add the yarn source
[email protected]_id:/# curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
[email protected]_id:/# echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list

# Install Node and yarn
[email protected]_id:/# apt-get update -yqq && apt-get install -yqq --no-install-recommends \
nodejs \
yarn read more

Introduction to Docker

Introduction to Docker

Docker is perhaps responsible for the proliferation of containers in application development. The concept of containers is quite old, and can be traced back in the 1980s by chroot wherein different user spaces can be used within the same operating system. Once Docker was introduced however, it paved the way for further developments in containerization and changed the way how we develop and deploy software.

In this article we will discuss how to install and setup Docker in your local machine. Common Docker commands will be introduced that will equip you with foundational knowledge for tackling the next steps in application development using containers (such as Compose).

Installing Docker

The following steps are specific to most Linux distributions, but after the installation step, the concepts discussed here can be applied to any operating system.

First, we need to ensure that old versions of Docker are removed so that we can install the latest version.

sudo apt-get remove docker docker-engine docker.io containerd runc

Update your package repositories and install the requirements for Docker.

sudo apt-get update 
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common

Install Docker’s official GPG key and add the stable repository.

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

Update the repositories again and install Docker CE, the CLI and associated packages. The CE means “Community Edition”, which is the free, open-source version of Docker. Docker (the company) also has an “Enterprise Edition” (EE) that is targeted specifically for businesses and large deployments. Both CE and EE share the same core features, but EE has more advanced management and support systems.

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

Note: If you encountered an error similar to the message below and you were not able to install Docker:

Package 'docker-ce' has no installation candidate read more

More Lessons I Learned Working From Home

More Lessons I Learned Working From Home

In the previous article, we discussed the advantages and disadvantages to working from home. In a remote work setup, it is important to establish a fixed time and place for you to work. But there are more things that we need to take into account: communication and security. These are the additional lessons I learned while working remotely since 2012.

Communication is a high priority

My boss always emphasized that communication is the most important thing especially in the context of working remotely. If we are not able to communicate effectively, then the whole setup will fail. read more

Lessons I Learned Working From Home

Lessons I Learned Working From Home

We live in interesting times due to the COVID-19 pandemic. More and more companies are being forced to adopt a remote working setup (or working from home) for its employees. I have been working remotely full-time since 2012. This article discusses some of the lessons and tricks I learned throughout the years.

I understand that working from home does not apply to all industries. It is mostly applicable to jobs that involve working in front of a computer. My career is in software development which fits this set up comfortably. However it can also apply to other industries such as business process operations, accounting, and company administration. read more

Slightly off Rails

Slightly off Rails

I use Rails both professionally and personally for more than a decade now. Through the years, my usage of Rails also evolved together with new concepts and trends in web development. Here are some of the personal changes I am applying moving forwards on how I work with the framework.

Authentication using JWT

JWT.io
https://jwt.io/

JSON Web Tokens (JWT) provides a way to effectively secure API endpoints. As web applications move more to the front-end with the popularity of Javascript frameworks like React and Angular, JWT ensures that the connection between the front-end and the back-end remains secure.

However, I don’t believe that we should reinvent the wheel, especially when it comes to authentication. Thus I will still continue to use the devise gem. This library has been a staple in Rails applications for many years now and as a result has already gone through a lot of patching and bug fixes.

There’s also a gem called devise-jwt that makes it easy to integrate devise authentication using JWT, and I am using this library for present and future projects.

What this gem does is that after a user signs in, it automatically adds a JWT token to the Authorization header. This token can then be used by the front-end code to submit requests back to the server. The server checks the token and validates if the user has access to the endpoint or resource. Using the gem, this is being done automatically, and that user is available through the current_user variable.

You can still use the (default) session authentication as well, so you can have both a web application and a front-end client application (like a mobile app) using the same authentication mechanism.

dry-rb

Rails is an opinionated framework. This means it has its own way of doing things, and by following them it can make your work simpler and faster. These methods utilize the full features of the framework and lets the framework do the heavy lifting for you. Some people do not like this approach as it makes things less transparent and “magical”.

While Rails is opinionated, it is not restrictive. You can ignore built-in features or build your own without having the need to abandon the framework altogether.

https://dry-rb.org/

There are several libraries that enable you to work outside the Rails conventions. Some of the most popular ones are Trailblazer and dry-rb. Moving forward, I intend to use dry-rb more as I found that their modular approach to libraries is a great way to implement projects.

Here are some of the concepts that I found useful in dry-rb:

Transactions and Operations

Using the transaction concept allows us to think of a feature in terms of steps. This in turn enables us to break down a feature into smaller parts. It also allows for easier debugging and refactoring of code as we can create steps that essentially just perform one specific task of the whole.

As an example, let’s say we want to create an event and send an email to its participants. In a transaction, we can break this down into three steps: checking the event form parameters, creating the record, and then sending the emails.

require "dry/transaction"

class CreateEvent
include Dry::Transaction

step :validate_parameters
step :create_event
step :send_email, with: 'event.operations.send_email'

private

def validate_parameters(params)
# returns Success(valid_params) or Failure(errors)
end

def create_event(params)
# returns Success(event)
end
end

You can see that the validate_parameters and create_event methods are simply private methods of the transaction class. The send_email method however is not present in the class itself but is invoked as an external operation. For this example, we can have an operation class called Event::Operations::SendEmail.

Using operation classes allows us to create reusable functions. These operations can be called on any other transaction (or any other code really). In addition to being reusable and modular, it is also easy to test as each operation performs only one specific function.

Monads

Notice that the return values of the methods are Success() and Failure(). These are called monads and they help structure your responses so that methods communicate with each other more effectively. The value I get from this is it forces me to think of failure scenarios in the code. I can then handle success and failure conditions effectively without resorting to complex logic or nested if-else conditions.

Validations

dry-rb also has a library called dry-validation. Using this library you can define your own custom schema and validation rules. In Rails, the default schema and validation is usually tied to your database models. dry-validation provides the same feature that you can use on any code in your application.

It also enables you to define types in each of the fields in your schema. This can simplify your code as you are guaranteed that each field has the correct type. This also reduces potential bugs in your application.

Here is an example found in their official documentation. It describes how to use dry-validation in setting up the schema and the validation for a new User record:

class NewUserContract < Dry::Validation::Contract
params do
required(:email).filled(:string)
required(:age).value(:integer)
end

rule(:email) do
unless /\A[\w+-.][email protected][a-z\d-]+(.[a-z\d-]+)*.[a-z]+\z/i.match?(value)
key.failure('has invalid format')
end
end

rule(:age) do
key.failure('must be greater than 18') if value < 18
end
end read more