Deploy a Sample Ruby on Rails Application

Page last updated:

This topic guides the reader through deploying a sample Ruby on Rails app to Cloud Foundry (CF).

Prerequisites

In order to deploy a sample Ruby on Rails app, you must have the following:

Step 1: Clone the App

Run the following terminal command to create a local copy of the rails_sample_app.

$ git clone https://github.com/cloudfoundry-samples/rails_sample_app
The newly created directory contains a manifest.yml file, which assists CF with deploying the app. See Deploying with Application Manifests for more information.

Step 2: Log in and Target the API Endpoint

  1. Run the following terminal command to log in and target the API endpoint of your deployment. For more information, see the Identifying your Cloud Foundry API Endpoint and Version topic.

    $ cf login -a YOUR-API-ENDPOINT
  2. Use your credentials to log in, and to select a Space and Org.

Note: The API endpoint must be entered in the format https://api.IP-ADDRESS, where IP-ADDRESS is the IP address of your API endpoint.

Step 3: Create a Service Instance

Run the following terminal command to create a PostgreSQL service instance for the sample app. Our service instance is rails-postgres. It uses the elephantsql service and the turtle plan.

$ cf create-service elephantsql turtle rails-postgres
Creating service rails-postgres in org YOUR-ORG / space development as clouduser@example.com....
OK

The manifest for the rails_sample_app contains a services sub-block in the applications block. The Cloud Foundry Command Line Interface tool (cf CLI) binds the service to the app.

---
applications:
- name: rails-sample
  memory: 256M
  instances: 1
  path: .
  command: bundle exec rake db:migrate && bundle exec rails s -p $PORT
  services:
    - rails-postgres

Step 4: Deploy the App

Make sure you are in the rails_sample_app directory. Run the following terminal command to deploy the app:

$ cf push rails_sample_app

cf push rails_sample_app creates a URL route to your application in the form HOST.DOMAIN. In this example, HOST is rails_sample_app. Administrators specify the DOMAIN. For example, for the DOMAIN shared-domain.example.com, running cf push rails_sample_app creates the URL rails_sample_app.shared-domain.example.com.

The example below shows the terminal output when deploying the rails_sample_app. cf push uses the instructions in the manifest file to create the app, create and bind the route, and upload the app. It then binds the app to the rails-postgres service and follows the information in the manifest to start one instance of the app with 256M of RAM. After the app starts, the output displays the health and status of the app.

$ cf push rails_sample_app
Using manifest file ~/workspace/rails_sample_app/manifest.yml

Updating app rails_sample_app in org Cloud-Apps / space development as clouduser@example.com...
OK

Using route rails_sample_app.shared-domain.example.com
Uploading rails_sample_app...
Uploading app files from: ~/workspace/rails_sample_app
Uploading 445.7K, 217 files
OK
Binding service rails-postgres to app rails_sample_app in org Cloud-Apps / space development as clouduser@example.com...
OK

Starting app rails_sample_app in org Cloud-Apps / space development as clouduser@example.com...
OK

...

0 of 1 instances running, 1 starting
1 of 1 instances running

App started

Showing health and status for app rails_sample_app in org Cloud-Apps / space development as clouduser@example.com...
OK

requested state: started
instances: 1/1
usage: 256M x 1 instances
urls: rails_sample_app.shared-domain.example.com

     state     since                    cpu    memory          disk
#0   running   2014-08-25 03:32:10 PM   0.0%   68.4M of 256M   73.4M of 1G

Note: If you want to view log activity while the app deploys, launch a new terminal window and run cf logs rails_sample_app.

Step 5: Verify the App

Verify that the app is running by browsing to the URL generated in the output of the previous step. In this example, navigating to rails_sample_app.shared-domain.example.com verifies that the app is running.

You’ve now pushed an app to Cloud Foundry! For more information about this topic, see the Deploy an Application topic.

View the source for this page in GitHub