docker 无法使用pydevd-pycharm连接到远程调试服务器

f0ofjuux  于 2023-01-08  发布在  Docker
关注(0)|答案(2)|浏览(163)

我有一个用docker部署的Django项目。我想用Python Remote Debug调试这个项目。这是我如何根据https://www.jetbrains.com/help/pycharm/remote-debugging-with-product.html配置它的

之后我启动了调试器,它说:

Use the following code to connect to the debugger:
import pydevd_pycharm
pydevd_pycharm.settrace('0.0.0.0', port=3000, stdoutToServer=True, stderrToServer=True)
Waiting for process connection...

然后,我将代码复制到manage.py中的__main__

顺便说一句,把上面的代码放到一些views中,而不是manage.py中,也没有帮助
接下来,我使用docker-compose up -d proj命令由docker启动
过了一会儿,我看到我的集装箱里的日志上写着:

Could not connect to localhost: 3000
NoneType: None
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/wsgiref/handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "/usr/local/lib/python3.6/site-packages/django/contrib/staticfiles/handlers.py", line 63, in __call__
    return self.application(environ, start_response)
  File "/usr/local/lib/python3.6/site-packages/sentry_sdk/integrations/django/__init__.py", line 93, in sentry_patched_wsgi_handler
    environ, start_response
  File "/usr/local/lib/python3.6/site-packages/sentry_sdk/integrations/wsgi.py", line 69, in __call__
    rv = self.app(environ, start_response)
  File "/usr/local/lib/python3.6/site-packages/sentry_sdk/integrations/django/__init__.py", line 92, in <lambda>
    return SentryWsgiMiddleware(lambda *a, **kw: old_app(self, *a, **kw))(
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/wsgi.py", line 157, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python3.6/site-packages/sentry_sdk/integrations/django/__init__.py", line 112, in sentry_patched_get_response
    return old_get_response(self, request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in get_response
    response = self._middleware_chain(request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/utils/deprecation.py", line 140, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/utils/deprecation.py", line 140, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/utils/deprecation.py", line 140, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/utils/deprecation.py", line 140, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/utils/deprecation.py", line 140, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/utils/deprecation.py", line 140, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/utils/deprecation.py", line 140, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/utils/deprecation.py", line 140, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/utils/deprecation.py", line 140, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/app/xwing/middleware.py", line 21, in middleware
    response = get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/viewsets.py", line 116, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 492, in dispatch
    response = handler(request, *args, **kwargs)
  File "/app/cards/views.py", line 52, in version
    return Response({'version': get_newest_version()})
  File "/app/cards/queries.py", line 76, in get_newest_version
    pydevd_pycharm.settrace('localhost', port=8080, stdoutToServer=True, stderrToServer=True)
  File "/usr/local/lib/python3.6/site-packages/pydevd.py", line 1282, in settrace
    stop_at_frame,
  File "/usr/local/lib/python3.6/site-packages/pydevd.py", line 1327, in _locked_settrace
    debugger.connect(host, port)  # Note: connect can raise error.
  File "/usr/local/lib/python3.6/site-packages/pydevd.py", line 364, in connect
    s = start_client(host, port)
  File "/usr/local/lib/python3.6/site-packages/_pydevd_bundle/pydevd_comm.py", line 568, in start_client
    sys.exit(1) #TODO: is it safe?
SystemExit: 1

到目前为止,我试着在不同的端口上运行调试服务器,如0.0.0.0:8000,localhost:8000。但没有帮助。我接了另一个项目,确实需要Docker才能工作,而且所有工作都能找到。看起来Docker有一些问题。有什么想法吗?我不知道为什么我不能工作。

9gm1akwq

9gm1akwq1#

我从来没有这样做过,但看起来'0.0.0.0'部分是Pycharm运行的机器的地址。
You have to set the IP of your machine. May it will be also successful to run your container in network mode "host" (because 0.0.0.0 refers to inside the container)
您可能会发现以下内容也很有用:

nvbavucw

nvbavucw2#

不确定这是否会对任何人有帮助,但在我的情况下,我忘记了mac操作系统上的docker使用host.docker.internal。
所以我设置:

pydevd_pycharm.settrace('host.docker.internal', port=YOUR_PORT, stdoutToServer=True, stderrToServer=True)

然后点击编辑配置=〉python调试服务器:

host : host.docker.internal
port YOUR_PORT

不要忘记路径Map,(访问您的docker,看看在哪里可以找到与根目录完全相同的文件夹,不要忘记放置根目录的绝对路径,在我的例子中类似于/Users/UserName/Repo/Your_root =/app/src)
而且奏效了!
事实证明,端口可以是任何端口,只要它不与现有端口冲突(注意,编辑配置和settrace中的端口必须匹配)

相关问题