Create ruby on rails app using docker and docker-compose

Create ruby on rails app using docker

Today we will learn how to create a ruby on rails using docker and docker-compose. I was waiting to publish this tutorial from long time. This tutorial will help in creating ruby on rails app using docker from scratch.


  • Docker and docker-compose must be installed or use this tutorial to install docker
  • I am using MacBook and i have installed docker for mac

Create application directory

Firstly create an application directory. I am using MacBook for this tutorial. I mostly use docker directory to create docker projects in my home directory.

mkdir ~/docker/docker-rails
cd ~/docker/docker-rails

Create Gemfile

Now create a Gemfile in our project directory and add the following contents in the file

vim Gemfile

source ''
gem 'rails', '4.2.0'

Create empty Gemfile.lock

Now create a empty Gemfile.lock in the project directory using touch file.

touch Gemfile.lock

Create Dockerfile

Now we will create a file named Dockerfile in the project directory in order to build docker image. Create a file and add this:

vim Dockerfile

FROM ruby:2.2.0

MAINTAINER Chetan Kapoor ([email protected])

RUN apt-get update -qq
RUN apt-get install -y build-essential libpq-dev
RUN mkdir /app
ADD Gemfile /app/Gemfile
ADD Gemfile.lock /app/Gemfile.lock
RUN bundle install
ADD . /app

Create docker-compose.yml file

Now we will create the docker-compose.yml file. Create a file and add this in the file:

vim docker-compose.yml

  image: postgres
  build: .
  command: bundle exec rails s -p 3000 -b ''
    - .:/app
    - "3000:3000"
    - postgres

Create the rails app

Now we will create the rails app using docker-compose. Execute the following command to create rails app:

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

The docker-compose run command will create a rails app in the current directory. Once the docker-compose finishes you can see rails app by executing following command:

ls -la

Build the app

Now we will build our app using docker-compose. Before open the Gemfile and uncomment the following line:

gem 'therubyracer', platforms: :ruby

Then execute the following command to build the app.

docker-compose build

Database connection

We need to change config for database connection. Open config/database.yml with your favourite editor and replace the lines with following:

development: &default
  adapter: postgresql
  encoding: unicode
  database: postgres
  pool: 5
  username: postgres
  host: postgres

  <<: *default
  database: web_test

Start the rails app with docker-compose

Now we can start our rails app using docker-compose. Docker-compose will spin up web and postgres container. Just execute the following command:

docker-compose up -d

View the app

Now our app is up and running, we can view the app in the browser. If you are creating app on locally then go to http://localhost:3000


Article Name
Create ruby on rails app using docker and docker-compose

Leave a Reply

Your email address will not be published.


The reCAPTCHA verification period has expired. Please reload the page.

48-Hour Flash Sale! Courses from just $10.99