目標
- Ruby on Rails6 のアプリケーションをDockerで新規にアプリを立ち上げたい
- DBはMySQL
前提知識
- Docker on mac
- Rails tutorial完了などRuby on rails に関する基礎知識
追記
- 2020/09/14 buildのコマンドを追記
1.作業ディレクトを作成し、移動する
MacBook-Air ~ % mkdir アプリ名
MacBook-Air ~ % cd アプリ名
MacBook-Air アプリ名 %
2.Dockerfileを定義する
/Dockerfile
FROM ruby:2.6.3
RUN apt-get update -qq && apt-get install -y nodejs
# yarnパッケージ管理ツールをインストール
# https://classic.yarnpkg.com/en/docs/install/#debian-stable
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 && apt-get install yarn
WORKDIR /アプリ名
COPY Gemfile /アプリ名/Gemfile
COPY Gemfile.lock /アプリ名/Gemfile.lock
RUN bundle install
COPY . /アプリ名
# 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を作成する
/Gemfile
source 'https://rubygems.org'
gem 'rails', '~>6'
4.空のGemfile.lockを生成する
MacBook-Air アプリ名 % touch Gemfile.lock
5.entrypoint.shを作成する
2つ目のところ、自身のアプリ名を入れることに気をつける
entrypoint.sh:
#!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails.
rm -f /アプリ名/tmp/pids/server.pid
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
6.docker-compose.ymlを作成する
webのvolumesのところを自分のアプリ名を入れる
docker-compose.yml
version: "3"
services:
db:
image: mysql:8.0
command: mysqld --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: root
volumes:
- ./tmp/db:/var/lib/mysql
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
environment:
MYSQL_HOST: db
volumes:
- .:/アプリ名
ports:
- "3000:3000"
depends_on:
- db
7.Rails newをする & コンテナをbuildする
ここではオプションとして、オプションでリンクしたサービスを起動しない設定、DBをmysqlにする設定を追加
MacBook-Air アプリ名 % docker-compose run web rails new . --force --no-deps --database=mysql
~
Starting アプリ名_db_1 ... done
Building web
~
Successfully built 36d2fef9a8a6
Successfully tagged アプリ名_web:latest
~
MacBook-Air アプリ名 % docker-compose build
8. DBと接続する
passwordとhostに環境変数を設定する
/config/database.yml
~
default: &default
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: <%= ENV.fetch("MYSQL_ROOT_PASSWORD", "root") %>
host: <%= ENV.fetch("MYSQL_HOST", "db") %>
~
9.コンテナを起動する
MacBook-Air アプリ名 % docker-compose up
~
web_1 | => Booting Puma
web_1 | => Rails 6.0.3.2 application starting in development
web_1 | => Run `rails server --help` for more startup options
web_1 | Puma starting in single mode...
web_1 | * Version 3.12.6 (ruby 2.6.3-p62), codename: Llamas in Pajamas
web_1 | * Min threads: 5, max threads: 5
web_1 | * Environment: development
web_1 | * Listening on tcp://0.0.0.0:3000
web_1 | Use Ctrl-C to stop
この表示がでたらブラウザで http://localhost:3000/ にアクセスする
Yay! You’re on Rails! と表示がでたらコンテナ起動成功
エラー: No such file or directory @ rb_sysopen - /アプリ名/config/webpacker.yml Errno::ENOENT)発生時
MacBook-Air アプリ名 % docker-compose run web rails webpacker:install
~
Webpacker successfully installed 🎉 🍰
参考文献
https://docs.docker.com/compose/rails/
https://railsdoc.com/rails