Back to Blog

Zero Configuration Rails

Convox can run and deploy a default Rails app with no manual configuration. Give it a try:

$ rails new myapp && cd myapp
$ convox start

Your app will boot and be available on port 5000 of the Docker host.

$ curl $(boot2docker ip):5000
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails: Welcome aboard</title>
...

Now that you have the app running locally, you can be sure it will run when deployed to Convox:

$ convox create myapp

$ convox deploy
Uploading... OK
...
Releasing... OK, RDKYJGPGXVZ
Waiting for app... OK
main: http://myapp-62376059.us-east-1.elb.amazonaws.com:5000

There you have it, a Rails app running on Convox with zero configuration. But how did it work? To find out, we can take a look at the 2 files generated by convox start, Dockerfile and docker-compose.yml.

Dockerfile

Let's break it down. Dockerfiles inherit from other Docker images, so for Rails we start with the official Ruby 2.2.2 image.

FROM ruby:2.2.2

Next we need to install a Javascript runtime (node) to support Rails.

RUN apt-get update
RUN apt-get -y install node

Webrick boots to port 3000 by default, so we'll open that port on the container and set the PORT environment variable to 3000.

EXPOSE 3000
ENV PORT 3000

Next we set our working directory to /app for subsequent RUN and CMD instructions.

WORKDIR /app

Rails needs its gem dependencies installed, so we'll copy Gemfile and Gemfile.lock to /app and run bundler. We do this in a separate step to take advantage of Docker layer caching. Bundler will only be run on subsequent builds when Gemfile or Gemfile.lock changes.

COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
RUN bundle install

Finally, we'll copy the rest of the app to /app on the container.

COPY . /app

Now we can start the app.

CMD ["rails", "server", "-b", "0.0.0.0"]

docker-compose.yml

The docker-compose.yml file describes the processes that run in your app and how they connect to each other and the Internet. The default file generated by convox start is very simple:

main:
build .
ports:
- 5000:3000

This file defines a single process called main. It's Docker image should be built from the Dockerfile in the current directory. Port 5000 on the Docker host is forwarded to port 3000 on the container.

Next steps

Think this is cool? Install Convox and try it for yourself. You can read more about Dockerfile and docker-compose.yml in the official docs. Both files are customizable and will not be overwritten by convox start or convox deploy if they already exist.

Let your team focus on what matters.