Setup Rails development environment with Docker

If you don’t have docker installed you can check out my last blog http://allenwei.cn/setup-docker-on-your-mac/

Build your first rails docker image

Assume you already have a rails git repo

First We need create a Dockerfile in your rails folder

This time we’ll use base image for latest ruby version, you can see other version here https://registry.hub.docker.com/u/library/ruby/

RVM? no you don’t need it, because each container is isolated, so you don’t need version management tool any more.

Here is the Dockerfile

FROM ruby:onbuild

CMD rails s

That’s it, really simple right?

Before you start the container, we need build it first

docker build -t my-rails-app .

How to setup postgresql

You still need database like mysql and postgresql, you don’t need install it on your mac, just use Docker container

You can find postgresql docker image here https://registry.hub.docker.com/_/postgres/

To start the postgresql container

docker run --name my-postgresql -e POSTGRES_PASSWORD=mypassword -d postgres

Next you need link to postgresql to your rails project
How the rails app connect to postgresql?

When you link postgresql container to your rails app container, all configuration for postgresql as ENV variable

You can check those configuration by run follow command

docker run --name my-rails-app --link my-postgresql:postgresql --rm my-rails-app env

you can see environment variables like:


When you know the postgresql environment, you can edit your rails database config in config/database.yml

default: &default
  adapter: postgresql
  pool: 5
  username: postgres
  host: <%= ENV['POSTGRESQL_PORT_5432_TCP_ADDR'] %>
  port: <%= ENV['POSTGRESQL_PORT_5432_TCP_PORT'] %>

  <<: *default
  database: myapp_development

  <<: *default
  database: myapp_test

  <<: *default
  database: myapp_production

if you didn’t add pg gem into your Gemfile, you need added gem 'pg' into you your Gemfile and rebuild your docker image

let’s try out our pg configuration

    docker run --link my-postgresql:postgresql -it  my-rails-app rake db:create

OK, we’ve setup our environment, let’s start to change our code.

I still ssh to my docker container? no, you edit your code on your mac directly, let’s see how we do it.

We can mount your local folder on docker container, let’s mount current rails folder to docker container

docker run --link my-postgresql:postgresql -it -v $(pwd):/usr/src/app my-rails-app bundle install

you can see when we run bundle install again, it is very fast, because the installed gem is already in the docker image

to access your rails server, you still need one more step, port docker port to you local machine

docker run --link my-postgresql:postgresql -it -v $(pwd):/usr/src/app -p 3000:3000 my-rails-app rails s

Because on Mac, we can only access docker container through boot2docker, we can run boot2docker ip to get the ip address

there is a convienent way to do it

open http://$(boot2docker ip):3000

you can give a name to your container and run it as daemon

docker run --name=my-rails-app --link my-postgresql:postgresql -it -v $(pwd):/usr/src/app -p 3000:3000 -d my-rails-app rails s

So when you need restart your server, just restart the container by it’s name

docker restart my-rails-app

How to run test

There is two ways:

1) start new container to test your test. It possible since start a new container is super faster

docker run --link my-postgresql:postgresql -it -v $(pwd):/usr/src/app my-rails-app rspec

2) You can log in to your started docker container

docker exec -it my-rails-app bash

in next blog http://allenwei.cn/docker-dependencies-with-fig/, I’ll introduce a way to simplfy the flow