2015-06-07 22:59:58 +02:00
<!-- [metadata]>
+++
title = "Quickstart Guide: Compose and Rails"
description = "Getting started with Docker Compose and Rails"
keywords = ["documentation, docs, docker, compose, orchestration, containers"]
[menu.main]
parent="smn_workw_compose"
weight=5
+++
<![end-metadata]-->
## Quickstart Guide: Compose and Rails
2014-01-27 16:03:21 +01:00
2015-02-27 03:58:06 +01:00
This Quickstart guide will show you how to use Compose to set up and run a Rails/PostgreSQL app. Before starting, you'll need to have [Compose installed ](install.md ).
### Define the project
Start by setting up the three files you'll need to build the app. First, since
your app is going to run inside a Docker container containing all of its
dependencies, you'll need to define exactly what needs to be included in the
container. This is done using a file called `Dockerfile` . To begin with, the
Dockerfile consists of:
2014-01-27 16:03:21 +01:00
2015-01-07 20:45:00 +01:00
FROM ruby:2.2.0
2014-01-27 18:58:58 +01:00
RUN apt-get update -qq & & apt-get install -y build-essential libpq-dev
2014-01-27 16:03:21 +01:00
RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
2015-10-07 20:14:53 +02:00
ADD Gemfile.lock /myapp/Gemfile.lock
2014-01-27 16:03:21 +01:00
RUN bundle install
ADD . /myapp
2015-02-27 03:58:06 +01:00
That'll put your application code inside an image that will build a container with Ruby, Bundler and all your dependencies inside it. For more information on how to write Dockerfiles, see the [Docker user guide ](https://docs.docker.com/userguide/dockerimages/#building-an-image-from-a-dockerfile ) and the [Dockerfile reference ](http://docs.docker.com/reference/builder/ ).
2014-01-27 16:03:21 +01:00
2015-02-27 03:58:06 +01:00
Next, create a bootstrap `Gemfile` which just loads Rails. It'll be overwritten in a moment by `rails new` .
2014-01-27 16:03:21 +01:00
source 'https://rubygems.org'
2015-01-07 17:45:48 +01:00
gem 'rails', '4.2.0'
2014-01-27 16:03:21 +01:00
2015-10-27 20:04:35 +01:00
You'll need an empty `Gemfile.lock` in order to build our `Dockerfile` .
$ touch Gemfile.lock
2015-10-16 08:28:27 +02:00
Finally, `docker-compose.yml` is where the magic happens. This file describes the services that comprise your app (a database and a web app), how to get each one's Docker image (the database just runs on a pre-made PostgreSQL image, and the web app is built from the current directory), and the configuration needed to link them together and expose the web app's port.
2014-01-27 16:03:21 +01:00
db:
2014-08-07 23:54:59 +02:00
image: postgres
2014-01-27 16:03:21 +01:00
web:
build: .
2015-01-07 17:45:48 +01:00
command: bundle exec rails s -p 3000 -b '0.0.0.0'
2014-01-27 16:03:21 +01:00
volumes:
- .:/myapp
ports:
2014-02-20 13:53:43 +01:00
- "3000:3000"
2015-10-16 08:28:27 +02:00
links:
- db
2014-01-27 16:03:21 +01:00
2015-02-27 03:58:06 +01:00
### Build the project
With those three files in place, you can now generate the Rails skeleton app
using `docker-compose run` :
2014-01-27 16:03:21 +01:00
2015-01-20 18:29:28 +01:00
$ docker-compose run web rails new . --force --database=postgresql --skip-bundle
2014-01-27 16:03:21 +01:00
2015-02-27 03:58:06 +01:00
First, Compose will build the image for the `web` service using the
`Dockerfile` . Then it'll run `rails new` inside a new container, using that
image. Once it's done, you should have generated a fresh app:
2014-01-27 16:03:21 +01:00
$ ls
2015-01-20 18:29:28 +01:00
Dockerfile app docker-compose.yml tmp
2014-01-27 16:03:21 +01:00
Gemfile bin lib vendor
2015-01-20 18:44:48 +01:00
Gemfile.lock config log
README.rdoc config.ru public
2014-01-27 16:03:21 +01:00
Rakefile db test
2015-10-27 20:04:35 +01:00
The files `rails new` created are owned by root. This happens because the
container runs as the `root` user. Change the ownership of the new files.
sudo chown -R $USER:$USER .
2015-02-27 03:58:06 +01:00
Uncomment the line in your new `Gemfile` which loads `therubyracer` , so you've
got a Javascript runtime:
2014-01-27 16:03:21 +01:00
gem 'therubyracer', platforms: :ruby
2015-02-27 03:58:06 +01:00
Now that you've got a new `Gemfile` , you need to build the image again. (This,
and changes to the Dockerfile itself, should be the only times you'll need to
rebuild.)
2014-01-27 16:03:21 +01:00
2015-01-20 18:29:28 +01:00
$ docker-compose build
2014-01-27 16:03:21 +01:00
2015-10-27 20:04:35 +01:00
2015-02-27 03:58:06 +01:00
### Connect the database
The app is now bootable, but you're not quite there yet. By default, Rails
expects a database to be running on `localhost` - so you need to point it at the
`db` container instead. You also need to change the database and username to
align with the defaults set by the `postgres` image.
2014-01-27 16:03:21 +01:00
2015-10-27 20:04:35 +01:00
Replace the contents of `config/database.yml` with the following:
2014-01-27 16:03:21 +01:00
development: & default
adapter: postgresql
encoding: unicode
2014-08-07 23:54:59 +02:00
database: postgres
2014-01-27 16:03:21 +01:00
pool: 5
2014-08-07 23:54:59 +02:00
username: postgres
password:
2014-08-08 20:00:10 +02:00
host: db
2014-01-27 16:03:21 +01:00
test:
< < : * default
database: myapp_test
2015-02-27 03:58:06 +01:00
You can now boot the app with:
2014-01-27 16:03:21 +01:00
2015-01-20 18:29:28 +01:00
$ docker-compose up
2014-01-27 16:03:21 +01:00
2015-02-27 03:58:06 +01:00
If all's well, you should see some PostgreSQL output, and then—after a few
seconds—the familiar refrain:
2014-01-27 16:03:21 +01:00
myapp_web_1 | [2014-01-17 17:16:29] INFO WEBrick 1.3.1
2015-01-07 17:45:48 +01:00
myapp_web_1 | [2014-01-17 17:16:29] INFO ruby 2.2.0 (2014-12-25) [x86_64-linux-gnu]
2014-01-27 16:03:21 +01:00
myapp_web_1 | [2014-01-17 17:16:29] INFO WEBrick::HTTPServer#start: pid=1 port=3000
2015-02-27 03:58:06 +01:00
Finally, you need to create the database. In another terminal, run:
2014-01-27 16:03:21 +01:00
2015-01-20 18:29:28 +01:00
$ docker-compose run web rake db:create
2014-01-27 16:03:21 +01:00
2015-08-24 21:25:25 +02:00
That's it. Your app should now be running on port 3000 on your Docker daemon. If you're using [Docker Machine ](https://docs.docker.com/machine ), then `docker-machine ip MACHINE_VM` returns the Docker host IP address.
2015-07-18 01:17:46 +02:00
2015-02-25 09:43:33 +01:00
2015-02-27 03:58:06 +01:00
## More Compose documentation
2015-02-25 09:43:33 +01:00
2015-11-18 11:17:23 +01:00
- [User guide ](index.md )
2015-05-12 13:44:43 +02:00
- [Installing Compose ](install.md )
2015-10-09 16:49:41 +02:00
- [Getting Started ](gettingstarted.md )
2015-05-12 13:44:43 +02:00
- [Get started with Django ](django.md )
2015-09-16 17:01:43 +02:00
- [Get started with WordPress ](wordpress.md )
2015-10-13 13:01:19 +02:00
- [Command line reference ](./reference/index.md )
2015-10-13 21:23:27 +02:00
- [Compose file reference ](compose-file.md )