CODEX

Dockerize your Ruby on Rails App

Allen Oliver M Chun
Jan 6 · 4 min read
Image for post
Image for post

What and Why Docker is Useful?

Docker allows you to package up an application or service with all of its dependencies into a standardized unit. This unit is typically labeled as a Docker image.

Installing Docker

Download and Install Docker from https://www.docker.com/

Creating Rails App

rails new rails_docker -d postgresql
cd rails_docker
bundle install
rails server

Setup Docker for Rails

Create a Dockerfile on the root directory of your project and add the following content.

FROM ruby:2.6.0-slimRUN apt-get update -qq \
&& apt-get install -y \
# Needed for certain gems
build-essential \
# Needed for postgres gem
libpq-dev \
# Others
nodejs \
vim-tiny \
# The following are used to trim down the size of the image by removing unneeded data
&& apt-get clean autoclean \
&& apt-get autoremove -y \
&& rm -rf \
/var/lib/apt \
/var/lib/dpkg \
/var/lib/cache \
/var/lib/log
# Changes localtime to Singapore
RUN cp /usr/share/zoneinfo/Asia/Singapore /etc/localtime
RUN mkdir /rails_dockerWORKDIR /rails_dockerCOPY Gemfile /rails_docker/GemfileCOPY Gemfile.lock /rails_docker/Gemfile.lockRUN bundle installADD . /rails_dockerCMD bash -c "rm -f tmp/pids/server.pid && rails s -p 3000 -b '0.0.0.0'"
  • Running the dependencies
  • Changing the image timezone to Singapore (or change it depending on the timezone you want)
  • Creating a rails_docker directory inside the image and setting this up as a work directory
  • Copying the Gemfile and Gemfile.lock from the project to the rails_docker directory image
  • Running bundle install in the image
  • Running rails app using shell command in localhost:3000
docker build .
docker tag <image-id> rails_docker:v1.0.0docker run -it -p 3000:3000 rails_docker:v1.0.0 bundle exec rails server -b 0.0.0.0 -p 3000

Run the Rails app using docker-compose

We now have a working Ruby on Rails app on top of Docker.

version: '3'
services:
db:
image: postgres:alpine
restart: always
volumes:
- ./tmp/db:/var/lib/postgresql/data
ports:
- "5432:5432"
app:
build: .
restart: always
command: bash -c "rm -f tmp/pids/server.pid && rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/rails_docker
- bundle_path:/bundle
ports:
- "3000:3000"
depends_on:
- db
volumes:
bundle_path:
  • Rails App which depends on db and running on port 3000
Image for post
Image for post
Image for post
Image for post
Access Rails App using http:localhost:3000
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") %>
development:
<<: *default
database: rails_docker_development
host: db
username: postgres
port: 5432
docker-compose run app rake db:create

Running Rails Console

As part of our development, it’s also important to access our database through rails console

Image for post
Image for post

Distributing Image to DockerHub

To distribute the image in DockerHub. Run the following commands

  • Create Respository
  • On the local machine run docker build -t <username>/<container_image> .
  • Push it to Dockerhub Repo using docker push <username>/<container_name>
  • To pull this image run `docker pull <username>/<container_name>

CodeX

Everything connected with Code & Tech!

Allen Oliver M Chun

Written by

Full Stack Software Developer based in Singapore. Software Developer since 2012. Specialized in Ruby on Rails, Vue.js. AWS Development.

CodeX

CodeX

Everything connected with Code & Tech!

Allen Oliver M Chun

Written by

Full Stack Software Developer based in Singapore. Software Developer since 2012. Specialized in Ruby on Rails, Vue.js. AWS Development.

CodeX

CodeX

Everything connected with Code & Tech!

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store