Railsで学習をする機会があったので、7.1の環境を自身のPCに作ってみたので
構築方法を記録する。
(環境)
Mac(バージョン:14.5 チップ:Apple M2)
(構築)
Rails:7.1.3
Ruby:3.3.2
MySQL8.0
memo
- Rails7になってWebPackが不要になった
- 途中でDockerfileが書き換わるので、build前にDockerfileを一部変更した
1.Docker for Macのインストール
(インストール済みならスキップ)
https://docs.docker.com/compose/install/
2.Rails / MySQLアプリケーションを設定
2-1.ファイル用意
開発していくディレクトリにアプリケーションを構築するために必要な4つのファイルを設定する。
{プロジェクト}
|_Dockerfile
|_Gemfile
|_Gemfile.lock
|_entrypoint.sh
|_docker-compose.yml
それぞれのファイルを設定していく。
FROM ruby:3.3.2
RUN mkdir /app
WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
RUN bundle install
COPY . /app
# 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"]
source 'https://rubygems.org'
gem 'rails', '~>7.1.3'
Gemfile.lockは空のファイルにしておく。
#!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails.
rm -f /app/tmp/pids/server.pid
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
services:
db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: root
ports:
- "3306:3306"
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'"
volumes:
- .:/app
ports:
- "3000:3000"
depends_on:
- db
ここまでのファイルを以下のブランチにアップしておく
https://github.com/mem0112mem/rails7-docker/tree/before-dcoker-build
2-2. プロジェクトの構築
作成した5つのファイルを利用してdocker-compose runを実行し、Railsアプリケーションを生成する。(ターミナルを使って実施)
$ docker-compose run web rails new . --force --no-deps --database=mysql
:
:
run bundle install
Bundle complete! 14 Gemfile dependencies, 82 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
2-3. Dockerfile修正
DockerfileにあるRAILS_ENVがproductionに変更されていた。
今回はローカル環境構築なので、developmentに変更する。
:
# Set production environment
ENV RAILS_ENV="production" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development"
以下に変更する。
ENV RAILS_ENV="development" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle"
2-4.データベースの設定
データベースの情報を設定するために、./config/database.yml
を変更する。
developmentとtestに、host、username、passwordを設定する。
:
development:
<<: *default
database: app_development
# ここから追加
host: db
username: root
password: password
# ここまで追加
:
:
test:
<<: *default
database: app_test
# ここから追加
host: db
username: root
password: password
# ここまで追加
:
2-5. Buildを実施
$ docker-compose build
WARN[0000] {プロジェクト}/docker-compose.yml: `version` is obsolete
[+] Building 255.7s (19/19) FINISHED
:
:
=> [web stage-2 4/4] RUN useradd rails --create-home --shell /bin/bash && chown -R rails:rails db log storage tmp 2.7s
=> [web] exporting to image 0.9s
=> => exporting layers 0.9s
=> => writing image sha256:xxxxxxxx 0.0s
=> => naming to docker.io/library/xxxx-web
2-6.コンテナ実行
$ docker-compose up -d
WARN[0000] {プロジェクト}/docker-compose.yml: `version` is obsolete
[+] Running 2/2
✔ Container xxxx-db-1 Running 0.0s
✔ Container xxxx-web-1 Started
2-7. DB作成
dockerコンテナにアクセスしてDB作成する。
bundle exec rails db:create
を実行する。
$ docker exec -it {webコンテナ名} /bin/bash
rails@0fa7b307672d:/rails$ bundle exec rails db:create
Created database 'app_development'
Created database 'app_test'
http://localhost:3000/
にアクセスしてRails画面が表示されれば、構築完了。