Deploy a Sample Ruby on Rails Application

Page last updated: October 1, 2015

This topic walks the reader through the process of deploying a sample Ruby on Rails app to Cloud Foundry.

The following subjects are covered:

  • Using the CF CLI to target a CF deployment.
  • App manifests.
  • Deploying an app.
  • How app URL endpoints are generated in CF.
  • Adding a service instance (A postrgres database, in this example).

Prerequisites

Step 1: Clone the App

Run the following in the terminal to clone the rails_sample_app from GitHub:

    git clone https://github.com/cloudfoundry-samples/rails_sample_app

Change into the rails_sample_app directory and examine its contents. The release contains a very simple Ruby on Rails app with the single addition of the application manifest manifest.yml, which allows CF to deploy the app. For more about application manifests, see Deploying with Application Manifests.

~/workspace/rails_sample_app $ ls
Gemfile README.markdown app bin config db files 
manifest.yml
script vendor Gemfile.lock Rakefile autotest bundler_stubs config.ru doc lib public spec

Step 2: Log in and Target the API Endpoint

Run the following to log in and target the API endpoint of your Cloud Foundry deployment. You will be prompted for login credentials, and to select a space and org.

For YOUR-API-ENDPOINT, enter the IP of the BOSH director for your Cloud Foundry deployment. The IP must entered in the following format: http://api.IP-ADDRESS

cf login -a YOUR-API-ENDPOINT

For more information about Spaces and Orgs, see Orgs, Spaces, Roles, and Permissions.

Step 3: Create a Service Instance for our Application Database

In Cloud Foundry, a database is considered a type of service. To add a database for our sample application we will create an instance of elephantsql, a third party database as a service provider.

Run the following terminal command to creates a postgresql service instance for our app.

  $   cf create-service elephantsql turtle rails-postgres
  Creating service rails-postgres in org Cloud-Apps / space development as [email protected]....
  OK
  

Our service instance is named rails-postgres. It uses the elephantsql service and the turtle plan.

The manifest for the sample app contains a services sub-block in the applications block, as the example below shows. This binds the rails-postgres service instance you created in the previous step.

---
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

From the root directory of your application, run the following to deploy the app:

cf push rails_sample_app

cf push APP-NAME creates a URL route to your application in the form HOST.DOMAIN, where HOST is your APP-NAME and DOMAIN is specified by your administrator. For example, if your domain is shared-domain.com then running cf push my-app creates the URL my-app.shared-domain.com.

If you want to view log activity while the app deploys, launch a new terminal window and run cf logs APP-NAME.

The example below shows the terminal output of 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 instructions in the manifest to start one instance of the app with 256M. 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 [email protected]...
        OK

        Using route my-app.shared-domain.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 [email protected]...
        OK

        Starting app rails_sample_app in org Cloud-Apps / space development as [email protected]...
        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 [email protected]...
        OK

        requested state: started
        instances: 1/1
        usage: 256M x 1 instances
        urls: my-app.shared-domain.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

Step 5: Verifying the app

You can verify the sample app you have pushed by browsing to the URL generated in the output of the previous step. In this example, the url is urls: my-app.shared-domain.com.

You’ve now pushed an app to Cloud Foundry! For help deploying your own app, visit the Deploy Your App: Guides by Framework section of the help.