Ruby on Rails

Deploy a Ruby on Rails App

This guide explains how to host a Ruby on Rails (opens in a new tab) application on Koyeb using:

  1. Git-driven deployment to automatically build and deploy a new version of your application each time a change is detected on your branch.
  2. Pre-built containers to deploy from any public or private registry.

You will need:

You can deploy and preview the sample Ruby on Rails application from this guide using the Deploy to Koyeb button:

Deploy to Koyeb (opens in a new tab)

Consult the repository on GitHub (opens in a new tab) to view this example application.

Create the Rails app

Get started by creating a basic Rails application that we will deploy on Koyeb.

Alternatively, you can fork the repository on GitHub (opens in a new tab) to get a complete copy of the code. If you fork the repository, then you can skip to section on git-driven deployment on Koyeb.

In your terminal, run the following command to create a new Rails application:

rails new example-rails --minimal

This creates a minimal Rails application in a new example-rails directory with Active Record, Action Pack, Active Support, Railties, and the strictly necessary support gems.

Koyeb automatically uses the version of Ruby specified in your Gemfile for deployment. Check the page on building Ruby projects to learn more.

Create a new Rails controller by running the following command:

rails generate controller HelloKoyeb index

Open the config/routes.rb file and edit using the following code to map GET requests to / to the index action of the HelloKoyeb controller:

config/routes.rb
Rails.application.routes.draw do
  get 'hello_koyeb/index'
 
 root 'hello_world#index'
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
 
  # Defines the root path route ("/")
  # root "articles#index"
end

Run the Rails app locally

Launch the application locally to test:

cd example-rails
rails server

You can now access the application by navigating to http://localhost:3000 in your browser. To stop the server, press CTRL-c.

Push the project to GitHub

The rails new command initialized a new Git repository in the example-rails directory.

We will use this repository to version the application code and push the changes to a GitHub repository. Run the following commands to commit and push changes to your GitHub repository, replacing the GitHub username and repository name with values from your account and the GitHub repo name:

git add .
git commit -m "Initial commit"
git remote add origin git@github.com:<YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME>.git
git push -u origin main

Replace <YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME> with your GitHub username and repository name.

Deploy to Koyeb using git-driven deployment

To deploy the Rails app on Koyeb using the control panel (opens in a new tab), follow these steps:

  1. Click Create Web Service on the Overview tab of the Koyeb control panel.
  2. Select GitHub as the deployment option.
  3. Choose the GitHub repository and branch containing your application code. Alternatively, you can enter our public Rails example repository (opens in a new tab) into the Public GitHub repository: https://github.com/koyeb/example-rails.
  4. Name the App and Service. For example, example-rails.
  5. Click the Deploy button.

This creates a Koyeb App and Service which builds and deploys your application on Koyeb. You can access your application running on Koyeb by clicking the URL ending with .koyeb.app.

Deploy to Koyeb using a pre-built container

As an alternative to using git-driven deployment, you can deploy a pre-built container from any public or private registry. This can be useful if your application needs specific system dependencies or you need more control over how the build is performed.

To dockerize the Rails application, create a Dockerfile in your project root directory and add the following to the file:

FROM ruby:3-alpine
 
WORKDIR /app
COPY . .
 
RUN gem install bundler
RUN bundle install
ENV RAILS_ENV=production
RUN bundle exec rails assets:precompile
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]

This Dockerfile provides the minimum requirements to run the Rails application. You can easily extend it depending on your needs.

To build and push the Docker image to a registry and deploy it on Koyeb, refer to the page on deploying pre-built container images.

What's next

For more examples of Ruby on Rails applications deployed on Koyeb, check out these tutorials:

To learn more about the features available for your apps, check out the following documentation: