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