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 | bash

# Add the yarn source
[email protected]_id:/# curl -sS | apt-key add -
[email protected]_id:/# echo "deb 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 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 \

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

curl -fsSL | sudo apt-key add -

sudo add-apt-repository \
"deb [arch=amd64] \
$(lsb_release -cs) \

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

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

Path of Least Regret

As we live and get older, we learn a lot of things about life. Good and bad experiences serve us lessons and make us more equipped when dealing with our unpredictable future. We can also tap other people’s experiences to open us to new ideas. These thoughts were highlighted one day when my wife said that she appreciated my calm demeanor even though things are not going well. While I am not always like this, when I think about it, I attribute this behavior to one principle: living life on the path of least regret. read more

Surviving and Thriving in a Recession

Surviving and Thriving in a Recession

When a recession hits, it affects many people and industries across the world. Even if you think that your industry is not affected, your company can still be hit due to knock-on effects. Revenue becomes harder if your clients are affected or if they realign their budgets. If this persists for several months, it can result to layoffs and furloughs.

Protect your income

It is with this increased risk that we should protect our sources of income as much as possible. If you run a business, then this is the time to optimize operations and reduce unnecessary expenses. Look into each recurring expense and determine whether it is critical for your business. You may be surprised to know that some of them can be eliminated without affecting your operations. 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