🐋
【Docker】Rails7+Ruby3.2.0+PostgresqlのDocker環境を構築🐋
初心者が挫折しない様、極力長い説明は省き、シンプルにしました!疑問点は適宜調べ深掘りしてみて下さい。
では、早速やってみましょう。
動作環境
- MacBook Air (M1,16GB)
Rails 7.0.4+Ruby3.2.0+PostgresqlのDocker環境構築手順
フォルダの作成
ターミナルにて下記コマンド実行
ターミナル
mkdir docker_rails7_postgresql
cd docker_rails7_postgresql
Gemfile, Gemfile.lock, Dockerfile,docker-compose.ymlファイルを作成
ターミナルにて下記コマンド実行
ターミナル
touch {Gemfile,Gemfile.lock,Dockerfile,docker-compose.yml}
Dockerfile,Gemfile,docker-compose.ymlを編集
作成したファイルを編集していく
Dockerfile
FROM ruby:3.2.0
RUN apt-get update -qq && apt-get install -y \
build-essential \
libpq-dev \
vim
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN gem update --system && gem install bundler \
&& bundle config set force_ruby_platform true \
&& bundle install
ADD . /myapp
- 備考メモ
- Rails7ではWebpackerが標準で組み込まれなくなったので、yarnやnodejsのインストールが不要
- nokogiriエラーが出たので、Bundlerをインストールする前に、Dockerfileにgem update --systemおよびbundle config set force_ruby_platform trueを実行するように修正
- Docker Instructionメモ(詳細は各自で調べてみて下さい)
- FROM:使用するイメージ、バージョン
- RUN:任意のコマンド実行
- WORKDIR:作業ディレクトリを指定
- COPY:コピー元とコピー先を指定, 今回はローカルのGemfileをコンテナ内の/app/Gemfileに
- CMD:コンテナ実行時、デフォルトで実行したいコマンド
続いてGemfileを編集
Gemfile
source 'https://rubygems.org'
gem 'rails', '~> 7.0.4'
続いてdocker-compose.yml
docker-compose.yml
version: '3'
services:
db:
image: postgres:latest
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
ports:
- "5432:5432"
volumes:
- postgres_volume:/var/lib/postgresql/data
restart: always
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
volumes:
postgres_volume:
docker compose run --rm web rails new . --force --database=postgresqlを実行
上記ファイル作成後下記コマンドを実行
docker compose run --rm web rails new . --force --database=postgresql
- 備考メモ
- --force : 同じファイルがある場合上書き
- --database=mysql : データベースにpostgresqlを指定
database.ymlファイル設定編集
database.ymlに下記のコードを追加
host: db
username: <%= ENV["POSTGRES_USER"] %>
password: <%= ENV["POSTGRES_PASSWORD"] %>
database.yml
# PostgreSQL. Versions 9.3 and up are supported.
#
# Install the pg driver:
# gem install pg
# On macOS with Homebrew:
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On macOS with MacPorts:
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem "pg"
#
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: db #追加!!!!!!
username: <%= ENV["POSTGRES_USER"] %> #追加!!!!!!!
password: <%= ENV["POSTGRES_PASSWORD"] %> #追加!!!!!!!!!
development:
<<: *default
database: myapp_development
port: 5432
# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user running Rails.
#username: myapp
# The password associated with the postgres role (username).
#password:
# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines.
#host: localhost
# The TCP port the server listens on. Defaults to 5432.
# If your server runs on a different port number, change accordingly.
#port: 5432
# Schema search path. The server defaults to $user,public
#schema_search_path: myapp,sharedapp,public
# Minimum log levels, in increasing order:
# debug5, debug4, debug3, debug2, debug1,
# log, notice, warning, error, fatal, and panic
# Defaults to warning.
#min_messages: notice
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: myapp_test
port: 5432
# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password or a full connection URL as an environment
# variable when you boot the app. For example:
#
# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# If the connection URL is provided in the special DATABASE_URL environment
# variable, Rails will automatically merge its configuration values on top of
# the values provided in this file. Alternatively, you can specify a connection
# URL environment variable explicitly:
#
# production:
# url: <%= ENV["MY_APP_DATABASE_URL"] %>
#
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full overview on how database connection configuration can be specified.
#
production:
<<: *default
database: myapp_production
username: myapp
password: <%= ENV["MYAPP_DATABASE_PASSWORD"] %>
docker-compose build を実行
rails new を実行したことにより、 Gemfileが変更。新しいgemをインストールするために、コマンドを打ちコンテナを再ビルドして起動。
docker compose up --build
データベースの作成
コンテナが起動したら下記コマンドでデータベースを作成
docker compose exec web rails db:create
localhost:3000
localhost:3000にアクセスするとrailsのマークが表示の画面にいくはずです!
↓起動できたらモチベーションアップに繋がるのでいいねお願いします!
Discussion