From building to deploying Ruby on Jets in docker-compose environment <Part 1>

Overview

Ruby on Jets is a Rails-like serverless framework. https://rubyonjets.com/docs/

It is assumed that you are using AWS Lambda.

It is very convenient to be able to manage Lambda settings with config/application.rb like Rails.

Since you can start puma locally like Rails with a single command, it is very easy to build an environment even though it is serverless.

From now on, I will write about building and deploying the development environment in the docker environment of Ruby On Jets.

This is Part 2.

Execution environment

docker 19.03.13

docker-compose 1.27.4

ruby 2.5.7

Ruby on Jets 2.0.6

PostgreSQL 11.6

Creating a local environment

Creating Dockerfile / docker-compose.yml / Gemfile

FROM ruby:2.5.7

RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - &&\
    apt-get update -qq &&\
    apt-get install -y build-essential nodejs postgresql-client graphviz task-japanese fonts-ipafont fonts-noto-cjk &&\
    apt-get -y install rsync &&\
    apt-get -y install zip

ENV LANG ja_JP.UTF-8

ENV TZ=Asia/Tokyo

RUN curl -sL https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip -o awscliv2.zip | bash - &&\
    unzip awscliv2.zip &&\
    ./aws/install

WORKDIR /usr/src/app
COPY Gemfile .
COPY Gemfile.lock .
RUN bundle install

CMD ["/bin/sh"]

I'm using rsync and awscli. I'm sorry if there is excess or deficiency. ..

version: '3'
volumes:
  bundle:
  postgres:
services:
  app:
    build: .
    ports:
      - 3000:3000
    volumes:
      - bundle:/usr/local/bundle
      - .:/usr/src/app:delegated
      - ~/.aws:/root/.aws
    depends_on:
      - postgres
    links:
      - postgres
    stdin_open: true
    tty: true
    env_file:
      - .env.container
    command: /bin/sh -c "bundle exec jets server --host '0.0.0.0' --port '3000'"
  postgres:
    image: postgres:11.6-alpine
    ports:
      - 5432:5432
    volumes:
      - postgres:/var/lib/postgresql/data

Since you can start puma with jets server, it runs when the container starts.

The environment variable management file is explicitly named .env.container.

The reason is that I wanted to make a clear distinction between the environment variables in the docker container and the environment variables in the application.

Also, what was the demon here was where to have the aws credential.

This time I mounted the credential file with volume.

It seems that it is not recommended. .. In CI, I think there is no problem if the encrypted key is decrypted on the pipeline.

Please note that this method is based on the assumption that the deployment user's credentials are used for local ~/.aws.

What this means is that you will be deploying using the user information that already exists in ~/.aws, so you will be giving that user the IAM policy required for deployment.

If it is not convenient, create a new deployment user and mount the credential file in the app container.

app:
  volumes:
    - <Path with credentials>:/root/.aws

For the time being, this is all you need.

source 'https://rubygems.org'
git_source(:github) do |repo| "https://github.com/#{repo}.git" end

gem 'jets'

Create an empty Gemfile.lock and .env.container.


$ touch Gemfile.lock
$ touch .env.container

Create a jets project


$ docker-compose build
$ docker-compose run --rm app jets new . --database=postgresql --mode=api

I hit the above command referring to the following.

https://rubyonjets.com/reference/jets-new/

https://rubyonjets.com/docs/new-modes/

This time we will create it in api mode.

You will be asked if you want to overwrite the Gemfile here.

Enter y. Please customize it later.


conflict  Gemfile
Overwrite /usr/src/app/Gemfile? (enter "h" for help) [Ynaqdhm]
#y press enter

Now that the Gemfile has been updated, do bundle install again.


$ docker-compose run --rm app bundle install

db creation

Describe the environment variables in .env.container. This time, we will describe the DB information.

DB_NAME=hoge
DB_HOST=fuga
DB_USER=hogehoge
DB_PASS=fugafuga
POSTGRES_HOST_AUTH_METHOD=trust

Execute db: create.


$ docker-compose run --rm app bundle exec jets db:create

Create a template with scaffold for the time being.

