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:
- Git-driven deployment to automatically build and deploy a new version of your application each time a change is detected on your branch.
- Pre-built containers to deploy from any public or private registry.
You will need:
- A Koyeb account (opens in a new tab) - it's free to get started!
- Ruby (opens in a new tab)
- Rails (opens in a new tab)
- (Optional) The Koyeb CLI for deployment from the terminal
You can deploy and preview the sample Ruby on Rails application from this guide using the Deploy to Koyeb button:
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:
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:
- Click Create Web Service on the Overview tab of the Koyeb control panel.
- Select GitHub as the deployment option.
- 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
. - Name the App and Service. For example,
example-rails
. - 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:
- How to Deploy a Rails Application and Add Authentication using Devise (opens in a new tab)
- Dockerize, Deploy and Run a Ruby on Rails application on Koyeb (opens in a new tab)
To learn more about the features available for your apps, check out the following documentation:
- Autoscaling (opens in a new tab) - Dynamically adjust the number of Instances within a Service to meet the demand of your applications.
- Scale-to-Zero (opens in a new tab) - Configure your Instances to automatically scale down to zero when there is no incoming traffic, reducing usage.
- Metrics (opens in a new tab) - Learn how to monitor your Services usage.