将 vscode 中的调试配置为 Rails 和 Docker 上的 Ruby

Configure debug in vscode to Ruby on Rails and Docker

当我尝试使用 docker 在 vscode 中为 Rails 应用程序上的 Ruby 配置调试时,遵循我的 Dockerfile:

FROM ruby:2.6.5

# add nodejs and yarn dependencies for the frontend
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - && \
    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

# Instala nossas dependencias
RUN apt-get update && apt-get install -qq -y --no-install-recommends \
    nodejs yarn build-essential libpq-dev imagemagick git-all nano

# Instalar bundler
RUN gem install bundler

# Seta nosso path
ENV INSTALL_PATH /onebitexchange

# Cria nosso diretório
RUN mkdir -p $INSTALL_PATH

# Seta o nosso path como o diretório principal
WORKDIR $INSTALL_PATH

# Copia o nosso Gemfile para dentro do container
COPY Gemfile ./

# Seta o path para as Gems
ENV BUNDLE_PATH /gems

# Copia nosso código para dentro do container
COPY . .

我的docker-compose.yml:

version: "3.8"

services: 
    db:
        image: "postgres:12.2"
        environment: 
            - POSTGRES_PASSWORD=postgres
        volumes: 
            - postgres:/var/lib/postgresql/data

    app:
        build: .
        command: bash start.sh
        ports:
            - "3000:3000"
            - "1234:1234"
        environment: 
            - DB_PASSWORD=postgres
        volumes: 
            - .:/onebitexchange
            - gems:/gems
        depends_on: 
            - db

volumes: 
    postgres:
    gems:

我的start.sh:

# Instala as Gems
bundle check || bundle install
​
# Roda nosso servidor
# bundle exec puma -C config/puma.rb

bundle exec rdebug-ide --host 0.0.0.0 --port 1234 --dispatcher-port 26162 -- /onebitexchange/bin/rails s -b 0.0.0.0 -e development

我的launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach to Docker",
            "type": "Ruby",
            "request": "attach",
            "remotePort": "1234",
            "remoteHost": "127.0.0.1",
            "remoteWorkspaceRoot": "/onebitexchange",
            "cwd": "${workspaceRoot}",
            "showDebuggerOutput": true
        }
    ]
}

如果我在没有调试的情况下启动(bundle exec puma -C config/puma.rb),我的项目工作正常,但是当我尝试使用调试容器启动时,我收到以下消息(还没有问题):

Successfully built c60b5fc8b26a
Successfully tagged onebit_exchange_app:latest
Starting onebit_exchange_db_1 ... done
Recreating onebit_exchange_app_1 ... done
Attaching to onebit_exchange_db_1, onebit_exchange_app_1
db_1   | 
db_1   | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1   | 
db_1   | 2020-05-19 16:38:29.734 UTC [1] LOG:  starting PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1   | 2020-05-19 16:38:29.734 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2020-05-19 16:38:29.734 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2020-05-19 16:38:29.745 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2020-05-19 16:38:29.784 UTC [28] LOG:  database system was shut down at 2020-05-19 14:38:39 UTC
db_1   | 2020-05-19 16:38:29.791 UTC [1] LOG:  database system is ready to accept connections
app_1  | The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
app_1  | The Gemfile's dependencies are satisfied
app_1  | start.sh: line 3: $'203': command not found
app_1  | Fast Debugger (ruby-debug-ide 0.7.2, debase 0.2.4.1, file filtering is supported) listens on 0.0.0.0:1234

但是当我在 vscode 中开始调试时:

app_1  | => Booting Puma
app_1  | => Rails 6.0.2.2 application starting in development 
app_1  | => Run `rails server --help` for more startup options
app_1  | A server is already running. Check /onebitexchange/tmp/pids/server.pid.
app_1  | Exiting
onebit_exchange_app_1 exited with code 1

谢谢

您的 start.sh 文件的第 3 行似乎有一些有趣的编码,但未被解释为注释,这可能导致注释行实际启动服务器。

bundle exec rdebug-ide --host 0.0.0.0 --port 1234... 命令执行时,它抛出 'port in use' 错误,因为第一个命令的端口 1234 上已经有一个服务器 运行。

如果您删除注释掉的行,您就可以开始了。