How to set up a Ruby on Rails development environment with Docker

Find out how to use Docker to build a Rails development framework that will make app programming and web development more efficient.

datacenterhero.jpg

Jack Wallen

Ruby on Rails (aka Rails) is a web development framework that makes app programming and web development more efficient. These frameworks serve as ready-made solutions for things like building menus, tables, or forms for a website. For many app and web developers, Rails is a must have.

In order to work with Rails, you need the proper environment. How do you set up the Rails environment? With the help of Docker, you can do so pretty quickly. I'm going to walk you through the process of creating such an environment on Ubuntu Server 18.04. Within minutes (or seconds, depending on quickly you type), you can generate your first Rails project.

SEE: Hiring kit: Database administrator (Tech Pro Research)

Let's get to work.

Docker

On the off-chance you do not already have Docker up and running on your server, here's how you do so:

  1. Log into your Ubuntu server.
  2. Install Docker with the command sudo apt-get install docker.io -y
  3. When the installation completes, add your user to the docker group with the command sudo usermod -aG docker $USER.
  4. Log out and log back in.

Deploying Rails

Our next step is to deploy the Rails container. Before we do that, create a new directory with the command:

mkdir -p ~/rails

Change into the newly created directory with the command:

cd ~/rails

Deploy the Rails container with the command:

docker run --rm -v ${PWD}:/usr/src -w /usr/src -ti ruby:alpine sh ; cd app

The above command does the following:

  • Deploys a temporary container, based on ruby:alpine image.
  • Mounts the host directory /usr/local inside the container.
  • Executes the sh shell command.
  • Drops you into the mounted /usr/local directory.

Now we can install the Rails dependencies with the command:

apk add build-base

With the dependency out of the way, you can now install Rails with the command:

gem install -N rails

The above command does take a bit longer than you might think, so give it time. Once it completes, you are ready to generate your first Rails project. Generate a new Rails project (named app with a MySQL database) with the command:

rails new app --database=mysql --skip-bundle

Once this completes, exit out of the container with the exit command. You should now find yourself in the ~/rails/app directory, ready to start building your Rails project. At the moment, however, this directory is owned by root. To change this, issue the command:

sudo chown -R $USER:$USER ~/rails/app

Docker compose

If you plan on creating your Ruby on Rails projects with dockerfiles, you'll need to make use of the docker-compose command. For this to work properly, you need to install docker-ce, instead of the docker.io package. This can be done with the following steps:

  1. Add the necessary key with the command curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  2. Add the repository with the command sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs)  stable"
  3. Install docker-ce with the command sudo apt install -y docker-ce

Once you complete the above steps, start and enable docker with the commands:

sudo systemctl start docker
sudo systemctl enable docker

Finally, download the docker-compose binary with the command:

sudo curl -L https://github.com/docker/compose/releases/download/1.21.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose

Give the newly added command executable permissions with the command:

sudo chmod +x /usr/local/bin/docker-compose

Now, if you plan on building Ruby on Rails projects with dockerfiles, you're ready.

And that's all there is to creating a Rails development environment with the help of Docker. Time to start building. Next time we visit this topic, we'll discuss how to build a Rails project using dockerfiles.

Also see