DEV Community

Cover image for Dockerize a Sinatra Microservice
Marko Anastasov
Marko Anastasov

Posted on • Edited on

6 3

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
├── README.md
├── app.rb
├── config.ru
└── spec
    ├── app_spec.rb
    └── spec_helper.rb
Enter fullscreen mode Exit fullscreen mode

To package it in Docker, create a Dockerfile:

FROM ruby:2.5

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

ENV APP_HOME /app
RUN mkdir $APP_HOME
WORKDIR $APP_HOME

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

ADD . $APP_HOME

EXPOSE 4567

CMD ["bundle", "exec", "rackup", "--host", "0.0.0.0", "-p", "4567"]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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:

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay