Monday 6 February 2017

Textual Representation of logo

Dockerizing an existing Rails application

  Docker is a relatively new and rapidly growing project that allows creating very light “virtual machines”.


There are no specific skills needed for this tutorial beyond a basic comfort with the command line and using a text editor. The following services are needed:
  • Docker Hub (Login - Signup if you haven't )
  • A Rails application

Setting up your computer

Getting all the tooling setup on your computer can be a daunting task, but thankfully as Docker has become stable, getting Docker up and running on your favorite OS has become very easy. First, we'll install Docker.
Until a few releases ago, running Docker on OSX and Windows was quite a hassle. Lately, however, Docker has invested significantly into improving the onboarding experience for its users on these OSes, thus running Docker now is a cakewalk. The getting started guide on Docker has detailed instructions for setting up Docker on Mac, Linux, and Windows.
Once you are done installing Docker, test your Docker installation by running the following:
$ docker run hello-world

Hello from Docker.

This message shows that your installation appears to be working correctly.
Starting with a Rails Application: I will not be going through docker commands as that would be found on dockers website and many blog sites. There are different ways you can deploy your Ruby applications in a Docker container. You can either choose one of the many existing Ruby images on the public docker registry

Building the base image

This image will set a base for the rest of the post as we will use the resulting image to create our set of Rails images.
The image will contain all the things Rails expects to compile and run properly on a Debian based OS. I am not going to talk about the required packages, instead, I am going to focus on the separation of concerns and configuration of the set of images.
One thing to keep in mind is that the image we’ll build is only for RubyOnRails and will not contain any database related packages. If there’s a need to install a gem with native extensions requiring “extra” packages those should go into that specific image unless all of your apps require it.
What should you expect from this image?
This image will not complain about any TTY warnings during installation because of the flag(noninteractive) we are using, all your applications will use as encoding(en_US.UTF-8) .thencoding.
Find the below Image and explanation as followed.
  • I am using ruby 1.9.3 for my Project.
  • Gemfile is already built with rails 3.2.3, You can use your own Gemfile.
FROM ubuntu:trusty  # Using Ubuntu OS
MAINTAINER "Santosh Mohanty <>" # Maintainer Name
RUN apt-get update # Updating OS

ENV PATH /usr/local/rvm/bin:$PATH  # Set ENV Path

RUN apt-get update && apt-get -y upgrade && apt-get -y install ruby 1.9.3 # Installation of Ruby
RUN ln -sf /usr/bin/ruby1.9.3 /etc/alternatives/ruby

# basics
RUN apt-get install -y build-essential
RUN apt-get install -y mysql-client libmysqlclient-dev openssl libreadline6 
                       libreadline6-dev curl zlib1g zlib1g-dev libssl-dev libyaml-dev
                       libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev
                       ncurses-dev automake libtool bison subversion pkg-  config gawk 
                       libgdbm-dev libffi-dev npm

RUN gem install bundler
ADD Gemfile /app/Gemfile
ADD Gemfile.lock /app/Gemfile.lock
RUN bundle install
ADD . /app      # Adds your Project Structure to docker
CMD ["rails","s"]

EXPOSE 3000 # Exposing PORT 3000 for Development

Executing the Image:

Follow the Below steps to Build your Docker Image from Dockerfile:
docker build -t rails_image ~/workspace/PATH_TO_DOCKERFILE
docker run -p 3000:3000 rails_image # Mapping container port 3000 to local port 3000
This would run your rails application inside docker container
Few Imp commands:
docker run -it rails_image /bin/bash # this would bring up the bash prompt of docker
docker images # List Images
docker rmi IMAGE_ID -f  # remove Image
docker ps  # for listing running containers.
You may find difficulties in connecting to DB from Container, This can be solved running a DB Image or pointing to Specific IP of DB instead of local IP.

I will be writing a blog on how to connect MySQL DB Image to Rails Image using docker compose.