概要
M1チップのMacBook側で、rails6 on Dockerを試しました。
成功時の手順を記録します。
前提情報
-
M1MacBook
-
Docker Desktop for Apple silicon Version 4.15.0
-
ruby-3.1.2
構築までのメモ
アプリケーション用のディレクトリを切る
mkdir railsapp
cd railsapp
基本ファイルを用意
touch Dockerfile
touch Gemfile
touch Gemfile.lock
touch docker-compose.yml
touch entrypoint.sh
./Dockerfile
FROM ruby:3.1.2
RUN apt-get update -qq && apt-get install -y build-essential nodejs postgresql-client libpq-dev
RUN apt-get update && apt-get install -y curl apt-transport-https wget && \
wget --quiet -O - /tmp/pubkey.gpg https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt-get update && apt-get install -y yarn
# install nodejs(LTS)
RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - && apt-get install -y nodejs
RUN mkdir /railsapp
WORKDIR /railsapp
COPY Gemfile /railsapp/Gemfile
COPY Gemfile.lock /railsapp/Gemfile.lock
RUN bundle install
COPY . /railsapp
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
./Gemfile
source "https://rubygems.org"
gem "rails", "~>6"
./docker-compose.yml
version: '3'
services:
db:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
environment:
POSTGRES_HOST_AUTH_METHOD: 'trust'
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/railsapp
ports:
- "3000:3000"
depends_on:
- db
./entrypoint.sh
#!/bin/bash
set -e
rm -f /railsapp/tmp/pids/server.pid
exec "$@"
Railsプロジェクトの作成
docker-compose run web rails new . --force --no-deps --database=postgresql --skip-bundle
ビルド
docker-compose build
DB基本情報の記載
./config/database.yml
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: postgres
password: xxxxxxxx
development:
<<: *default
database: railsapp_development
※実際運用する際は、passwordはベタ書きせず、最低限
gem 'dotenv-rails'
などを使用して.env(ignoreする)にて管理し、
password: <%= ENV['APP_DBPASS'] %>
として運用する
DB作成
docker-compose run web rake db:create
webpackのインストール
docker-compose run web rails webpacker:install
upして動作確認
docker-compose up
http://localhost:3000/
参考記事
詰まった箇所(上記には反映済)
GPG(GNU Privacy Guard)キー部分を更新
gpg: no valid OpenPGP data found.
↓参考にして更新
https://zenn.dev/junki555/articles/2de6024a191913
Rubyやnodejsの対応バージョンをあわせる
はじめはnokogiriエラーがあったので
以下記事を参考にトラシューしたが、結果的にrubyとnodejsのバージョン変更で解決
https://regardie.dev/post-5126
ほか
gem "net-smtp"
を足しても良いかも