Sunday 1 January 2017

Running Rails Application With Docker

How to Run Rails Application with Docker


In this tutorial we will walk you through on "How to run rails application with Docker". If you are still wondering about Docker and do not know how it works then please first complete the tutorial - Introduction to Docker.

Video Tutorial:


Let's start creating our containerised rails application.

Create Required Files

We will be using docker-compose, a tool for defining and running multi-container Docker application. A compose file (docker-compose.yml) is used to configure the services that our application is going to use. Using this we can simply create and start all services with a single command.

Create a directory dockerapp (run mkdir dockerapp) in which we will be placing all files like Dockerfile, docker-compose.yml, Gemfile etc.

Dockerfile - Used to specify all the dependency of our app which will get included in the container. The content of Dockerfile looks like this:

FROM ruby:2.3.3
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /myrailsapp
WORKDIR /myrailsapp
ADD Gemfile /myrailsapp/Gemfile
ADD Gemfile.lock /myrailsapp/Gemfile.lock
RUN bundle install
ADD . /myrailsapp

This will put the application code in an image that will build the container with Ruby, Bundler and all required dependencies .

Next is to create a Gemfile which will simply loads Rails and this will be overwritten when Docker will create the myrailsapp application with its own default Gemfile.

Gemfile

source 'https://rubygems.org'
gem 'rails', '5.0.0.1'

Also create an empty Gemfile.lock (run touch Gemfile.lock)

docker-compose.yml - This file tells what all services our app is going to have, here a web app and a database app. The database runs on a pre-build or a ready to use postgreSQL image and the web app would be picked up from our current directory. So the docker-compose.yml will link these and will expose a port to interact with web app.

docker-compose.yml

version: '2'
services:
  db:
    image: postgres
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/myrailsapp
    ports:
      - "3000:3000"
    depends_on:
      - db

Build

Using these files (Dockerfile, Gemfile, docker-compose.yml) we will create the skeleton of rails app using command docker-compose run

docker-compose run web rails new . --force --database=postgresql --skip-bundle

First compose will build the web service using Dockerfile then it will run the rails new inside the container. This will results in a default rails skeleton.

Note: If you are running Docker on Linux, all files created would be owned by root because container runs as the root user. Change the ownership of the new files:

  sudo chown -R $USER:$USER .

If you are running Docker for mac or windows than you don't need to change the ownership as all files would be owned by the current user.

Now, we will build the image:

docker-compose build

Every time you change the Dockerfile or Gemfile or any other configuration files then you need to build it again.

Database Configuration

In config/database.yml file, you need to change certain configuration like host to db instead of localhost as the postgres host would be db as specified by us in docker-compose.yml. Replace the configuration with the one shown below.

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: myrailsapp_development
  username: postgres
  password:
  pool: 5
  host: db


test:
  <<: *default
  database: myrailsapp_test

production:
  <<: *default
  database: myrailsapp_production
  username: myrailsapp
  password: <%= ENV['MYRAILSAPP_DATABASE_PASSWORD'] %>

Let's boot up the app, run:

docker-compose up

This will boot the app and if you head towards http://localhost:300 you can see that our app is running, but you may encounter an error message saying no database found.

Keep this app running and in a new terminal, run the following command to create a the database:


docker-compose run web rails db:create

This is it, now you can refresh the page in the browser and your app must be running fine giving you the default page as below:


congratulation, we have successfully Dockerized a rails application.

2 comments:

  1. u did not mentioned that how to install docker ?

    ReplyDelete
  2. How to run docker for existing rauls app

    ReplyDelete

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel