LoginSignup
1
1

More than 1 year has passed since last update.

あんまり頭を使わないで構築するRailsAPI on Docker

Last updated at Posted at 2020-02-11

すでに多くの記事がありますが、APIモードのRailsがDocker上で動くところまで、必要最小限の情報でまとめてみます。RubyMineでの設定も最後にちょっと書いてあります。なお、詳しく知りたい方は下記公式ドキュメントを参照してください。

参考: 公式 Quickstart: Compose and Rails

2021/12/06 バージョン上げたり、動かなくなっていた部分を修正しました。

ファイル準備

アプリケーションを作成したいディレクトリで設定ファイルを作成します。

 $ touch Gemfile Gemfile.lock Dockerfile docker-compose.yml entrypoint.sh 

各ファイルの内容を埋めます。

Gemfile
source 'https://rubygems.org'
gem 'rails', '~>6'
Dockerfile
FROM ruby:2.7

RUN curl https://deb.nodesource.com/setup_12.x | bash
RUN curl https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update -qq && apt-get install -y nodejs yarn postgresql-client
RUN mkdir /app
WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
RUN bundle install
COPY . /app

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]
entrypoint.sh
#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /app/tmp/pids/server.pid

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
docker-compose.yml
version: '3'
services:
  db:
    image: postgres:11
    volumes:
      - db:/var/lib/postgresql/data:delegated
    ports:
      - 5432:5432
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
  app:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    depends_on:
      - db
    environment:
      LANG: C.UTF-8
      BUNDLE_PATH: vendor/bundle
      RAILS_ENV: development
      DB_USER: postgres
      DB_NAME: app_development
      DB_PASSWORD: password
      DB_HOST: db
    stdin_open: true
    tty: true

volumes:
  db:
    driver: local

Rubyは2.7、Railsは6系です。

作る

$ docker-compose run app rails new . --force --no-deps --database=postgresql --api 

--api を消したら通常のRailsアプリケーションが作れます。

$ docker-compose build 

したら一通りRailsが必要なファイルを作ってくれます。自動で作成された database.yml を編集します。

database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  host: <%= ENV['DB_HOST'] %>
  username: <%= ENV['DB_USER'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  pool: 5

development:
  <<: *default
  database: app_development

test:
  <<: *default
  database: app_test
$ docker-compose run app bin/setup

動作確認

$ docker-compose up

http://localhost:3000/で動いてるはずです。

補足: RubyMine使ってる方、ちょろっと設定編

インタプリタの設定

Preferences > Languages & Frameworks > Ruby SDK and Gems

で、追加 > Docker Composeをクリック > OK

読み込みが完了したら、該当のSDKをチェックし、APPLYして完了です。補完がいい感じに効くようになります。

DB設定

Database > 追加 > Data Source > PostgreSQL

  • Host: localhost
  • Port: 5432
  • User: postgres
  • Password: password

で接続ができるはずです。うまくいかない場合は、タブから適切なスキーマを選択したり、DBが起動しているか確かめたりしてください。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1