Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Containerize your Ruby on Rails/PostgreSQL application with RHSCL Docker images

October 20, 2015
Josef Stříbný
Related topics:
ContainersLinux
Related products:
Red Hat Enterprise Linux

    New RHSCL-based Docker images that are now in beta let you easily build your own application containers even without writing any Dockerfiles. Here is an example of a Ruby on Rails application built with the Ruby 2.2 image using the PostgreSQL 9.4 image as a database backend.

    For building the application image we will use a tool called source-to-image (s2i, formally sti) which is a program that can build your application image on top of s2i images. For example the latest version of OpenShift uses s2i images to run your applications as well and you can use them with source-to-image to containerize your own applications locally.

    Prerequisites

    In this blog post I assume you are running RHEL 7 and you have the latest Docker from rhel-7-server-extras-rpms channel and that you enabled rhel-server-rhscl-7-beta-rpms channel for getting the source-to-image tool.

    If you did not, run:

    $ sudo subscription-manager repos --enable=rhel-7-server-extras-rpms
    $ sudo subscription-manager repos --enable=rhel-server-rhscl-7-beta-rpms
    $ sudo yum install -y docker source-to-image
    

    Database setup

    To use PostgreSQL image we need to provide the database user, password and name. We also need to map ports from the container to the host and map the data directory from the container to somewhere else.

    For now let's assume we want to use /data directory to store our application data:

    $ sudo mkdir -p /data
    $ sudo chmod a+rwx /data
    $ sudo chcon -t svirt_sandbox_file_t /data
    

    We need svirt_sandbox_file_t label if we are using SELinux with Docker.

    And export application password if we did not yet expose it:

    $ export APP_DATABASE_PASSWORD=password1
    

    Afterwards everything is ready to start the Docker daemon and run our database container:

    $ sudo systemctl start docker
    $ sudo docker run -d --name db -e POSTGRESQL_USER=app -e POSTGRESQL_PASSWORD=$APP_DATABASE_PASSWORD -e POSTGRESQL_DATABASE=app_production -p 5432:5432 -v /data:/var/lib/pgsql/data rhscl_beta/postgresql-94-rhel7
    

    The command above run RHSCL rhscl_beta/postgresql-94-rhel7 image in the background (-d) with the required username (app), password ($APP_DATABASE_PASSWORD from the environment) and name (app_production) while mapping the PostgreSQL standard port 5432 to the same one on the host (-p). -v option at the end then specifies volume mounts telling Docker to use our /data directory. We call this container db (--name). The name just makes it easier to reference it in the next steps.

    Use docker ps command to check that the database is running as expected (if the database is not running, it was stopped for some reason):

    $ sudo docker ps
    CONTAINER ID        IMAGE                            COMMAND                CREATED             STATUS              PORTS                    NAMES
    8293e4586ba0        rhscl_beta/postgresql-94-rhel7   "run-postgresql.sh p   12 seconds ago      Up 11 seconds       0.0.0.0:5432->5432/tcp   db
    

    Rails application setup

    For our example we need a Rails application powered by PostgreSQL database. We could use any Rails app from any remote git repository, but for the sake of completeness we create one. Therefore we need to install some dependencies first:

    $ sudo yum install -y libxml2-devel ruby-devel postgresql-devel gcc-c++ patch redhat-rpm-config nodejs curl git
    

    We need libxml2-devel, ruby-devel, postgresql-devel, gcc-c++, patch, redhat-rpm-config for installing Ruby on Rails and application dependencies, git for creating a repository and curl for checking out the results.

    Once done, install Ruby on Rails and create a brand new application with the PostgreSQL backend:

    $ gem install rails
    $ rails new app --database=postgresql && cd app
    

    To take advantage of our PostgreSQL database we build a simple ToDo lists management into our app:

    $ bin/rails generate scaffold todo_list title:string description:text
    

    Specify the database connection in config/database.yml as:

    ...
    production:
      <<: *default
      url: postgres://<%= ENV['APP_DATABASE_USER'] %>:<%= ENV['APP_DATABASE_PASSWORD'] %>@<%= ENV['APP_DATABASE_HOST'] %>/<%= ENV['APP_DATABASE_NAME'] %>
    

    As you can see we will want to connect to the $APP_DATABASE_NAME database with the $APP_DATABASE_USER database user and $APP_DATABASE_PASSWORD password to a PostgreSQL running on a given $APP_DATABASE_HOST host.

    And run bundle command to create Gemfile.lock:

    RAILS_ENV=production bundle
    

    As we are finished building the application let's check it in the source control:

    $ git init && git add * -f
    $ git config --global user.name "Git username"
    $ git commit -am 'Initial commit'
    

    The latest upstream version of source-to-image tool should not require the git repository to be created, but old versions do.

    Building the application container

    To build the application container we will use RHSCL-based rhscl_beta/ruby-22-rhel7 image with Ruby 2.2 and source-to-image tool. rhscl_beta/ruby-22-rhel7 is s2i image meaning it contains assemble s2i script that can build the application image for us.

    Install source-to-image if you haven't yet and create a new application image called app:

    $ sudo s2i build file:///$PWD rhscl_beta/ruby-22-rhel7 app
    

    First argument is our current working directory (file:///$PWD, it could be any git repository), second is the name of our s2i image and last the name of the new application image to create.

    By default it will use production environment which we want, but you could change RACK_ENV variable to something else in .s2i/environment file to avoid it.

    $ sudo docker images
    REPOSITORY                                                  TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
    app                                                         latest              ee406846e337        23 seconds ago      523.6 MB
    registry.access.redhat.com/rhscl_beta/ruby-22-rhel7         latest              a6c37f6f13e2        2 weeks ago         466.4 MB
    registry.access.redhat.com/rhscl_beta/postgresql-94-rhel7   latest              20a1960468dc        3 weeks ago         236 MB
    

    We have just build a Rails application in a container without writing any Dockerfile, how nice!

    Running the containers

    Our database container is already running, but to run our Rails application container we need to set two things. Database access and secret key base (a long randomized string which is used to verify the integrity of signed cookies used in Rails by default).

    Create a secret key base and export it:

    $ RAILS_ENV=production rake secret
    d68735d681db2ef0639f149e45bb11c805e1e46ae309b464541b89ab91b589dc8e94bc9e86508b8e35f6c5d5a5e1b48a6cef4ff9c7113b851267a021e3659e10
    
    $ export SECRET_KEY_BASE=d68735d681db2ef0639f149e45bb11c805e1e46ae309b464541b89ab91b589dc8e94bc9e86508b8e35f6c5d5a5e1b48a6cef4ff9c7113b851267a021e3659e10
    

    Last missing piece of configuration is the IP address of our database server:

    $ sudo docker inspect db | grep '"IPAddress"'
            "IPAddress": "172.17.0.42",
    $ export APP_DATABASE_HOST=172.17.0.42
    

    (Yes, we could avoid that by using Docker linking ability by specifying DB_PORT_5432_TCP_ADDR instead of APP_DATABASE_HOST in our config/database.yml. We would then run our application image with --link db:db option.)

    But let's not forget that this is our new application and so we did not even run our database migrations which has to be done beforehand. The commands themselves will be pretty similar. First we run rake db:migrate and later we run our container in the background (-d) without arguments which will run the container entrypoint (in our case the s2i run script which will eventually run rackup).

    Migrations:

    $ sudo docker run -e APP_DATABASE_USER=app -e APP_DATABASE_PASSWORD=$APP_DATABASE_PASSWORD -e APP_DATABASE_NAME=app_production -e APP_DATABASE_HOST=172.17.0.42 -e SECRET_KEY_BASE=$SECRET_KEY_BASE -e RAILS_ENV=production -p 8080:8080 app rake db:migrate
    

    Running the app itself:

    $ sudo docker run -d --name=app -e APP_DATABASE_USER=app -e APP_DATABASE_PASSWORD=$APP_DATABASE_PASSWORD -e APP_DATABASE_NAME=app_production -e APP_DATABASE_HOST=$APP_DATABASE_HOST -e SECRET_KEY_BASE=$SECRET_KEY_BASE -e RAILS_ENV=production -p 8080:8080 -e app
    

    As with our database image we needed to specify all the required environment variables — for connecting to the database, RAILS_ENV=production for the production setup of the Rails app and -p 8080:8080 to map the container port to the same one on the host. In the last command we also name our container as app.

    Check that both db and app containers are running:

    $ sudo docker ps
    CONTAINER ID        IMAGE                            COMMAND                CREATED             STATUS              PORTS                    NAMES
    d29efb225a5f        app                              "container-entrypoin   6 seconds ago       Up 5 seconds        0.0.0.0:8080->8080/tcp   app
    8293e4586ba0        rhscl_beta/postgresql-94-rhel7   "run-postgresql.sh p   About an hour ago   Up About an hour    0.0.0.0:5432->5432/tcp   db
    

    If not try docker logs db and docker logs app commands to investigate.

    If they are both running you should be able to fetch the index of our ToDo list controller:

    $ curl 0.0.0.0:8080/todo_lists
    ...
    <h1>Listing Todo Lists</h1>
    
    <table>
      <thead>
        <tr>
          <th>Title</th>
          <th>Description</th>
          <th colspan="3"></th>
        </tr>
      </thead>
    
      <tbody>
      </tbody>
    </table>
    ...
    

    That should mean everything works. You can now visit http://0.0.0.0:8080/todo_lists in your browser, add some items and check that our PostgreSQL database at /data/userdata/ is getting filled up.

    And that's it! We created a new application image for our Ruby on Rails application with source-to-image tool and RHSCL Ruby 2.2 image and made it to save data to /data using the PostgreSQL RHSCL image.

    Last updated: November 1, 2023

    Recent Posts

    • A guide to JIT checkpointing with Kubeflow Trainer on OpenShift AI

    • How to manage TLS certificates used by OpenShift GitOps operator

    • Configure a split disk on OpenShift Container Platform

    • Red Hat Enterprise Linux 10.2 and 9.8: Top features for developers

    • What GPU kernels mean for your distributed inference

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.