Zudoku
Guides

Ruby on Rails

In this guide we create and deploy a Ruby on Rails application. To run this example, follow these steps:

  1. Install the kraft CLI tool and a container runtime engine, e.g. Docker.

  2. Clone the examples repository and cd into the examples/ruby3.2-rails/ directory:

TerminalCode
git clone https://github.com/kraftcloud/examples cd examples/ruby3.2-rails/

Make sure to log into Unikraft Cloud by setting your token and a metro close to you. We use fra0 (Frankfurt, ๐Ÿ‡ฉ๐Ÿ‡ช) in this guide:

TerminalCode
# Set Unikraft Cloud access token export UKC_TOKEN=token # Set metro to Frankfurt, DE export UKC_METRO=fra0

When done, invoke the following command to deploy this application on Unikraft Cloud:

TerminalCode
kraft cloud deploy -M 1024 -p 443:3000 -e GEM_HOME=/usr/local/bundle -e BUNDLE_APP_CONFIG=/usr/local/bundle .

The output shows the instance URL and other details:

TerminalCode
[โ—] Deployed successfully! โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ name: ruby32-rails-apa93 โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ uuid: 2f85b9db-94f8-45d2-8e38-ed9b56cb8695 โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ state: running โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ url: https://aged-waterfall-qraz0s7d.dal0.kraft.host โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ image: ruby32-rails@sha256:fdd46011408fdee05644665ad59b24115737e3fdb352169ec2f3f16a45d4f31d โ”œโ”€โ”€โ”€โ”€โ”€ boot time: 577.34 ms โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ memory: 1024 MiB โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€ service: aged-waterfall-qraz0s7d โ”œโ”€โ”€ private fqdn: ruby32-rails-apa93.internal โ”œโ”€โ”€โ”€โ”€ private ip: 172.16.3.3 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ args: /usr/bin/ruby /app/bin/rails server -b 0.0.0.0

In this case, the instance name is ruby32-rails-apa93 and the URL is https://aged-waterfall-qraz0s7d.dal0.kraft.host. They are different for each run.

Use curl to query the Unikraft Cloud instance of the Python-based HTTP web server:

TerminalCode
curl https://aged-waterfall-qraz0s7d.dal0.kraft.host/hello
Code
[...] <body> <h1>Hello World</h1> Hello, World! </body> [...]

At any point in time, you can list information about the instance:

TerminalCode
kraft cloud instance list
Code
NAME FQDN STATE CREATED AT IMAGE MEMORY ARGS BOOT TIME ruby32-rails-apa93 aged-waterfall-qraz0s7d.dal0.kraft.host running 2 minutes ago ruby32-rails@sha256:fdd46011408fdee... 1.0 GiB /usr/bin/ruby /app/bin/rails server -b 0.0.0.0 577.34 ms

When done, you can remove the instance:

TerminalCode
kraft cloud instance remove ruby32-rails-apa93

Customize your Application

To customize the application, update the files in the repository, listed below:

  • app/ and config/: the contents to update the Rails setup
  • Kraftfile: the Unikraft Cloud specification
  • Dockerfile: the Docker-specified application filesystem

The app/ and config/ directories contain files that are to overwrite generated Rails files:

TerminalCode
tree app/ config/
Code
app/ |-- controllers/ | `-- hello_controller.rb `-- views/ `-- hello/ `-- index.html.erb config/ |-- environments/ | `-- development.rb `-- routes.rb

These files add the configuration (controller, view, route) to print the Hello, World! message in Rails. They overwrite the generated Rails configuration to provide the application setup. Update these files, and other files, with required contents for your own application.

Lines in the Kraftfile have the following roles:

  • spec: v0.6: The current Kraftfile specification version is 0.6.

  • runtime: ruby:3.2: The Unikraft runtime kernel to use is Ruby 3.2.

  • rootfs: ./Dockerfile: Build the application root filesystem using the Dockerfile.

  • cmd: ["/usr/bin/ruby", "/app/bin/rails", "server", "-b", "0.0.0.0"]: Use /usr/bin/ruby /app/bin/rails server -b 0.0.0.0 as the starting command of the instance.

Lines in the Dockerfile have the following roles:

  • The RUN lines install Rails and generate the default application setup for a hello application.

  • COPY . /app/: Copies the local files (from app/ and config/) to the application, overwriting generated contents to provide the user-specified application.

  • FROM scratch: Build the filesystem from the scratch container image, to create a base image.

  • COPY --from=build /app /app: Copy the application directory to the filesystem.

The following options are available for customizing the application:

  • If only updating the implementation in the app/ or config/ directories, or adding new directories or files to overwrite Rails generated files, no other change is required.

  • If changing the application name, change the hello name in RUN rails generate controller hello to a new one. The file names in the app/ and config/ directories have to be similarly updated.

  • More extensive changes may require expanding the Dockerfile with additional Dockerfile commands.

Learn More

Use the --help option for detailed information on using Unikraft Cloud:

TerminalCode
kraft cloud --help

Or visit the CLI Reference.

Last modified on