Set up a Rails app using Docker and Compose

John John (304)
20 minutes

Learn how to set up a Ruby on Rails/PostgreSQL app using docker and docker compose.

Posted in these interests:
h/docker12 guides
h/webdev60 guides
h/rails3 guides

The first step is to install the Docker Toolbox.

On this page, find your platform and run the installation. On a Mac, you'll be installing Docker, Docker Compose, and Docker Machine. Docker Machine will use a Linux Virtual Machine to actually run Docker.

If you're using a Mac, you'll want to start working on Docker by opening the Docker Quickstart Terminal. This will ensure that your environment is set up properly since Docker is actually running in a VM.

I typically put projects like this in my home directory in a directory called Developer. We'll call this test app "rails-docker".

mkdir -p ~/Developer/rails-docker
cd ~/Developer/rails-docker

Create a file called Dockerfile and add this:

FROM ruby:2.2.0
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

These are the instructions for building the base image for your rails app.

Create a file called Gemfile and add the following:

source ''
gem 'rails', '4.2.0'

In your Dockerfile, you added a line RUN bundle install. This command will install the packages specified in your Gemfile.

The Gemfile.lock file is where the bundler will record the exact versions of the packages that were installed. You must add this file in order for the image to be built properly.

touch Gemfile.lock

Create a file called docker-compose.yml and add the following:

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

This compose file contains the instructions for how to build and link the containers required to run your rails app. As you can see there are two services - db and web. There are a few important parts of this file to take note of. First, we are mounting the current directory to /app. This means you can work on the app locally, and the changes will be available from within the container. And the command we specified will start the rails server, and it will be run whenever you run docker-compose up.

Now we're ready to actually create the rails app.

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

The docker-compose run command will spin up a container to run this single command. Once it finishes you can see the rails app created in your current directory:

ls -lha

At this point you'll want to change the owner of the files.

sudo chown -R $USER:$USER .

You'll notice if you examine your Gemfile that it has changed a bit. At this point, you should go ahead a rebuild the image so that you won't have to run bundle install every time you start a new container from your existing image.

First, open the Gemfile and uncomment the following line:

gem 'therubyracer', platforms: :ruby

Then run

docker-compose build

Edit the file config/database.yml and replace the contents with the following:

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

  <<: *default
  database: app_test

Notice the host is db. When we linked the containers in our docker-compose.yml file our web container can access our db container using the hostname db.

docker-compose run web rake db:create

If you're running docker-machine, you can find your machine's IP address by running:

docker-machine ip default

(Assuming you are using the default machine name.)

And open your browser and navigate to {IP}:3000. Mine is

If you're running Docker locally, you can simply go to http://localhost:3000.

John John (304)
15 minutes

What is docker? Docker is a tool that allows you to deploy applications inside of software containers.