8
8

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 3 years have passed since last update.

Docker Rails Sampleアプリ構築

Last updated at Posted at 2019-12-02

適当なRailsアプリを作成するのに脳死で作成する

前提

  • Ruby 2.6.5
  • Railsバージョン6.0.1
  • MySQL 5.7
  • Node.js 8系
  • webpacker用のコンテナは用意していない
$ mkdir rails-sample

$ rbenv local [使用するrubyバージョン]

$ git init

$ bundle init
gem 'rails'のコメントアウトを外す

$ bundle install --path vendor/bundle

$ bundle exec rails new . -B -d mysql --skip-test
-B bundle install をスキップする(お好み)
-d 利用するDBを指定(デフォルトはSQLite)
--skip-test railsのデフォルトのminitestというテストを利用しない場合は指定(お好み)

Gemfileの上書きしていいかどうかは Y でEnter

$ bundle exec rails webpacker:install

.gitignore に vendor/bundleを追記(お好み)

docker-compose.ymlとDockerfile作成

Dockerfile

FROM ruby:[使用するrubyバージョン]
ENV LANG C.UTF-8
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs

# nodejsとyarnはwebpackをインストールする際に必要
# Node.js
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - && \
apt-get install nodejs
# yarnパッケージ管理ツール
RUN apt-get update && apt-get install -y curl apt-transport-https wget && \
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt-get update && apt-get install -y yarn

WORKDIR /tmp
COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
RUN bundle install
ENV APP_HOME /rails-sample
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME
COPY . /rails-sample
docker-compose.yml
version: '3'
services:
  db:
    image: mysql:5.7
    environment:
      MYSQL_USER: root
      MYSQL_ROOT_PASSWORD: password
    volumes:
      - ./tmp/docker/mysql:/var/lib/mysql:delegated

  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/chiko
    ports:
      - "3000:3000"
    depends_on:
      - db

database.ymlを編集(お好み)

database.yml
default: &default
  adapter: mysql2
  timeout: 5000
  encoding: utf8mb4
  charset: utf8mb4
  collation: utf8mb4_general_ci
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: password
  host: db
  port: 3306

development:
  <<: *default
  database: rails-sample_development

test:
  <<: *default
  database: rails-sample_test

production:
  <<: *default
  database: <%= ENV["DATABASE_NAME"] %>
  username: <%= ENV["DATABASE_USERNAME"] %>
  password: <%= ENV["DATABASE_PASSWORD"] %>
  host: <%= ENV["DATABASE_HOST"] %>
  port: <%= ENV["DATABASE_PORT"] %>

defaultに

  • charset: utf8mb4
  • collation: utf8mb4_general_ci
  • port: 3306

を追記

productionは

  • database: <%= ENV["DATABASE_NAME"] %>
  • username: <%= ENV["DATABASE_USERNAME"] %>
  • password: <%= ENV["DATABASE_PASSWORD"] %>
  • host: <%= ENV["DATABASE_HOST"] %>
  • port: <%= ENV["DATABASE_PORT"] %>

を全部環境変数に変更

$ docker-compose build

$ docker-compose run --rm web rails db:create

ScaffoldでUserモデル作成

$ docker-compose run --rm web rails g scaffold user name:string age:integer

トップページを用意

$ bundle exec rails g controller home index
routes.rb
Rails.application.routes.draw do
  root 'home#index' # これを追記

  resources :users
end

Userページへのリンクを付与

index.html.erb
<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>

<%= link_to "user", users_path %>   <%# これを追記

マイグレーションして、コンテナを立ち上げる

$ docker-compose run --rm web rails db:migrate

$ docker-compose up -d

=> http://localhost:3000 にアクセスして確認

8
8
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
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?