- ※作成中
$ sh init.sh {project_name}
# ex) sh init.sh portfoliodocker-compose build の実行
docker-compose up -dの実行docker ps -aの実行 で STATUSがUpになっていることを確認する
# 実行例
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2cd4872e0928 rails_sample_web "irb" 16 minutes ago Up 16 minutes 0.0.0.0:3000->3000/tcp rails_web
d3922d633a50 mysql:8.0 "docker-entrypoint.s…" 16 minutes ago Up 16 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp rails_mysqldocker-compose logsで、
webサーバーコンテナ(以下、Webコンテナもしくは、rails_web),
DBサーバーコンテナ(以下、DBコンテナもしくは、rails_mysql)が
正常に起動できているか等の確認ができる
docker exec -it rails_web /bin/bash の実行で、
rails_webというコンテナ名のサーバーに入る
ここでは、Railsガイト/Railsをはじめよう にしたがって作るものとする。
以下は、Railsガイト/Railsをはじめよう の 3.1 Railsのインストール にしたがった一例
root@2cd4872e0928:/usr/src# ruby -v
=> ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux]
- 2020.2現在最新の公式Rubyイメージ(ruby:2.7.0)をDockerfileで定義している
root@2cd4872e0928:/usr/src# sqlite3 --version
=> bash: sqlite3: command not found
- DockerのRubyイメージを使っているため、当然インストールされていない
- 今後SQLiteを使うこともないので、インストールの必要はない
root@2cd4872e0928:/usr/src# gem install rails
=> Successfully installed rails-6.0.2.1
1 gem installed
- オプションなしだと、最新のRailsがインストールされる(2020.2現在、rails-6.0.2.1)
- バージョンを保つ目的でDockefile内で以下を定義している
- 事前にその定義の実行によって作られた環境であるため、改めて
gem install railsをここで実行しなくてよい
# Dockerfile
(抜粋)
RUN gem install rails --version="~>6.0.0"
- rails --versionでRailsのバージョンは調べられる
root@a19247e0a147:/usr/src# rails --version
=> Rails 6.0.2.1
root@2cd4872e0928:/usr/src# rails new {project_name}
ex)
root@2cd4872e0928:/usr/src# rail new blog とか
root@2cd4872e0928:/usr/src# rails new portfolio など
- ただし、初期状態からMySQLを使っていくので、オプションをつける
- rails newコマンドを以下のオプションつきで実行
root@2cd4872e0928:/usr/src# rails new {project_name} --database=mysql
- --database=mysql:
- 取り扱うDBをMySQLにする
- オプションなしの場合、SQLiteを使うものとしてプロジェクトが作られる
- 他オプションについては、参考 Railsドキュメント/アプリケーションの作成 を参照
- VSCode等のエディタ等で見ると、Webコンテナ内で
rails newして作成したファイルが、{project_name}のフォルダ名で外部にも作成されている - {project_name}/config/database.ymlをVSCode等のエディタ等で開く
- database.ymlのpasswordとhostを以下のように修正
# before(rails newによる生成直後)
password:
host: localhost
# after
password: root
host: db- なぜ修正するのか?
- password : 後の手順で、DBを作成するがその時のrootユーザーのパスワードが必要
- host : Dockerで作ったMySQLとのDBコンテナとWebコンテナが通信する必要があるため、docker-compose.ymlのservice名「db」で指定する
rails db:createでDBが作成される
root@2cd4872e0928:/usr/src/{project_name}# rails db:create
=> /usr/local/bundle/gems/actionpack-6.0.2.1/lib/action_dispatch/middleware/stack.rb:37: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/usr/local/bundle/gems/actionpack-6.0.2.1/lib/action_dispatch/middleware/static.rb:110: warning: The called method `initialize' is defined here
Created database '{project_name}_development'
Created database '{project_name}_test'
- DBコンテナに入って、DBが作成されているか確認する
- まず、Webコンテナから出る
root@2cd4872e0928:/usr/src# exit
- DBコンテナに入る
$ docker exec -it rails_mysql /bin/bash
- MySQLのログインコマンドでMySQLとの対話シェルを起動
root@58a6d02a3467:/# mysql -u root -p
=> Enter password:
とパスワードを求められるので`-u`で指定したrootユーザーのパスワードを入力する
=> Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.19 MySQL Community Server - GPL
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
- データベースの一覧を見る
mysql>show databases;
=> # rails new の{project_name}を portfolio にしたので
# portfolio_development と portfolio_test ができている
+-----------------------+
| Database |
+-----------------------+
| information_schema |
| mysql |
| performance_schema |
| portfolio_development |
| portfolio_test |
| sys |
+-----------------------+
6 rows in set (0.01 sec)
- MySQLとの対話シェルをやめる
mysql>exit;
- DBコンテナから出る
root@58a6d02a3467:/# exit
- (Webコンテナに入る)
$ docker exec -it rails_web /bin/bash
rails newで作ったプロジェクトのフォルダ内(以下、「ディレクトリ」または「プロジェクト配下」)に移動
# Webコンテナにいる状態で
root@a19247e0a147:/usr/src# cd {project_name}
ex)
root@a19247e0a147:/usr/src# cd portfolio
rails serverにより、Webコンテナ内でアプリサーバーを起動
root@a19247e0a147:/usr/src/portfolio# rails server(rails sでもよい)
- サーバーの起動を待ち、ブラウザから初期画面にアクセスする
- Listening on tcp:0.0.0.0:3000が表示されるまで待つ
http://localhost:3000でブラウザからアクセスする- Yay! You’re on Rails! を確認する
- Macの場合「control + c」でアプリサーバーを止める
=> Booting Puma
=> Rails 6.0.2.1 application starting in development
=> Run `rails server --help` for more startup options
/usr/local/bundle/gems/actionpack-6.0.2.1/lib/action_dispatch/middleware/stack.rb:37: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/usr/local/bundle/gems/actionpack-6.0.2.1/lib/action_dispatch/middleware/static.rb:110: warning: The called method `initialize' is defined here
Puma starting in single mode...
* Version 4.3.1 (ruby 2.7.0-p0), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop