Deploying Ruby apps with Docker

20 Sep 2015

Docker is a very fantastic tool for packing, shipping and running any application as a lightweight container. I will show a way to use Docker to deploy Ruby to an EC2 instance.

First we need a Ruby application, I have this simple sinatra application that I created to test deployment with Docker.

At the root of the project we have a Dockerfile:

 1 FROM rodrigosaito/ruby:2.1.2
 3 MAINTAINER Rodrigo Saito <>
 5 ADD . /app/
 7 WORKDIR /app/
 9 RUN bundle install --deployment --without development test --jobs 4
11 ENV RACK_ENV development
13 CMD bundle exec unicorn -E $RACK_ENV
15 EXPOSE 8080

Explaining how a Dockerfile works is beyond the scope of this post, but basically this Dockerfile will be built starting from another image that I created with ruby 2.1.2 installed, copy the app files to the container, run bundle install, set the command to run when we start the container and exposes port 8080.

To build this image you have to run the following command:

docker build -t rodrigosaito/rack_unicorn_docker .

After the build finishes you can see the image with docker images command.

To run a new container with this image you execute:

# creates a new container from image and starts it
docker run -p 8080:8080 -e RACK_ENV=production -d rodrigosaito/rack_unicorn_docker

# check if your docker container is running
docker ps

# check if your application is running
curl localhost:8080/hello

Now that we have successfully built a docker image with our ruby app we can deploy it to an AWS EC2 instance. Note: You can be charged by using an EC2 instance, be aware of that