Google Cloud Run on Rails: a real life example (Part 2: running locally)

Laurent Julliard
Google Cloud - Community
4 min readMay 21, 2019

Part 1 was about setting up your GCP project as well as your work environment. Let’s now focus on the Photo Album application itself and run it locally before going to production.

Note: running the Photo Album application in your local environment is optional. You can safely skip this part (or just skim through it) and go to Part 3.

Introducing the Photo Album application

The Rails application we are using in this tutorial is a Photo Album. It is simple enough to fit in a single Rails controller and yet rich enough so as to exercise a number of GCP services that a professional application typically needs in real life.

This includes:

  • Cloud Build to build the container image of your application and deploy it to Cloud Run
  • Container Registry to store and uniquely identify a version of your container image as well as check it against vulnerabilities
  • Cloud SQL to manage your production MySQL database at scale
  • Google Cloud Storage as the Rails Active Storage backend to store picture files
  • Google Cloud Key Management Service (KMS) to protect your application secrets and credentials

The source code of the application is available from Github. To clone it locally run the following command:

$ git clone https://github.com/ljulliar/photo_album.git

In case you’d like to understand what’s under the hood, you’ll find the meat of the application in the following files:

  • the Pictures Controller: what you’ll find here is a typical Rails controller with actions for creating, showing and deleting photos.
  • The Picture Model : this is where the picture model is declared and also where we state that Rails ActiveStorage manage the image files associated with each Picture instance created.
  • The two database migration files defining the database schema: one that is automatically generated by ActiveStorage and the other one for the Picture data.
  • The Views and partials files used to render the application HTML pages. The look and feel of the application is based on the materialize-sass Ruby gem, a Sass powered version of Materialize, a modern responsive front-end framework based on Material Design.

Installing Ruby and gems

If Ruby is not yet installed on your work environment, you can set it up in a snap as follows:

First install RVM, the Ruby version manager. Although installing RVM is optional, I highly recommend using it (or any other Ruby version manager of your choice) as it makes the installation of Ruby versions so easy.

# install rvm signature key
$ gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
# install rvm
$ curl -sSL https://get.rvm.io | bash
# load rvm environment variables
$ source ~/.rvm/scripts/rvm

With rvm up and running, install the Ruby version needed for this tutorial (2.6.3 or whatever the latest stable 2.6.x release is by the time you read this article):

$ rvm install ruby-2.6.3       # Install ruby 2.6.3
$ rvm --default use ruby-2.6.3 # Make it the default version
$ ruby -v # check that it’s working fine

Let’s now install the Ruby gems we need for the Photo Album application as specified in the Gemfile file. You’ll notice that the Rails application relies on the google-cloud-storagegem as Rails ActiveStorage needs it to access the GCS bucket where image files are stored.

You may have noticed that there is no specific gem for Cloud SQL. As we are using a Cloud SQL MySQL instance, the conventional mysql Ruby gem is all we need to interface with either the local or the production MySQL database.

$ cd ./photo_album
$ bundle install

With this, our Ruby environment is ready. Let’s now explore and setup MySQL.

Setting up MySQL

For this tutorial we need to install MySQL 5.6 or above. From your work environment type the following command:

$ sudo apt install mysql-server libmysqlclient-dev  # install...
$ sudo service mysql start # and start the database server

Note: If you are using Cloud Shell as your work environment, all the changes you make in the home directory of your Cloud Shell is persisted across work sessions but system wide modifications such as installing new Debian packages are not. So if you decide to close your session or suspend your work for some time you’ll have to install and start mysql again.

Now create the user names for the local MySQL development database (replace xxx below with your preferred password and make sure to update the development and test sections of the config/database.yml file accordingly to reflect the password you have chosen):

$ mysql -u rootCREATE USER ‘dev_db_user’@’localhost’ IDENTIFIED BY ‘xxx’;
GRANT ALL ON *.* TO ‘dev_db_user’@’localhost’ WITH GRANT OPTION;
CREATE USER ‘test_db_user’@’localhost’ IDENTIFIED BY ‘xxx’;
GRANT ALL ON *.* TO ‘test_db_user’@’localhost’ WITH GRANT OPTION;
FLUSH PRIVILEGES;exit

Running Photo Album locally

Then let’s create and populate the local MySQL development database…

$ bundle exec rake db:setup

… and launch the Photo Album application in our local environment.

$ bundle exec rails server

Visit http://localhost:3000/ with your favorite browser and you should land on a home page inviting you to load a new photo in your empty Photo Album.

Note: If you are using Cloud Shell, click on the Web Preview icon right above the shell window (look for the <> symbol) and change the Preview port to 3000. Then click on “Preview on Port 3000” and voilà !

Your pristine Photo Album

You can play with your Photo Album: upload a couple of pictures along with their captions, click on one of them to see the details and delete a photo too. You should see something like this:

Your Photo Album with a couple of nifty pictures

All good ? Let’s now go to Part 3 and lay the groundwork for deployment in production.

--

--