見出し画像

Ruby on Rails6習得編 Ruby on Rails 6 実践ガイド Chapter2 開発環境の構築


こちらの書籍について学んだことです。

Chapter2 開発環境の構築

Dockerを使う

2.1 Dockerを利用したRails開発

Dockerの何が優れているのか

2.1.1 仮想化環境を使う理由

開発用マシンと実運用マシンの環境は異なる。

OSの種類やバージョン、OS上で動くソフトウェアやライブラリの種類やバージョンetc

railsは一応windowsでもmacでもlinuxでも動作するように設計されてる。一応。

gemはそうでもない。動作が微妙に違ったり不具合が出たりする。

単純な解決方法がある。

開発用マシンと実運用マシンを一致させてしまえばいい。

そのために使うのが仮想環境

windows,mac,ubuntuなどのデスクトップOSの上に仮想環境を構築
そこにLinuxベースのServerOSをインストールする。


railsアプリケーションは仮想化環境で動作させる。

そして、デスクトップOS上のwebブラウザからrailsアプリケーションにアクセスして動作を確認する。

railsアプリケーションのソースコードは共有フォルダ機能でデスクトップOS上で開いて編集する。

使い慣れたテキストエディタやIDEを使い続けながら実運用マシンと同等の環境下でrailsアプリケーションの開発が行えることになる。

2.1.2 Dockerとは

仮想化環境を提供する。
設定の容易さや速さなどの理由により、rails開発者の間でとても人気がある。

個々の仮想化環境をコンテナと呼ぶ。

コンテナの内容はDockerfileと呼ばれるテキストファイルで記述される。
このファイルがあればさまざまなOS上でコンテナを復元できる。

2.1.3 Docker-composeとは

Dockerを用いてrailsアプリケーションを開発したり、プロダクション環境で動かすとき、railsアプリケーションとデータベースサーバーを別々のコンテナとして構築するのが一般的。

Docker Compose はこれらの複数のコンテナをまとめて起動・停止するためのツール

docker-compose up というコマンドひとつ実行するだけで、Webアプリケーションを構成するすべてのコンテナ群が動き出す。

2.2 DockerおよびDocker Composeのインストール

インストール方法

2.2.1 macOSの場合

Docker Hubのアカウントを取得する。

https://hub.docker.com/signup

アカウント名、メールアドレス、パスワードを入力

そのあと、プロフィールを入力

Firstnameは名前
Lastnameは名字
Countryは2つある・・・どっちもJapanにしといた

Tailor your experience
Tailor 調整
experience 経験
これは経験により調整する項目っぽい

Developer(開発者)
Just getting started learning about Docker(Dockerの勉強したて)
を選択

入力し終わったのでContinueをポチ

mailを確認してと言われるのでyahooメールへ

verify email addresをポチ

ログインさせられる

とりあえずお気に入り登録しておく
https://hub.docker.com

ブラウザで次のアドレスを開く

https://hub.docker.com/editions/community/docker-ce-desktop-mac

画面右のGetDockerをポチしてインストーラをダウンロード

708Mくらいあって結構でかめ

ダウンロードされたDocker.dmgをダブルクリックしてインストーラを開き、くじらをApplicationsにドロップすればインストール完了

とりあえずログインしてみた

最後にバージョンを確認してみる

docker --version
docker-compose --version

[教科書の環境]
Docker version 19.03.2, build 6a30dfc
docker-compose version 1.24.0, build 0aa59064

[自分の環境]
Docker version 19.03.5, build 633a0ea
docker-compose version 1.25.4, build 8d51620a

2.2.2 windowsの場合

省略

2.2.3 Ubuntuの場合

省略

2.3 Rails開発環境の構築

2.3.1 Gitのインストール

gitをインストールする
https://git-scm.com/download/mac

でも既に2.22.1なら入ってるんだよな・・・
この時点での最新は2.23.0らしい。

terminalでコマンドでgitを最新化する方法ないかな?
まあいっか!このままでいいや!

