Setup Ruby on Rails Development environment with Docker and Docker Compose on Ubuntu

Docker is an open-source project that provides an open platform for developers and sysadmins to build, package, and run applications anywhere as a lightweight container. Docker automates the deployment of applications inside software containers.

Ruby on Rails (RoR) is an open source web application framework, published under the MIT License. It is a server-side web application framework that follows the MVC (Model-View-Controller) concept.

In this tutorial, I will show you how to set up a development environment for Ruby on Rails applications using Docker and Docker compose. We will be using Ubuntu 18.04 as the hosts operating system and using the PostgreSQL database for our Rails project.

What we will do:

  1. Install Docker and Docker Compose
  2. Generate the Rails Project
  3. Setup the Rails Project
  4. Create Docker Compose Script
  5. Build the Project
  6. Test Create Basic CRUD on Rails

Step 1 - Install Docker and Docker Compose

The first step we must do is to install the docker and docker compose itself. We will install the docker from the official docker repository, and install the docker-compose from the official docker GitHub project.

Before installing the Docker packages, run the apt command below to install packages dependencies.

sudo apt install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

Now add the docker key and docker 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"

The command will automatically update all repositories on the system. When it's complete, install the docker-ce packages.

sudo apt install -y docker-ce

Wait for the docker-ce installation, and then start the docker service and add it to the boot time.

sudo systemctl start docker
sudo systemctl enable docker

The Docker is up and running on the system.

Next, install the docker-compose by downloading the binary file directly from the docker GitHub repository.

Download the docker-compose binary file to the '/usr/local/bin/' directory and make it an executable.

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
sudo chmod +x /usr/local/bin/docker-compose

The docker and docker-compose have been installed to the system, check the version using commands below.

docker version
docker-compose version

Below is the result.

Installing Docker

Step 2 - Generate Ruby on Rails Project

After installing the core packages docker and docker-compose to the system, we want to create a new user and then generate the Rails project using the docker images.

Add a user named 'hakase' and give the user a password.

useradd -m -s /bin/bash hakase
passwd hakase

Add the user to the 'sudo' and 'docker' group and login to the 'hakase' user shell.

usermod -a -G sudo hakase
usermod -a -G docker hakase
su - hakase

Now the 'hakase' user can execute and run the docker command.

Next, we will create a new directory 'rails' for our Ruby on Rails project.

Create the 'rails' directory and goto it.

mkdir -p ~/rails
cd ~/rails/

Now execute the 'docker run' command below.

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

The command will run the temporary container based on ruby:alpine image, mount the local directory to the '/usr/src' directory inside the container, and then execute the 'sh' shell command and go to the 'app' directory.

Generate Ruby on Rails project

Inside the container, install the 'build-base' packages.

apk add build-base

Now install the Ruby on Rails inside the temporary container.

gem install -N rails

And generate the new Rails project named 'app' with PostgreSQL as the database, then exit/logout from the container.

rails new app --database=postgresql --skip-bundle
exit

And you will be on the Rails project directory 'app'.

Now change the owner of the 'app' project directory to the 'hakase' user.

sudo chown -R hakase:hakase ~/rails/app/
ls -lah

And the Ruby on Rails project has been generated through the temporary docker container.

Rails Project

Step 3 - Setup the Rails Project

In this step, we will create a new Dockerfile for our Rails applications.

Inside the 'rails' directory, create a new Dockerfile using vim.

vim Dockerfile

Paste configuration below.

FROM ruby:alpine

RUN apk update
RUN apk add build-base nodejs postgresql-dev tzdata
RUN gem install -N rails

RUN mkdir -p /app
WORKDIR /app

COPY ./app/Gemfile /app
COPY ./app/Gemfile.lock /app
RUN bundle install --binstubs

Save and exit.

We're creating a new docker image based on the Ruby Alpine linux. We're installing new packages for the rails installation, create a new /app directory, copy the Gemfile and Gemfile.lock from app local directory, and the install all packages based on the Gemfile.

Next, go to the 'app' directory, create a new file Gemfile.lock.

cd app/
touch Gemfile.lock

Edit the 'database.yml' file.

vim config/database.yml

Change the default database configuration and change details as below.

default: &default
   adapter: postgresql
   encoding: unicode
   host: db
   username: postgres
   pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
   timeout: 5000

Save and exit.

Setup the Rails Project

Rails project configuration has been completed.

Step 4 - Create Docker Compose File

In this step, we will create a new docker-compose file for our Rails Application. We will only create two services db database PostgreSQL and web is the rails application itself.

Create a new file 'docker-compose.yml' inside the 'rails' directory.

vim docker-compose.yml

And paste configuration below.

version: '3.6'

services:

  db:
    image: postgres:alpine
    volumes:
      - ./postgresql:/var/lib/postgresql/data

  web:
    build: .
    volumes:
      - ./app:/app
    working_dir: /app
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    ports:
      - 80:3000
    depends_on:
      - db

Save and exit.

Create Docker Compose File

Now create the 'postgresql' directory inside the 'rails' project.

mkdir -p ~/rails/postgresql

And we're ready to build our Rails project.

Step 5 - Build The Project

Build the Rails docker image using the docker-compose command below.

docker-compose build

The command will download the Ruby Alpine Linux image and build the custom image as we need based on our Dockerfile.

Build The Project

Generate the PostgreSQL database for the project.

docker-compose run web rake db:create

Docker compose run

Now bring the 'db' and 'web' services up.

docker-compose up -d

And rails services is up and running, check it using the docker-compose command below.

docker-compose ps

You can see the service 'web' is running on port '80' on the host.

Now check docker images on our system.

docker-compose images

And you will get the result as below.

Docker compose images

Now open your web browser and type the server IP address or domain name. Mine is:

http://rails.hakase-labs.io/

And you will get the default Rails page application on it.

Rails is running successfully

Now we're ready to develop our Rails project.

Step 6 - Test Create Basic CRUD on Rails

Generate simple CRUD on rails by running the rails command inside the 'web' container service.

docker-compose exec web rails g scaffold Post title:string body:text

Now generate the database.

docker-compose exec web rake db:migrate

Test Create Basic CRUD on Rails

Now open your web browser and type the server IP address on address bar with path '/posts'. Mine is:

http://rails.hakase-labs.io/posts

And you will get the simple CRUD page as below.

Test POST form

Type the post and click the 'Create Post' button.

And you will get the result as below.

Rails app is working properly

The Development Environment setup for Ruby on Rails with Docker and Docker Compose has been completed successfully.

Reference

Share this page:

Suggested articles

0 Comment(s)

Add comment