How to Containerize a Ruby on Rails 6 App

Intro

Ruby on Rails should give you super powers. However its not the easiest to deploy in the traditional way (deploying on a Linux virtual machine).

Docker to the Rescue ! If you already know what docker is then you can just copy and paste the docker file below. However if you don’t here it is in a nut shell:

What is Docker ?

Docker is a virtualization tool that does not need a operating system for each application. It does not emulate an entire operating system. Instead it uses the concept of “Containers” to run your applications.

Containers act like a virtual world that your application lives in. The magic of docker comes from the “Docker Engine” its what translates the system calls  from the docker container to the base operating system kernel.

Long story short you will never have to worry about dependency management ever again !!! Or have a case of  “It works on my machine”  

Now on to the docker file

RUN apt-get update && apt-get install -y postgresql-client
 
RUN curl https://deb.nodesource.com/setup_12.x | bash
 
RUN curl https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
 
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
 
RUN apt-get update && apt-get install -y nodejs yarn
 
RUN apt install imagemagick
 
ENV RAILS_SERVE_STATIC_FILES true
 
WORKDIR /application
 
COPY Gemfile Gemfile.lock ./
 
RUN bundle install
 
COPY package.json yarn.lock ./
 
COPY . .
 
RUN yarn install
 
EXPOSE 8080
 
# RUN rake assets:precompile
RUN RAILS_ENV=production bundle exec rake assets:precompile
 
ENV RAILS_ENV=production
# Start the application server
CMD ["rails", "server", "-p","8080","-b", "0.0.0.0"]

What Are we doing ?

  1. First we are using the latest “Base Image” (set of stuff we are using as a base of our dependencies of our app) of Ruby:2.6.6
  2. All the “RUN” commands install all the other dependencies. We going to be adding:
    1. postgresql-client
    2. Node
    3. Yarn
    4. imagemagick (included because I want to use “ActionText”)
  3. Now we set the Enviroment variable called RAILS_SERVE_STATIC_FILES, because we want our rails app to serve our Javascript and CSS.
  4. After that we create a directory inside our container where our project files will live.
  5. We copy over our Gem file and install our dependencies.
  6. Then we do the same with our package.json file
  7. Now we expose a port that allows our app to communicate to the outside world with, Port 8080.
  8. We run the assets:precompile command so that we compile any SCSS to CSS and put them in the right folders.
  9. And we set the RAILS_ENV to production so our Ruby on Rails app runs in high performance mode.
  10. Finally we specify the first command that runs we start our container:
    1. “rails server” : starts our server up
    2. “-p 8080” : lets our app know which port to listen to
    3. “-b 0.0.0.0” : lets app know it should listen on all the IPs of the container

Hope this helps you deploy your ruby apps a lot easier