How to Dockerize your Ruby on Rails App

Travis Reeder
Travis on Docker
Published in
2 min readAug 30, 2016

--

Rails on Docker

Turning your apps into a Docker image is a great way to distribute and deploy your applications. I won’t get into why to use Docker, but I will show you the easiest way to Dockerize your app. This is the first of many of these that I will do for various languages and frameworks.

Following along with the Rails getting started guide, let’s create a new application. This assumes you have Ruby installed.

# Get latest rails
gem install rails
# Create an application
rails new blog
# Now start it up to ensure it's working
cd blog
bin/rails server

Now surf to http://localhost:3000/ and you’ll see the “Yay! You’re on Rails!” page.

Let’s Dockerize It!

Alright, we’ve got an app, let’s make a Docker image for it. There are a couple Gemfile changes you need to make in order for this to work properly. There are minor changes that won’t affect your normal workflow.

1. Modify Gemfile

# Add a couple gems:
gem 'json'
gem 'bigdecimal'
# Change tzinfo-data gem from this: gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby], to this:
gem 'tzinfo-data'

Now update your bundle to get the changes:

bundle update

2. Create a Dockerfile

Create a file called Dockerfile and put the following in it:

FROM iron/ruby:dev RUN apk update && apk upgrade 
RUN apk add nodejs # Yes, Rails requires node... ¯\_(ツ)_/¯
WORKDIR /app
ADD Gemfile* /app/
RUN bundle install
ADD . /app/
ENTRYPOINT ["bin/rails"]

You can use this same Dockerfile for all your Rails apps.

3. Build it:

Run the following command to build your Docker image using the Dockerfile above.

docker build -t treeder/rails-blog .

Note: treeder above is my Docker Hub username, you should change that to yours if you want to push it to Docker Hub.

4. Now Run It

Now that we have our image, let’s run it!

docker run --rm -it -p 3000:3000 -e "PORT=3000" treeder/rails-blog

And again, surf to http://localhost:3000/ to see it running.

Now What?

Ok, so you turned your app into a Docker image, now what?

Well the next thing you’ll probably want to do is push it to a Docker Registry. Docker Hub is the default so try that first.

docker push YOUR_USERNAME/rails-blog

Then go ask a friend to try your app by running this command:

docker run --rm -it -p 3000:3000 -e "PORT=3000" YOUR_USERNAME/rails-blog

If your your friend can run your app that easily, you can also deploy software to your servers just as easily. All your friend and your servers need are Docker installed… which is a beautiful thing.

--

--

Travis Reeder
Travis on Docker

Founder, CTO at GoChain - Building and breaking things