LoginSignup
7
6

More than 1 year has passed since last update.

M1 MacにRuby on Rails 6のDocker環境を構築する

Last updated at Posted at 2022-01-27

概要

M1 MacにRuby on Rails 6のDocker環境を構築する

手順

  1. 必要なファイルの用意

Dockerfile

FROM ruby:3.0.2

RUN wget --quiet -O - /tmp/pubkey.gpg https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
    echo 'deb http://dl.yarnpkg.com/debian/ stable main' > /etc/apt/sources.list.d/yarn.list
RUN set -x && apt-get update -y -qq && apt-get install -yq nodejs yarn

RUN mkdir /app
WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
RUN bundle install
COPY . /app

docker-compose.yml

version: '3'
services:
  app:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/app
    ports:
      - 3000:3000
    depends_on:
      - db
    tty: true
    stdin_open: true
  db:
    platform: linux/x86_64
    image: mysql:5.7
    volumes:
      - db-volume:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: password
volumes:
  db-volume:

Gemfile

source 'https://rubygems.org'
gem 'rails', '6.1.4'

entrypoint.sh

#!/bin/bash
set -e

rm -f /myapp/tmp/pids/server.pid

exec "$@"

空のGemfile.lockを作成

touch Gemfile.lock
  1. アプリ作成コマンド実行
docker-compose run app rails new . --force --database=mysql
  1. DBファイルの修正
    config/database.yml
    (変更前)
~略~
default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password:
  host: localhost
~略~

(変更後)

~略~
default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: password
  host: db
~略~
  1. build
 docker-compose build
  1. 起動
 docker-compose up -d
  1. DB作成
docker-compose run app rails db:create

参考文献

https://zenn.dev/tmasuyama1114/articles/rails-docker-6x-how-to
→M1 macでは挙動怪しかった

7
6
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
6