I’ve started a little blog-like project few days ago. And I decided use Hanami web framework for Ruby instead of Rails. It seems good designed and requires much less resources. I’ve been playing with Hanami months ago, but nothing serious or production-ready hadn’t done.

To create a new Hanami project, we need to install the Hanami gem from Rubygems. Then we can use the new hanami executable to generate a new project:

$ gem install hanami
$ hanami new bookshelf --database=postgres

This creates a new directory structure, init git repository and cofigures the new app to use PostgreSQL.

Create Dockerfile in the root of the created app.

FROM ruby:2.6.5-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    libpq-dev \
    curl \
 && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

WORKDIR /usr/src/app/

COPY Gemfile* /usr/src/app/

RUN gem install bundler -v 1.17.2
RUN bundle install

COPY . /usr/src/app/

And then create docker-compose.yml in the app root:

version: '3.4'
services:
  db:
    image: postgres
    ports:
      - "5432:5432"
    volumes:
      - db_data:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: app_development
      POSTGRES_USER: appuser
      POSTGRES_PASSWORD: appuser_password
  web:
    build:
      context: .
    command: bash -c "bundle exec hanami server --port 3000 --host '0.0.0.0'"
    volumes:
      - .:/user/src/app
    ports:
      - "3000:3000"
    depends_on:
      - db
    environment:
      DATABASE_URL: "postgresql://appuser:[email protected]/app_development"

volumes:
  db_data:
    driver: local

Here we declared to services: db and web. Also we’ve created db_data volume to save our data. We’ve declared DATABASE_URL already, so now we can remove it from .env.development.

And that’s it! Now we can run our container.

$ docker-compose up -d --no-deps --build

Now you’ll be able open http://localhost:3000 to see Hanami’s default start page or an error of some kind :)

To shut the container down:

$ docker-compose down

Conclusion

It is not so difficult to start with Docker. Try it if you haven’t yet! I’ll write more about Hanami soon.