Deploy a Ruby on Rails App
This guide explains how to deploy 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.
To successfully follow this documentation, you will need to have a Koyeb account (opens in a new tab). You can optionally install the Koyeb CLI if you prefer to follow this guide without leaving the terminal.
You can deploy and preview the sample Ruby on Rails application that we will run on Koyeb in this guide using the Deploy to Koyeb button below.
You can consult the repository on GitHub (opens in a new tab) to find out more about the example application that this guide uses.
Create the Rails app
Get started by creating a minimalistic Rails application that we will deploy on Koyeb. You will need Ruby (opens in a new tab) and Rails (opens in a new tab) installed on your machine.
In your terminal, run the following command to create a new Rails application:
rails new example-rails --minimal
This will create a new minimum Rails application skeleton in a new example-rails
directory with just Active Record, Action Pack, Active Support, Railties, and the bare necessity support gems.
During deployment, Koyeb will automatically deploy using the version of Ruby specified in your Gemfile
.
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
Next, open the config/routes.rb
file and edit as shown below 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 make sure everything is running as expected:
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 we ran previously 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. If you don't have an existing GitHub repository to push the code to, you can create a new one and run the following commands to commit and push changes to your GitHub repository:
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
Make sure to 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 the steps below:
- 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 at the bottom of the page:
https://github.com/koyeb/example-rails
. - Name the App and Service, for example
example-rails
. - Click the Deploy button.
A Koyeb App and Service will be created. Your application will be built and deployed to Koyeb. Once the build is complete, you will be able to 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 copy the content below:
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"]
The Dockerfile above 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.