I have a Django project that I deploy with docker. I want to debug the project with Python Remote Debug
. This I how I configured it according to the https://www.jetbrains.com/help/pycharm/remote-debugging-with-product.html
Afterwards I launched the debugger and it said:
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...
Afterwards I coppied the code to __main__
in manage.py
Btw. Puting above code to some views
instead manage.py dint help either
Next I launched by docker with docker-compose up -d proj
command
After a while I saw logs from my container that says that:
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
So far I tried to run the debug server on differet ports like 0.0.0.0:8000, localhost:8000. But It didn't help. I took another project does does require docker to work, and everything worked find. It seems that there is some problem with a docker. Any ideas? I have no idea why I does not work.
I never had done this, but it looks like the part '0.0.0.0' is the address to the machine where Pycharm is running.
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)
May you find also useful:
A very good post over host networking: https://stackoverflow.com/a/24326540/10962269
The documentation: https://www.jetbrains.com/help/pycharm/remote-debugging-with-product.html