我在docker中使用了Django (2, 2, 4, 'final', 0)
,但是我可以在里面猛击以打开或执行所需的命令。但是我无法调试。 (How to debug in Django, the good way?指出了一些方法,但对我而言却无效)
在我的views.py
中,我有各种功能,例如这里的功能。
def visGraph(request):
showgraph = 'Graphen'
selectDB = request.GET.get('selectDB', '')
__import__("pdb").set_trace()
title += " <i>"+showgraph+"</i> ("+selectDB+")"
在我填写pdb之前,它工作正常,添加调试器会使我的应用立即崩溃:
> /code/DjangoGraphen/views.py(74)visGraph()
-> title += " <i>"+showgraph+"</i> ("+selectDB+")"
(Pdb)
Internal Server Error: /DjangoGraphen/visGraph
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "./DjangoGraphen/views.py", line 74, in visGraph
title += " <i>"+showgraph+"</i> ("+selectDB+")"
File "./DjangoGraphen/views.py", line 74, in visGraph
title += " <i>"+showgraph+"</i> ("+selectDB+")"
File "/usr/lib64/python3.7/bdb.py", line 88, in trace_dispatch
return self.dispatch_line(frame)
File "/usr/lib64/python3.7/bdb.py", line 113, in dispatch_line
if self.quitting: raise BdbQuit
bdb.BdbQuit
ERROR:django.request:Internal Server Error: /DjangoGraphen/visGraph
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "./DjangoGraphen/views.py", line 74, in visGraph
title += " <i>"+showgraph+"</i> ("+selectDB+")"
File "./DjangoGraphen/views.py", line 74, in visGraph
title += " <i>"+showgraph+"</i> ("+selectDB+")"
File "/usr/lib64/python3.7/bdb.py", line 88, in trace_dispatch
return self.dispatch_line(frame)
File "/usr/lib64/python3.7/bdb.py", line 113, in dispatch_line
if self.quitting: raise BdbQuit
bdb.BdbQuit
[21/Oct/2019 17:47:14] "GET /DjangoGraphen/visGraph?selectDB=Test&showgraph=graph HTTP/1.1" 500 88178
如果我使用__import__("pdb").set_trace()
或breakpoint()
并不重要-两者都返回相同的结果。
到目前为止,在我的settings.py中,我有DEBUG = True
,将其设置为False
并没有任何改变。
我正在使用以下命令查看命令行中的日志:
docker logs django_web_1 -f
我假设对于pdb,我需要一个活动的Shell,而不仅仅是一个日志查看器,但是我不知道要更改什么或如何进行更改。 但是已经尝试过这里给出的答案:Interactive shell in Django 但这只是打开了一个Python Shell。
答案 0 :(得分:0)
该解决方案实际上相当简单。问题是码头工人。该解决方案在这里说明并有效: https://medium.com/@vladyslav.krylasov/how-to-use-pdb-inside-a-docker-container-eeb230de4d11
将此添加到您的docker-compose.yml:
ports:
- "4444:4444"
stdin_open: true
tty: true
安装remote-pdb
并使用而不是默认的pdb命令:
__import__("remote_pdb").set_trace(host='0.0.0.0', port=4444)
登录您的docker,并登录telnet进入pdb会话:
telnet 0.0.0.0 4444
答案 1 :(得分:0)
如果您知道将要进入调试器,则可以使用run
而不是up
。
$ docker-compose run --rm --service-ports django_web
问题在于up
假设它会运行多个服务,即使您告诉它仅运行一个服务,也因此会为您包装它们。这就是为什么在输出之前加上服务名称的原因:
web_1 | Some output
db_1 | Some db logs
run
命令doesn't do this,因此您可以拥有一个shell和一个调试器,而不会出现问题或remote_pdb解决方法。
注意:使用run
时,您必须configure the dependencies,因为并非所有内容都会自动启动。