14
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ruby on Rails 6.0.0.beta1をDockerで動かそう!

Last updated at Posted at 2019-01-23

概要

Ruby on Rails 6.0.0.beta1 がリリースされました!
早速使ってみたいのでDockerで開発環境を作ってみます。
Dockerの公式手順は Rails 5 になってます。
Rails 6 にすると上手くいかないのでちょっと修正しつつ作ってみます。

基本的に以下の手順で行います。
Quickstart: Compose and Rails | Docker Documentation

実行環境

完成品

  • OS Debian 9.6
  • Ruby 2.6
  • Ruby on Rails 6.0.0.beta1
  • PostgreSQL 11.1

手順

1. アプリケーションを作るディレクトリ作成

$ mkdir myapp
$ cd myapp

2. Dockerfile作成

$ vim Dockerfile

以下内容

FROM ruby:2.6
# nodeのバージョンが6以上であればなんでも良いと思う
RUN curl -SL https://deb.nodesource.com/setup_11.x | bash
# 最新のyarnを取得
RUN curl -sS 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 postgresql-client yarn

RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

# 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"]

3. Gemfile作成

$ vim Gemfile

以下内容

source 'https://rubygems.org'
gem 'rails', '~> 6.0.0.beta1'

4. Gemfile.lock作成

$ touch Gemfile.lock

5. entrypoint.sh作成

$ vim entrypoint.sh

以下内容

#!/bin/bash
set -e

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

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

6. docker-compose.yml作成

$ vim docker-compose.yml

以下内容

version: '3'
services:
  db:
    image: postgres:11.1
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db

6. rails new する!

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

最後に以下が出力されたらおけです。

Webpacker successfully installed 🎉 🍰

7. root権限を変更

$ sudo chown -R $USER:$USER .

8. イメージを作る!

$ docker-compose build

9. DB設定

$ vim config/database.yml

以下内容

default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password:
  pool: 5

development:
  <<: *default
  database: myapp_development

test:
  <<: *default
  database: myapp_test

10. DB作成

$ docker-compose run web rake db:create

11. 起動

$ docker-compose up

12. みてみる

以下のURLに飛んで確認してみましょう!
http://0.0.0.0:3000

少し画像が変わりましたね。
スクリーンショット 2019-01-24 0.23.18.png

やったぜ

14
10
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
14
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?