如何在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:localhostPort: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
相关产品推荐
相关产品推荐

