Slim Ruby-On-Rails Containers

This Slim Starter Kit will show you how to build a RoR `Hello World` application and use the Slim platform to secure it for production.
sk-g sk-g

Hello Ruby developers!

Optimize and harden your containerized applications the easy way — with Slim.AI.

This Starter Kit will help you proactively remove vulnerabilities from your applications.

Simply replace the application code here with your own application, run it through Slim.AI's automated container optimization process, and you'll remove up to 90-percent of the image's vulnerabilities while also making it up to 10X smaller.

No more chasing down hard to patch vulns that your application isn't even using, and you can use any base image you want — even ruby:latest.

Optimization Results

Result of optimized Rails image

Slimming this Rails container results in 93-percent reduction in overall vulnerabilities.

Vulnerability difference by severity

Result of optimized Rails image

Note: Vulnerability scanners do not always accurately reflect changes in containers. Please verify your results.

Get Started

To start a Rails application application, you'll need the following libraries installed locally, or running in a dev environment link GitPod, Docker Environments, or Code Spaces.

ruby 3.1.2
rails ~> 7.0.3

To run locally use:

bundle install
RAILS_ENV=development bundle exec rails s -p 3000 -b '0.0.0.0'

While using docker:

set RAILS_ENV as required in Dockerfile.

update bin/start to switch between puma and rails server.

Sample Application

Our sample application is a simple REST API that merely returns "Hello World!".

While this app is simple, it's a great starting point for more complex development.

Project structure

├── app
│   ├── channels
│   │   └── application_cable
│   │       ├── channel.rb
│   │       └── connection.rb
│   ├── controllers
│   │   ├── application_controller.rb
│   │   ├── concerns
│   │   └── hello_controller.rb
│   ├── jobs
│   │   └── application_job.rb
│   ├── mailers
│   │   └── application_mailer.rb
│   ├── models
│   │   ├── application_record.rb
│   │   └── concerns
│   └── views
│       └── layouts
│           ├── mailer.html.erb
│           └── mailer.text.erb
├── bin
│   ├── bundle
│   ├── rails
│   ├── rake
│   ├── setup
│   └── start
├── config
│   ├── application.rb
│   ├── boot.rb
│   ├── cable.yml
│   ├── credentials.yml.enc
│   ├── database.yml
│   ├── environment.rb
│   ├── environments
│   │   ├── development.rb
│   │   ├── production.rb
│   │   └── test.rb
│   ├── initializers
│   │   ├── cors.rb
│   │   ├── filter_parameter_logging.rb
│   │   └── inflections.rb
│   ├── locales
│   │   └── en.yml
│   ├── puma.rb
│   ├── routes.rb
│   └── storage.yml
├── config.ru
├── db
│   └── seeds.rb
├── Dockerfile
├── Gemfile
├── Gemfile.lock
├── lib
│   └── tasks
├── log
├── public
│   └── robots.txt
├── Rakefile
├── README.md
├── storage
├── test
│   ├── channels
│   │   └── application_cable
│   │       └── connection_test.rb
│   ├── controllers
│   │   └── hello_controller_test.rb
│   ├── fixtures
│   │   └── files
│   ├── integration
│   ├── mailers
│   ├── models
│   └── test_helper.rb
└── vendor

Code sample

class HelloController < ApplicationController
    def index
        yourip = request.remote_ip
        ruby_version = RUBY_VERSION
        rails_version = Rails::VERSION::STRING
        render json: { hello:"world", yourip:yourip,ruby_version:ruby_version,rails_version:rails_version }
      end
end

Replace this placholder code with your own application code, and install any necessary dependencies, to create your own slimmable container.

Sample Dockerfile

Our Dockerfile builds off of the rails:latest image and starts at 979MB.

FROM ruby:latest

# Set a variable for the install location.
ARG RAILS_ROOT=/usr/src/app
RUN mkdir -p $RAILS_ROOT
ENV RAILS_ENV production
ENV BUNDLE_APP_CONFIG="$RAILS_ROOT/.bundle"
WORKDIR $RAILS_ROOT
#Copy Source Files
COPY . .
#Remove the lock file and fetch all gems again
RUN rm -f Gemfile.lock
RUN bundle install

RUN chmod +x bin/start
EXPOSE 3000
CMD ["bin/start"]

The slimming process reduces the size by 10X to just 97MB.

Try it yourself on Slim.AI