LoginSignup
7
11

More than 3 years have passed since last update.

M1 MacBookでDockerを使ったRails環境構築

Last updated at Posted at 2021-03-24

M1 MacBookAirを購入しました。
CPUがIntel版→Appleシリコン版になったことで、Dockerが心配でしたが、問題なく動作しています。
以下手順通りの操作で環境構築できました。

1.Docker Desktop for Apple Siliconをダウンロード

Intel版ではありません。ご注意を。

公式ページからダウンロードできます。
https://docs.docker.com/docker-for-mac/apple-m1/

2.Rosetta 2をインストール

Rosetta 2
→Intel版ソフトをAppleシリコン版で使えるように変換。

ターミナルでコマンド入力

softwareupdate --install-rosetta

3.ディレクトリとファイルの準備

ここからは公式クイックスタート通りにやるだけ。
https://docs.docker.com/compose/rails/

①作業ディレクトリの作成

mkdir myapp

myappの部分は好きなものでOK。アプリの名前とか。

②作業ディレクトリに移動

cd myapp

③必要なファイルを用意
ファイルは5つ
・Dockerfile→imageを作成
・Gemfile→gemのインストール
・Gemfile.lock→インストールしたgemの結果を記録
・entrypoint.sh→Docker起動時の処理
・docker-compose.yml→Dockerビルドやコンテナ起動に必要

まずはファイル作成。5つ同時に作れます。
ターミナルでコマンド入力

touch {Dockerfile,Gemfile,Gemfile.lock,entrypoint.sh,docker-compose.yml}

一応①の段階でディレクトリとファイル同時に作ることもできます。好きな方でどうぞ。

mkdir myapp && touch myapp/{Dockerfile,Gemfile,Gemfile.lock,entrypoint.sh,docker-compose.yml}

④ファイルを編集
それぞれコピペでOK

Dockerfile

FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

# 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

# Configure the main process to run when running the image
CMD ["rails", "server", "-b", "0.0.0.0"]

Gemfile

source 'https://rubygems.org'
gem 'rails', '~>5'

Gemfile.lock
空のままでOK

entrypoint.sh
#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
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
    ports:
      - "3000:3000"
    depends_on:
      - db

4.プロジェクトを立ち上げる

下準備が整えば後はコマンドでビルドできます。
rails newのDocker版です。

docker-compose run --no-deps web rails new . --force --database=postgresql

処理が終わったらGemfileが更新されているので再度ビルド

docker-compose build

5.データベースを繋げる

デフォルトのものからpostgres imgaeに合うように変更
以下コードをコピペ

config/database.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

6.アプリを起動

ターミナルでコマンドを入力

docker-compose up

逆にアプリを止めたい時は

docker-compose down

7.データベースを作成

ターミナルでコマンドを入力

docker-compose run web rake db:create

8.ブラウザで確認

お手持ちのブラウザで以下URLにアクセスして下さい。
http://localhost:3000

いつもの"Yay!You're on Rails!"が表示されていれば成功です。
お疲れ様でした。

9.まとめ

公式推奨のやり方なので安定して環境構築できるのが気持ちいい。
しかし、RubyやRailsのバージョンが最新ではないので必要に応じてアップグレードしてください。

7
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
11