Dockerize a Sinatra Microservice

Sinatra is a lean and fun framework to build simple web apps and microservices in Ruby. Traditionally developers would deploy to a platform like Heroku or self-managed server. But what if your team is working in more than one language and need a uniform way of deploying things to the cloud? This is, of course, a great use case for Docker.
Let’s say your Sinatra app has a simple structure:
├── Gemfile
├── Gemfile.lock
├── app.rb
└── spec
├── app_spec.rb
└── spec_helper.rb

To package it in Docker, create a Dockerfile:
FROM ruby:2.5

RUN apt-get update -qq && apt-get install -y build-essential


ADD Gemfile* $APP_HOME/
RUN bundle install –without development test



CMD [“bundle", "exec", "rackup", "–host", "", "-p", "4567"]

Note the RUN bundle install –without development test line. I’m assuming that you’re building a container for deployment purposes, and we don’t need our development dependencies for that.
In case you want to run all development tasks and tests in Docker, you have two options:

Bundle everything and live with a slightly bloated container image in production
Create a and use that for development and testing before building a production image with Dockerfile.

Either way, you can build and run the container locally:
$ docker build -t sinatra-demo .
$ docker run-p 80:4567 sinatra-demo
$ curl localhost
> hello world

That’s great, now we have our app running in a standard container.
How can we deploy it? The best platform to run Docker containers today is Kubernetes. I’m working on a detailed tutorial on Continuous Integration and deployment to Kubernetes, so follow me for further updates. 😉