Dockerize a Rails development environment integrated with Postgresql, Redis, and Elasticsearch using Docker Compose

In this post I'll share an example configuration to Docker-ize a Rails development environment integrated with Postgresql, Redis, and Elasticsearch using Docker Compose. In this guide I'll focus on interoperability between running your code in Docker and locally.

I started development using the pre-built OSX download for Docker which uses Xhyve. I had some problems with container volume peristence, so for this post I'll demonstrate how to use VirtualBox as your docker-machine.

Initial Docker installation

# install docker packages via brew
brew install docker docker-machine docker-compose

# create a new virtualbox docker-machine
docker-machine create -d virtualbox docker-machine

# start new docker-machine (if not already running)
docker-machine start docker-machine

# set docker environment variables.
# You might want to put this in your shell profile, ex: ~/.profile
eval $(docker-machine env docker-machine)

Initial project creation

# create directory for Rails and Docker files
mkdir rails_dev

# create RVM files. Note: these will not be used by Docker, just for local development
echo ruby-2.3.1 > rails_dev/.ruby-version
echo rails_docker > rails_dev/.ruby-gemset
cd rails_dev

# install rails gem (latest was and create the new project files
gem install rails
rails new . -d postgresql

Add required gems: devise for authentication, elasticsearch for searching, and redis-rails for sessions/caching. edit file: Gemfile, add:

gem 'devise'
gem 'elasticsearch-model'
gem 'elasticsearch-rails'
gem 'redis-rails'

Normally you could run bundle install to install the new gems, but we can wait for the Docker build to do so.

Rails configuration

Update database configuration to use environment variables. edit file: config/database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  host: <%= ENV.fetch('POSTGRESQL_HOST', 'localhost') %>
  password: <%= ENV.fetch('POSTGRESQL_PASSWORD', '') %>
  username: <%= ENV.fetch('POSTGRESQL_USERNAME', 'postgres') %>

Set session store to use Redis, edit file: config/initializers/session_store.rb

Rails.application.config.session_store :redis_store, servers: "redis://#{ENV.fetch('REDIS_HOST', 'localhost')}:6379/0/session"

Set cache store to use Redis as well, edit file: config/environments/development.rb

# replace:
config.cache_store = :memory_store

# with:
config.cache_store = :redis_store, "redis://#{ENV.fetch('REDIS_HOST', 'localhost')}:6379/0/cache", { expires_in: 90.minutes }

Create a Redis initializer to instantiate a Redis client. new file: config/initializers/redis.rb

module RedisClient
  class << self
    def redis
      @redis ||= ENV.fetch('REDIS_HOST', 'localhost'))

Run generator for Devise installation and create the User model:

rails generate devise:install
rails generate devise User

Create an initializer for Elasticsearch to set the client host from the environment variable, new file: config/initializers/elasticsearch.rb

Elasticsearch::Model.client = ENV.fetch('ELASTICSEARCH_HOST', 'localhost'))

Add Elasticsearch logging, edit file: config/application.rb, add:

require 'elasticsearch/rails/instrumentation'

Docker integration

At this point the Rails configuration and project files are ready to live in a Docker container.

Create the Dockerfile which will be used to create the Rails container, new file: Dockerfile.

FROM ruby:2.3.1

RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs netcat

RUN mkdir /rails
WORKDIR /rails
ADD Gemfile /rails/Gemfile
ADD Gemfile.lock /rails/Gemfile.lock
RUN bundle install
ADD . /rails

Read more about using Docker Compose with Rails in this article.

And the contents of docker-compose.yml below. Some key points to highlight:

  • top level volumes are used for persistence
  • the web container "links" and depends on the postgresql, elasticsearch, and redis containers
  • each container's main service is exposing the default ports for connectivity
  • environment variables are set in the main web container
version: '2'
    image: elasticsearch
      - "9200:9200"
      - "9300:9300"
      - elasticsearch:/usr/share/elasticsearch/data
    image: postgres
      - "5432:5432"
      - postgres:/var/lib/postgresql/data
    image: redis
      - "6379:6379"
      - redis:/data
    build: .
    command: bin/docker-start
      - ELASTICSEARCH_HOST=elasticsearch
      - POSTGRESQL_HOST=postgres
      - POSTGRESQL_USERNAME=postgres
      - REDIS_HOST=redis
      - postgres
      - elasticsearch
      - redis
      - .:/rails
      - "3000:3000"
      - postgres
      - elasticsearch
      - redis
  elasticsearch: {}
  postgres: {}
  redis: {}