2.3.2 Rails開発用コンテナ群の構築

git cloneで自分のPCにrails開発用コンテナ群を持ってくる
cdでそのコンテナ群の中に入る
lsでどんなファイルがあるか確認する
setup.shでシェルスクリプトを実行
ただし、setup.shと打ってもシェルスクリプトは実行されないので、./setup.shと打つ必要がある

下記コマンドを実行する
git clone https://github.com/oiax/rails6-compose.git
ls
cd rails6-compose
ls
setup.sh
./setup.sh

----------------------------------------
localpc ~ % git clone https://github.com/oiax/rails6-compose.git
Cloning into 'rails6-compose'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 98 (delta 0), reused 2 (delta 0), pack-reused 94
Unpacking objects: 100% (98/98), done.
localpc ~ % ls
Desktop		Downloads	Library		Music		Public
Documents	Dropbox		Movies		Pictures	rails6-compose
localpc ~ % cd rails6-comopse
cd: no such file or directory: rails6-comopse
localpc ~ % cd rails6-comopose
cd: no such file or directory: rails6-comopose
localpc ~ % cd rails6-compose
localpc rails6-compose % ls
Dockerfile		README.win.md		docker-compose.yml
RAILS.md		apps			init
README.md		docker-compose.win.yml	setup.sh
localpc rails6-compose % setup.sh
zsh: command not found: setup.sh
localpc rails6-compose % ./setup.sh
latest: Pulling from oiax/rails6-deps
e7c96db7181b: Pull complete 
622c94c90cb1: Pull complete 
dbd6b8b21943: Pull complete 
d76322ad1296: Pull complete 
b802da761a42: Pull complete 
67290cf17951: Pull complete 
fb12b1feddeb: Pull complete 
8e1bc084cdce: Pull complete 
Digest: sha256:92588edfb0863920d85496152bc8804a8ac641c8c3a94abd633bb62c748d6ea1
Status: Downloaded newer image for oiax/rails6-deps:latest
docker.io/oiax/rails6-deps:latest
Building web
Step 1/17 : FROM oiax/rails6-deps:latest
---> 68b3321c4f66
Step 2/17 : ARG UID=1000
---> Running in ab48ac940da7
Removing intermediate container ab48ac940da7
---> f2192b8cd8b9
Step 3/17 : ARG GID=1000
---> Running in 62efecc8b8e1
Removing intermediate container 62efecc8b8e1
---> 0ddfe96351e9
Step 4/17 : RUN mkdir /var/mail
---> Running in ab6491ac5024
Removing intermediate container ab6491ac5024
---> 6e4897c31d3c
Step 5/17 : RUN groupadd -g $GID devel
---> Running in 71aab123c892
Removing intermediate container 71aab123c892
---> c1b4f2f8845d
Step 6/17 : RUN useradd -u $UID -g devel -m devel
---> Running in 288cab90a111
Removing intermediate container 288cab90a111
---> 9b00518b3b7d
Step 7/17 : RUN echo "devel ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
---> Running in b0613ab89141
Removing intermediate container b0613ab89141
---> 20d08a52f4a0
Step 8/17 : WORKDIR /tmp
---> Running in 3d2b0dc0482d
Removing intermediate container 3d2b0dc0482d
---> 600624278c62
Step 9/17 : COPY init/Gemfile /tmp/Gemfile
---> af4141e5429a
Step 10/17 : COPY init/Gemfile.lock /tmp/Gemfile.lock
---> 0202e2eb0425
Step 11/17 : RUN bundle install
---> Running in b8761be3b419
Fetching gem metadata from https://rubygems.org/.............
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Fetching rake 13.0.1
Installing rake 13.0.1
Fetching concurrent-ruby 1.1.6
Installing concurrent-ruby 1.1.6
Fetching i18n 1.8.2
Installing i18n 1.8.2
Fetching minitest 5.14.0
Installing minitest 5.14.0
Fetching thread_safe 0.3.6
Installing thread_safe 0.3.6
Fetching tzinfo 1.2.6
Installing tzinfo 1.2.6
Fetching zeitwerk 2.2.2
Installing zeitwerk 2.2.2
Fetching activesupport 6.0.2.1
Installing activesupport 6.0.2.1
Fetching builder 3.2.4
Installing builder 3.2.4
Fetching erubi 1.9.0
Installing erubi 1.9.0
Fetching mini_portile2 2.4.0
Installing mini_portile2 2.4.0
Fetching nokogiri 1.10.8
Installing nokogiri 1.10.8 with native extensions
Fetching rails-dom-testing 2.0.3
Installing rails-dom-testing 2.0.3
Fetching crass 1.0.6
Installing crass 1.0.6
Fetching loofah 2.4.0
Installing loofah 2.4.0
Fetching rails-html-sanitizer 1.3.0
Installing rails-html-sanitizer 1.3.0
Fetching actionview 6.0.2.1
Installing actionview 6.0.2.1
Fetching rack 2.2.2
Installing rack 2.2.2
Fetching rack-test 1.1.0
Installing rack-test 1.1.0
Fetching actionpack 6.0.2.1
Installing actionpack 6.0.2.1
Fetching nio4r 2.5.2
Installing nio4r 2.5.2 with native extensions
Fetching websocket-extensions 0.1.4
Installing websocket-extensions 0.1.4
Fetching websocket-driver 0.7.1
Installing websocket-driver 0.7.1 with native extensions
Fetching actioncable 6.0.2.1
Installing actioncable 6.0.2.1
Fetching globalid 0.4.2
Installing globalid 0.4.2
Fetching activejob 6.0.2.1
Installing activejob 6.0.2.1
Fetching activemodel 6.0.2.1
Installing activemodel 6.0.2.1
Fetching activerecord 6.0.2.1
Installing activerecord 6.0.2.1
Fetching mimemagic 0.3.4
Installing mimemagic 0.3.4
Fetching marcel 0.3.3
Installing marcel 0.3.3
Fetching activestorage 6.0.2.1
Installing activestorage 6.0.2.1
Fetching mini_mime 1.0.2
Installing mini_mime 1.0.2
Fetching mail 2.7.1
Installing mail 2.7.1
Fetching actionmailbox 6.0.2.1
Installing actionmailbox 6.0.2.1
Fetching actionmailer 6.0.2.1
Installing actionmailer 6.0.2.1
Fetching actiontext 6.0.2.1
Installing actiontext 6.0.2.1
Using bundler 1.17.2
Fetching method_source 0.9.2
Installing method_source 0.9.2
Fetching thor 1.0.1
Installing thor 1.0.1
Fetching railties 6.0.2.1
Installing railties 6.0.2.1
Fetching sprockets 4.0.0
Installing sprockets 4.0.0
Fetching sprockets-rails 3.2.1
Installing sprockets-rails 3.2.1
Fetching rails 6.0.2.1
Installing rails 6.0.2.1
Bundle complete! 1 Gemfile dependency, 43 gems now installed.
Bundled gems are installed into `/usr/local/bundle`
Post-install message from i18n:
HEADS UP! i18n 1.1 changed fallbacks to exclude default locale.
But that may break your application.
If you are upgrading your Rails application from an older version of Rails:
Please check your Rails app for 'config.i18n.fallbacks = true'.
If you're using I18n (>= 1.1.0) and Rails (< 5.2.2), this should be
'config.i18n.fallbacks = [I18n.default_locale]'.
If not, fallbacks will be broken in your app by I18n 1.1.x.
If you are starting a NEW Rails application, you can ignore this notice.
For more info see:
https://github.com/svenfuchs/i18n/releases/tag/v1.1.0
Removing intermediate container b8761be3b419
---> b4a1d904b4c2
Step 12/17 : COPY ./apps /apps
---> af2e1be2e847
Step 13/17 : RUN apk add --no-cache openssl
---> Running in c62265ac9c1a
fetch http://dl-cdn.alpinelinux.org/alpine/v3.9/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.9/community/x86_64/APKINDEX.tar.gz
OK: 307 MiB in 93 packages
Removing intermediate container c62265ac9c1a
---> a6930797616e
Step 14/17 : USER devel
---> Running in 4f70d6f11319
Removing intermediate container 4f70d6f11319
---> eecdc82ccb31
Step 15/17 : RUN openssl rand -hex 64 > /home/devel/.secret_key_base
---> Running in 0fbab8fa6648
Removing intermediate container 0fbab8fa6648
---> 6ae97af87b44
Step 16/17 : RUN echo $'export SECRET_KEY_BASE=$(cat /home/devel/.secret_key_base)'   >> /home/devel/.bashrc
---> Running in 04b204186bdb
Removing intermediate container 04b204186bdb
---> 676454fa7891
Step 17/17 : WORKDIR /apps
---> Running in 95df1eaec922
Removing intermediate container 95df1eaec922
---> 43ebf344740b
Successfully built 43ebf344740b
Successfully tagged rails6-compose_web:latest
localpc rails6-compose % 
----------------------------------------