$ docker-compose run --rm app bundle exec jets g scaffold post title:string
This will be rejected in the future unless you explicitly pass the options `check_default_type: false` or call `allow_incompatible_default_type!` in your code
You can silence deprecations warning by setting the environment variable THOR_SILENCE_DEPRECATION.
      invoke  active_record
      create    db/migrate/20200927064200_create_posts.rb
      create    app/models/post.rb
      invoke  resource_route
       route    resources :posts
Deprecation warning: Expected string default value for '--test-framework'; got false (boolean).
This will be rejected in the future unless you explicitly pass the options `check_default_type: false` or call `allow_incompatible_default_type!` in your code
You can silence deprecations warning by setting the environment variable THOR_SILENCE_DEPRECATION.
      invoke  scaffold_controller
      create    app/controllers/posts_controller.rb

Execute db: migrate.

$ docker-compose run --rm app bundle exec jets db:migrate

Try launching the container

$ docker-compose up

This will run the jets server and bring up the local server.

If you can check the browser at http://0.0.0.0:3000, it's OK.

image.png

Summary

This time, we were able to confirm that the application server started up locally, creating an environment where we could develop.

In the second part, I would like to write about how to create a stag, prod environment, etc. from the IAM policy for deployment.

Recommended Posts

From building to deploying Ruby on Jets in docker-compose environment <Part 2>
From building to deploying Ruby on Jets in docker-compose environment <Part 1>
From 0 to Ruby on Rails environment construction [macOS] (From Homebrew installation to Rails installation)
Difficulties in building a Ruby on Rails environment (Windows 10) (SQLite3)
Ruby on Rails --From environment construction to simple application development on WSL2
Notes on building Kotlin development environment and migrating from Java to Kotlin
[Ruby] Building a Ruby development environment on Ubuntu
Change from SQLite3 to PostgreSQL in a new Ruby on Rails project
Environment construction of Ruby on Rails from 0 [Cloud9] (From Ruby version change to Rails installation)
Building a Ruby environment for classes on Mac
Building OpenPose (Pytorch_Realtime_Multi-Person_Pose_Estimation) environment on Docker: training part
[Ruby on Rails] How to install Bootstrap in Rails
Build a development environment to create Ruby on Jets + React apps with Docker
From building an AWS cloud environment to deploying a Spring Boot app (for beginners)
[Ruby on Rails] How to write enum in Japanese
Deploy to Ruby on Rails Elastic beanstalk (Environment construction)
[Ruby On Rails] How to reset DB in Heroku
The road from Git clone to building the environment and participating in the project (all in full)
From Java to Ruby !!
[Ruby on Rails] From MySQL construction to database change
From Ruby on Rails error message display to Japanese localization
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
How to display a graph in Ruby on Rails (LazyHighChart)
Apply CSS to a specific View in Ruby on Rails
[Docker context] ~ How to access docker in remote environment from VScode ~
Update Ruby in Unicorn environment
Ruby on Jets Verification (WSL)
I can't log in to MySQL from Django when using docker-compose
How to get and add data from Firebase Firestore in Ruby
Things to remember and concepts in the Ruby on Rails tutorial
Steps to build a Ruby on Rails development environment with Vagrant
How to iterate infinitely in Ruby
Try to implement Yubaba in Ruby
How to use Ruby on Rails
Ruby on Rails Japanese-English support i18n
How to install Bootstrap in Ruby
Ruby on Rails 6.0 environment construction memo
Upgrade from MYSQL5.7 to 8.0 on CentOS 6.7
Introduction to Ruby (from other languages)
Notes on migrating from CircleCI 1.0 to 2.0
How to build a Ruby on Rails environment using Docker (for Docker beginners)
How to develop from VScode in a remote destination environment or a remote destination container environment
Minimal steps to set up a Ruby environment with rbenv on Ubuntu 20.04
What to do when Blocked Host: "host name" appears in Ruby on Rails
How to implement gem "summer note" in wysiwyg editor in Ruby on Rails
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
[Error] Switch environment construction to use Ruby on Rails oss (open source)
How to solve the local environment construction of Ruby on Rails (MAC)!
A story about converting character codes from UTF-8 to Shift-jis in Ruby
How to debug the processing in the Ruby on Rails model only on the console
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Ruby on Rails ✕ Docker ✕ MySQL Introducing Docker and docker-compose to apps under development