Here is the script I chose to execute when the main web container starts, new file: bin/docker-start


set -x

# wait for postgresql
until nc -vz $POSTGRESQL_HOST 5432; do
  echo "Postgresql is not ready, sleeping..."
  sleep 1
echo "Postgresql is ready, starting Rails."

# wait for elasticsearch
until nc -vz $ELASTICSEARCH_HOST 9200; do
  echo "Elasticsearch is not ready, sleeping..."
  sleep 1
echo "Elasticsearch is ready, starting Rails."

# optional
# rm /rails/tmp/pids/

# setup database and start puma
RAILS_ENV=development bundle exec rake db:create
RAILS_ENV=development bundle exec rake db:migrate
RAILS_ENV=development bundle exec rake db:seed
RAILS_ENV=development bundle exec rails s -p 3000 -b ''

Running Docker

Docker can now be built and started via:

docker-compose build
docker-compose up

You can look up the docker machine IP and open in a browser via:

open "http://`docker-machine ip docker-machine`:3000"

Connectivity Test Code

To test that the containers are working together, I added a model and controller to execute some statements and display the results.

New migration to create the table for TestModel, new file: db/migrate/20161029123507_create_test_models.rb

class CreateTestModels < ActiveRecord::Migration[5.0]
  def change
    create_table :test_models do |t|
      t.string :title, null: false
      t.text :body, null: false


Integrate the TestModel with Elasticsearch and Redis, file: app/models/test_model.rb

require 'elasticsearch/model'

class TestModel < ApplicationRecord
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  validates :body, presence: true
  validates :title, presence: true

  REDIS_COUNTER_KEY = "#{self}:counter"

  def self.increment_count
    RedisClient.redis.incr REDIS_COUNTER_KEY

  def self.index_mapping
    __elasticsearch__.client.perform_request('GET', "#{index_name}/_mapping", {}, nil).body[index_name]['mappings'][ActiveSupport::Inflector.singularize(index_name)]

  def = nil, options = {})
    search_definition = {
      size: options[:size] || 10,

    if query.present?
      search_definition[:query] = {
        bool: {
          should: [
            multi_match: {
              query: query,
              fields: %w(title body),
              operator: 'and'

Create a Pages controller to execute some test queries and display the results, new file: app/controllers/pages_controller.rb

class PagesController < ApplicationController
  before_action :authenticate_user!

  def index
    # postgresql model data
    @test_models = TestModel.all

    # elasticsearch data
    @index_mapping = TestModel.index_mapping
    @search_results ='test')

    # redis data
    @increment_count = TestModel.increment_count
    @redis_keys = RedisClient.redis.keys

The corresponding view ERB file: app/views/pages/index.html.erb

<h2>Test Status</h2>

# Redis counter:
<%= @increment_count %>

# Redis keys:
<%= @redis_keys.join("\n") %>

# Model data:
<%= JSON.pretty_generate( %>

# Index mapping:
<%= JSON.pretty_generate(@index_mapping) %>

# Search results:
<%= JSON.pretty_generate(@search_results) %>

Added routes for the new controller, file: config/routes.rb

Rails.application.routes.draw do
  devise_for :users
  resources :pages, only: [:index]
  root 'pages#index'

Last I created a simple seeds script to create a User and TestModel. edit file: db/seeds.rb

User.find_or_create_by!({email: ENV.fetch('DEVISE_ADMIN_EMAIL', '')}) do |user|
  user.password = 'password'

TestModel.find_or_create_by!({title: 'test title', body: 'test body'})

I executed: open "http://`docker-machine ip docker-machine`:3000" to open the Rails docker app. Here is the sample output from the Rails container:

Docker Rails Ouput

To list the created docker containers, execute: docker ps -a

docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                                            NAMES
e088939e2f07        railsdev_web        "bin/docker-start"       About a minute ago   Up About a minute>3000/tcp                           railsdev_web_1
74867756a019        elasticsearch       "/docker-entrypoint.s"   About a minute ago   Up About a minute>9200/tcp,>9300/tcp   railsdev_elasticsearch_1
99c4f8208cc1        postgres            "/docker-entrypoint.s"   About a minute ago   Up About a minute>5432/tcp                           railsdev_postgres_1
c2627ec9928f        redis               ""   About a minute ago   Up About a minute>6379/tcp                           railsdev_redis_1