ちなみにsetup.shの中は以下のようになってる。

----------------------------------------
#!/bin/bash
set -eu
docker pull oiax/rails6-deps:latest
BUILD_CMD="docker-compose build --no-cache"
case "$OSTYPE" in
 darwin*)
   $BUILD_CMD web
   ;;
 linux*)
   $BUILD_CMD --build-arg UID=$(id -u) --build-arg GID=$(id -g) web
   ;;
 *)
   echo "Unknown OS Type: $OSTYPE"
   ;;
esac
----------------------------------------

意味はわからん

2.3.3 コンテナ群の起動と停止

Baukis2の開発に2つのコンテナを使用する。
railsアプリ動作用(web)
PostgreSQL用(db)

全てのコンテナ群を起動
docker-compose up -d

----------------------------------------
localpc rails6-compose % docker-compose up -d
Creating network "rails6-compose_default" with the default driver
Pulling db (postgres:11.2-alpine)...
11.2-alpine: Pulling from library/postgres
bdf0201b3a05: Downloading [>                                                  ]  27.98kB/2.757MBlling fs layer
365f27dc05d7: Downloading [==================================================>] bdf0201b3a05: Downloading [===================>                               ] bdf0201b3a05: Downloading [=======================================>           ] bdf0201b3a05: Downloading [==================================================>] bdf0201b3a05: Extracting [>                                                  ]  bdf0201b3a05: Extracting [==================================================>]  bdf0201b3a05: Pull complete
365f27dc05d7: Extracting [==================================================>]  365f27dc05d7: Extracting [==================================================>]  365f27dc05d7: Pull complete
bf541d40dfbc: Pull complete
823ce70c3252: Pull complete
a92a31ecd32a: Pull complete
83cc8c6d8282: Pull complete
7995b9edc9bf: Pull complete
7616119153d9: Pull complete
b3f69561e369: Pull complete
Digest: sha256:ac1494b4cd5c0c6a85209e645fbb84ea186b55fa8c3678ef66dd9e4edf2b0195
Status: Downloaded newer image for postgres:11.2-alpine
Creating rails6-compose_db_1 ... done
Creating rails6-compose_web_1 ... done
localpc rails6-compose % 
----------------------------------------

全てのコンテナ群を停止
docker-compose stop

----------------------------------------
localpc rails6-compose % docker-compose stop
Stopping rails6-compose_web_1 ... done
Stopping rails6-compose_db_1  ... done
localpc rails6-compose % 
----------------------------------------

もっかいやったらupのほうのログが短くなった。

学習を再開するときはコンテナ群を起動する
学習を終了するときはコンテナ群を終了する


この記事が気に入ったらサポートをしてみませんか?