0
0

More than 1 year has passed since last update.

M1MacのローカルでDockerを使ってRailsの環境を構築する。

Posted at

Dockerでの環境構築につまづいたこととコントローラとルーティングでTOPページを表示させるのにつまづいたので備忘録として簡単に設定をやってきたことを記載します。

行ったことはほぼこの動画通りなので、記載内容について詳しく知りたい方はこちらを参照ください。
とてもわかりやすく解説されていました。
https://www.youtube.com/watch?v=lZD1MIHwMBY

最初に準備したものは
デスクトップにフォルダを作成して以下のファイルを用意した。
・Dockerfile
・docker-compose.yml
・srcディレクトリの配下にGemfile

スクリーンショット 2022-05-22 15.11.16.png

src以下のGemfile以外はこの後の操作で作成される。
スクリーンショット 2022-05-22 15.11.53.png

ここからファイルの中身を記載

Dockerfile
FROM ruby:2.7
RUN curl -sS 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 -qq \
  && apt-get install -y nodejs yarn
WORKDIR /app
COPY ./src /app
RUN bundle config --local set path 'vendor/bundle' \
  && bundle install
docker-compose.yml
version: '3'
services:
  db:
    platform: linux/x86_64
    image: mysql:8.0
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - ./src/db/mysql_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: password
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - ./src:/app
    ports:
      - "3000:3000"
    environment:
      RAILS_ENV: development
    depends_on:
      - db
Gemfile
source 'https://rubygems.org'

gem 'rails', '~> 6.1.0'

これらを用意した後に

ターミナル
docker-compose run web rails new . --force --database=mysql

インストールまでに10分ほどかかった。

これによって大量のRailsのファイルができている

スクリーンショット 2022-05-22 17.02.21.png

Gemfileの中身が更新されていることを確認する。

Gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.7.6'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main'
gem 'rails', '~> 6.1.6'
# Use mysql as the database for Active Record
gem 'mysql2', '~> 0.5'
# Use Puma as the app server
gem 'puma', '~> 5.0'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 5.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.4', require: false

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 4.1.0'
  # Display performance information such as SQL time and flame graphs for each request in your browser.
  # Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md
  gem 'rack-mini-profiler', '~> 2.0'
  gem 'listen', '~> 3.3'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 3.26'
  gem 'selenium-webdriver', '>= 4.0.0.rc1'
  # Easy installation and use of web drivers to run system tests with browsers
  gem 'webdrivers'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Gemfileの中身が更新されたら(Dockerfileの場合も同様)
イメージをビルドし直す必要がある。

M1Macの場合は以下のようにdbの下に追記する。

docker-compose.yml
version: '3'
services:
  db:
    platform: linux/x86_64 # M1チップ対応のため追記
    image: mysql:8.0
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - ./src/db/mysql_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: password
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - ./src:/app
    ports:
      - "3000:3000"
    environment:
      RAILS_ENV: development
    depends_on:
      - db

そしてビルドする。

ターミナル
docker-compose build

src/config/database.ymlのファイルを見る

password :
host:
この二つを下記のように追記・修正する。

src/config/database.yml
default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: password
  host: db

development:
  <<: *default
  database: app_development

# 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: app_test

この修正が終わったらターミナルでDBの作成コマンドを入力する。

ターミナル
docker-compose run web rails db:create 

DBが作成できたらコンテナの作成と起動

ターミナル
docker-compose up

これでサーバが起動したはずなので

GoogleのURLバーに

localhost:3000と入力する。

この画面が出たら、OK

スクリーンショット 2022-05-22 17.16.34.png

詳しい説明はこちらの動画をご覧ください。
https://www.youtube.com/watch?v=lZD1MIHwMBY

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