Cover image for Dockerize a Sinatra Microservice

Dockerize a Sinatra Microservice

markoa profile image Marko Anastasov Updated on ・2 min read

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
├── README.md
├── app.rb
├── config.ru
└── 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:

  1. Bundle everything and live with a slightly bloated container image in production
  2. Create a Dockerfile.dev 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. For a detailed tutorial on deployment, see my other post:

Posted on Feb 18 '19 by:

markoa profile

Marko Anastasov


Let's learn together. 🎄 Semaphore CI/CD cofounder. @markoa on Twitter.


markdown guide