LoginSignup
9
2

More than 1 year has passed since last update.

【Rails】【docker】お前もdebug.gemにならないか?

Last updated at Posted at 2022-12-16

※debug gemの使いかたは説明しません!!

使い方はこちらの記事みてください
https://techlife.cookpad.com/entry/2021/12/27/202133
https://qiita.com/yuskubo/items/111a87e2402de8e12914

対象読者

  • debug.gem使いたいんだけど、docker上のRailsアプリだと何か上手く使えないニキ
  • controllerとかで p とか puts 使ってるニキ

そもそもdebug.gemってなに?

debug gemは2021年に公開されたRuby用のデバッガです。gemをインストールすることでRuby2.6以降で利用可能です。Ruby3.1以降ではデフォルトでRubyに添付されています。
また、Rails7.0以降でデフォルトで入っているデバッガとなっています。https://zenn.dev/igaiga/books/rails-practice-note/viewer/ruby_debug_gem#debug-gem

なんかつよいデバッガらしい

Docker上のRailsだと上手く起動できないニキ

docker上だと上手く起動できなかったので、忘備録として記事に残します。
dockerで開発している紳士諸君は是非に p や puts を使わずにdebug.gemを使って欲しい。
そしてこの記事を読んでおまえも、debug.gemにならないか?

docker-compose.yml

docker-compose.ymlに「tty: true」と「stdin_open: true」の設定を追加

docker-compose.yml
version: '3'
services:
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: root
    ports:
      - "3306:3306"
    volumes:
      - ./tmp/db:/var/lib/mysql

  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0' && ./bin/dev"
    container_name: myrailsapp_web_1
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
  
    ## 以下2行を追記
    stdin_open: true
    tty: true

    depends_on:
      - db

デバッグの準備

コンテナの起動

コンテナ起動させます。

docker-compose up

次に立ち上げたWebサーバーのコンテナ名を取得します。

コンテナにアタッチする

docker attach Webサーバーのコンテナ名(docker psで取得)

これでデバックができるようになります!やったね!

ブレイクポイントを入れてみる

コントローラーにブレイクポイントを入れて値を確認してみる

users_controller.rb
class UsersController < ApplicationController

  def index
    @users = User.all
  end

  def show
    @user = User.find(params[:id])
    # ブレークポイントを追加
    binding.break
  end
end

コンテナにアタッチしたコンソールを確認すると

[5, 12] in /app/app/controllers/users_controller.rb
     5|   end
     6| 
     7|   def show
     8|     @user = User.find(params[:id])
     9|     # ブレークポイントを追加
=>  10|     binding.break
    11|   end
    12| end
=>#0    UsersController#show at /app/app/controllers/users_controller.rb:10
  #1    ActionController::BasicImplicitRender#send_action(method="show", args=[]) at /usr/local/bundle/gems/actionpack-6.1.6.1/lib/action_controller/metal/basic_implicit_render.rb:6
  # and 71 frames (use `bt' command for all frames)
(rdbg) 

これでデバックができるようになりました。
試しに@userの中身を確認してみます。

[5, 12] in /app/app/controllers/users_controller.rb
     5|   end
     6| 
     7|   def show
     8|     @user = User.find(params[:id])
     9|     # ブレークポイントを追加
=>  10|     binding.break
    11|   end
    12| end
=>#0    UsersController#show at /app/app/controllers/users_controller.rb:10
  #1    ActionController::BasicImplicitRender#send_action(method="show", args=[]) at /usr/local/bundle/gems/actionpack-6.1.6.1/lib/action_controller/metal/basic_implicit_render.rb:6
  # and 71 frames (use `bt' command for all frames)
(ruby) @user
#<User _id: BSON::ObjectId('639c0cbfe5614c0001ba7b1c'), admin: false, created_at: Fri, 16 Dec 2022 15:14:23.309000000 JST +09:00, name: "きょうじろう", updated_at: Fri, 16 Dec 2022 15:14:23.309000000 JST +09:00>
(rdbg) 

おお〜簡単に中身がみれました。他にもdebugの使い方等をみたい方は参考文献みてくだちい
まだそんな方法で使ってんのかよ!もっといい方法あるぞ!!って人いてたら教えてください

📖参考

debug gemの使い方
https://techlife.cookpad.com/entry/2021/12/27/202133
https://qiita.com/yuskubo/items/111a87e2402de8e12914

dockerの設定
http://www.code-magagine.com/?p=8697

9
2
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
9
2