You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Docker运行的Ruby应用中配置RubyMine断点调试?

Docker环境下Rails应用RubyMine断点调试实现步骤

想要在Docker运行的Rails应用中,实现和本地RubyMine添加debugger语句一样的断点调试功能,现有docker-compose.yml和Dockerfile配置如下:

现有配置文件

docker-compose.yml

version: '3'
services:
  web:
    environment:
      - RUBYOPT=-rdebug
    build:
      context: .
      dockerfile: Dockerfile
    container_name: debug-rails
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    working_dir: /app
    command: bash -c "bundle install && rails server -b 0.0.0.0"

  db:
    image: postgres:latest
    container_name: debug-postgres
    environment:
      POSTGRES_USER: myappuser
      POSTGRES_PASSWORD: myapppassword
      POSTGRES_DB: myappdb
    volumes:
      - ./db-data:/var/lib/postgresql/data

Dockerfile

# Use the official Ruby image
FROM ruby:2.7.5

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    nodejs \
    yarn

# Set the working directory
WORKDIR /app

# Copy the Gemfile and Gemfile.lock into the container
COPY Gemfile Gemfile.lock ./

# Install gems
RUN bundle install
RUN gem install ruby-debug-ide debase
RUN gem install docker-api

# Copy the rest of the application code into the container
COPY . .

# Start the application
CMD ["rails", "server", "-b", "0.0.0.0"]

断点调试具体步骤

1. 调整Docker配置,开放调试端口

修改docker-compose.yml的web服务部分,添加调试端口映射并调整启动命令:

services:
  web:
    # 保留原有environment、build、container_name、volumes、working_dir配置
    ports:
      - "3000:3000"
      - "1234:1234" # 添加调试主端口映射
      - "26162:26162" # 添加调试调度端口映射
    # 替换原有command为调试启动命令
    command: bash -c "bundle install && ruby-debug-ide --host 0.0.0.0 --port 1234 --dispatcher-port 26162 -- rails s -b 0.0.0.0"

说明:--host 0.0.0.0允许外部连接调试端口,两个端口需保持本地与容器的映射一致。

2. 重新构建并启动容器

在项目根目录执行命令:

docker-compose down && docker-compose build && docker-compose up

启动后,观察容器日志,出现Fast Debugger listens on 0.0.0.0:1234类的输出,说明调试服务已正常启动。

3. RubyMine配置远程调试

  • 打开RubyMine,进入Run > Edit Configurations
  • 点击左上角+号,选择Ruby remote debug
  • 配置名称(如Docker Rails Debug),设置:
    • Host: localhost
    • Port: 1234(与docker-compose中配置的调试主端口一致)
    • 勾选Remote host port,保持26162不变
  • 切换到Deployment标签页,选择Local or mounted folder,设置路径映射:本地项目根目录 → /app(容器内工作目录)
  • 点击Apply保存配置

4. 设置断点并启动调试

  • 在RubyMine代码中需要调试的行左侧点击,添加断点(红色圆点)
  • 点击右上角调试按钮(绿色虫子图标),启动远程调试会话
  • 访问应用触发断点对应的逻辑(如接口、页面),RubyMine会自动暂停在断点处,可进行变量查看、单步调试等操作

5. 可选:用debugger语句触发断点

和本地调试逻辑一致,在代码中添加debugger语句,执行到此处时,调试会话会自动暂停,无需手动设置断点。

内容的提问来源于stack exchange,提问作者Ruby On Rails

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 18:42:47