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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 FROM rodrigosaito/ruby:2.1.2 MAINTAINER Rodrigo Saito <firstname.lastname@example.org> ADD . /app/ WORKDIR /app/ RUN bundle install --deployment --without development test --jobs 4 ENV RACK_ENV development CMD bundle exec unicorn -E $RACK_ENV 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:
After the build finishes you can see the image with docker images command.
To run a new container with this image you execute:
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