高木のブログ

Docker ComposeでSinatraの開発環境を構築する

2020/11/24

SinatraでちょっとしたAPIを作った時の構築メモ。
DBなど使わないシンプル構成。

手順

アプリのディレクトリを作って移動

$ mkdir docker-sinatra
$ cd docker-sinatra

必要なファイルを作成

  • Dockerfile
  • docker-compose.yml
  • Gemfile
  • app.rb
Dockerfile
FROM ruby:2.7.2

ADD . /app
WORKDIR /app

RUN bundle install -j4
docker-compose.yml
version: '3'
services:
  web:
    build: .
    command: bundle exec ruby app.rb -o 0.0.0.0
    ports:
      - 4567:4567
    volumes:
      - .:/app
Gemfile
source 'https://rubygems.org'

ruby '2.7.2'

gem 'sinatra'
app.rb
require 'sinatra'

get '/' do
  'Hello, World!'
end

ビルドと起動

$ docker-compose up --build
Creating network "docker-sinatra_default" with the default driver
Building web
Step 1/4 : FROM ruby:2.7.2
 ---> 09fcf72ff321
Step 2/4 : ADD . /app
 ---> b71bf29331d0
Step 3/4 : WORKDIR /app
 ---> Running in a54f49247e95
Removing intermediate container a54f49247e95
 ---> d4c279f6608b
Step 4/4 : RUN bundle install -j4
 ---> Running in 02cbb1b78c6c
Fetching gem metadata from https://rubygems.org/....
Resolving dependencies...
Using bundler 2.1.4
Fetching ruby2_keywords 0.0.2
Fetching rack 2.2.3
Fetching tilt 2.0.10
Installing ruby2_keywords 0.0.2
Installing tilt 2.0.10
Fetching mustermann 1.1.1
Installing rack 2.2.3
Installing mustermann 1.1.1
Fetching rack-protection 2.1.0
Installing rack-protection 2.1.0
Fetching sinatra 2.1.0
Installing sinatra 2.1.0
Bundle complete! 1 Gemfile dependency, 7 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
Removing intermediate container 02cbb1b78c6c
 ---> efc5c16e07ff

Successfully built efc5c16e07ff
Successfully tagged docker-sinatra_web:latest
Creating docker-sinatra_web_1 ... done
Attaching to docker-sinatra_web_1
web_1  | [2020-11-24 14:12:29] INFO  WEBrick 1.6.0
web_1  | [2020-11-24 14:12:29] INFO  ruby 2.7.2 (2020-10-01) [x86_64-linux]
web_1  | == Sinatra (v2.1.0) has taken the stage on 4567 for development with backup from WEBrick
web_1  | [2020-11-24 14:12:29] INFO  WEBrick::HTTPServer#start: pid=1 port=4567

動作確認

$ curl localhost:4567
Hello, World!

参考

Dockerを使ってsinatraの開発環境を作る - Qiita


SNS でシェアする


ytkg

Written by ytkg, Twitter, GitHub