はじめに
Everyday Rails の作業をDocker-composeで環境構築し、勉強していくためのメモ
DBはpostgreSQLを利用
リモートリポジトリからのクローン、作業ディレクトリ作成
-bで指定するブランチのコピーができる
git clone -b 01-untested https://github.com/JunichiIto/everydayrails-rspec-jp-2022.git
ディレクトリに移動
cd everydayrails-rspec-jp-2022
Docker環境の構築の準備
Dockerfile
Rubyのバージョンは本書指定の3.1
FROM ruby:3.1.2
ENV LANG C.UTF-8
RUN apt-get update -qq && apt-get install -y \
build-essential \
nodejs \
&& rm -rf /var/lib/apt/lists/*
RUN gem install bundler
WORKDIR /tmp
ADD Gemfile Gemfile
ADD Gemfile.lock Gemfile.lock
RUN bundle install
ENV APP_HOME /myapp
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME
ADD . $APP_HOME
docker-compose.yml
version: "3.9"
services:
db:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: password
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
- bundle:/usr/local/bundle
ports:
- "3000:3000"
depends_on:
- db
- chrome
chrome:
image: selenium/standalone-chrome:latest
ports:
- 4444:4444
volumes:
bundle:
postgre_data:
chromeのコンテナは今後 chromeのheadressを使用するためのコンテナです。
(以前RSpecのテスト時にないとできなかったため)
もしかしたらいらないかも。
Gemfile
sqliteの行をpostgreに変更
gem 'pg'
DB設定変更
config/databese.yml
default: &default
adapter: postgresql
encoding: unicode
host: db
username: postgres
password: password
pool: 5
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
Dockerコンテナビルド&起動
$ docker-compose build
bundle install
docker-compose run web bundle install
Dockerコンテナ起動
docker-compose up -d
これでlocalhost:3000に接続すると、
databeseがないよと怒られるので、
データベースを作成
docker-compose exec web rails db:create
マイグレーション
docker-compose exec web rails db:migrate
ブラウザでlocalhost:3000にアクセスし出ればOK
テストの実行
docker-compose run web bundle exec rspec
#No examples found.
Finished in 0.00232 seconds (files took 0.07722 seconds to load)
0 examples, 0 failures
Chromeのheadlessブラウザの設定
systemテストをする場合、RSpecインストール後、
下記サイトを参考にrails_helperなどの設定を変更する
git で進捗を管理する場合
githubで新しいリポジトリを作って、
remote ブランチを変更する
今のリモートリポジトリを確認
everydayrails-rspec-jp-2022 % git remote -v
origin https://github.com/JunichiIto/everydayrails-rspec-jp-2022.git (fetch)
origin https://github.com/JunichiIto/everydayrails-rspec-jp-2022.git (push)
リモートリポジトリを変更
everydayrails-rspec-jp-2022 % git remote set-url origin {自分のGitHubリポジトリのURL}
everydayrails-rspec-jp-2022 % git remote -v
origin 自分のGitHubリポジトリのURL (fetch)
origin 自分のGitHubリポジトリのURL (push)
リモートリポジトリへプッシュまで行う
everydayrails-rspec-jp-2022 % git add -A
everydayrails-rspec-jp-2022 % git commit -m"first commit"
everydayrails-rspec-jp-2022 % git branch -M main
everydayrails-rspec-jp-2022 % git push -u origin main
githubのリポジトリにコードがあればOK
あとは、章が終わるごとなどでコミット&プッシュしていけば、作業履歴が残る
参考