【问题标题】:Configure debug in vscode to Ruby on Rails and Docker将 vscode 中的调试配置为 Ruby on Rails 和 Docker
【发布时间】:2020-05-19 16:43:32
【问题描述】:

当我尝试使用 docker 在 vscode 中为 Ruby on Rails 应用程序配置调试时,请按照我的 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: $'\342\200\213': 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 中注释掉的行?看起来那里可能有一些有趣的编码,即使它被注释掉,也会导致服务器脚本触发。这可以解释为什么你已经有了 rails 服务器的 PID
  • 谢谢!工作正常。

标签: ruby-on-rails docker docker-compose vscode-debugger


【解决方案1】:

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

bundle exec rdebug-ide --host 0.0.0.0 --port 1234... 命令执行时,它会抛出“正在使用的端口”错误,因为从第一个命令开始,端口 1234 上已经有一个服务器在运行。

如果你删除了注释掉的行,你应该很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    • 2015-09-02
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    相关资源
    最近更新 更多