简体   繁体   English

Python Docker远程调试VS代码

[英]Python Docker Remote Debugging VS Code

trying to get remote debugging for my python flask API I have. 试图为我的python flask API进行远程调试。 I'm able to docker-compose up and have postman successfully call the running container, but when I try to attach the debugger, it never compiles. 我能够使用docker-compose并让postman成功调用正在运行的容器,但是当我尝试连接调试器时,它从不编译。 Below is my yml, dockerfile and vscode launch settings... the following error I get is: 下面是我的yml,dockerfile和vscode启动设置......我得到的以下错误是:

There was an error in starting the debug server. 启动调试服务器时出错。 Error = {"code":"ECONNREFUSED","errno":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":5050} 错误= {“代码”:“ECONNREFUSED”,“errno”:“ECONNREFUSED”,“系统调用”:“连接”,“地址”:“127.0.0.1”,“端口”:5050}

version: '2'

services:
  website:
    build: .
    command: >
      python ./nomz/app.py
    environment:
      PYTHONUNBUFFERED: 'true'
    volumes:
      - '.:/nomz'
    ports:
      - '5000:5000'
      - '5050'

DockerFile DockerFile

FROM python:3.6-slim

ENV INSTALL_PATH /nomz
RUN mkdir -p $INSTALL_PATH

WORKDIR $INSTALL_PATH

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

COPY . .

EXPOSE 5000 5050

VSCode Launch Settings VSCode启动设置

{
  "name": "Python: Attach",
  "type": "python",
  "request": "attach",
  "localRoot": "${workspaceFolder}/nomz/app.py",
  "remoteRoot": "/nomz/",
  "port": 5050,
  "host": "localhost"
}

I finally got it working with remote debugging. 我终于让它与远程调试一起工作了。 I had to pip3 install ptvsd==3.0.0 on my local, and make sure that the requirements.txt for my docker container had the same version. 我不得不在我的本地pip3安装ptvsd == 3.0.0,并确保我的docker容器的requirements.txt具有相同的版本。 (note: the latest version 3.2.1 didn't work) (注意:最新版本3.2.1不起作用)

@BrettCannon had the right link for a good tutorial https://code.visualstudio.com/docs/python/debugging#_remote-debugging @BrettCannon有一个很好的教程的正确链接https://code.visualstudio.com/docs/python/debugging#_remote-debugging

What I had to do was add some code to the app.py of the flask app. 我必须做的是在烧瓶应用程序的app.py中添加一些代码。 I originally was getting address already in use error when starting the container, so I added the socket code and after the first successful attach of debugger I didn't seem to need it anymore (strange I know, but that's why I left it in in case someone else gets that error) 我最初在启动容器时得到了地址已经在使用中的错误,所以我添加了套接字代码,在第一次成功连接调试器之后我似乎不再需要它了(奇怪我知道,但这就是为什么我把它留在了案件别人得到那个错误)

try:
    import ptvsd
    # import socket
    # sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # sock.close()
    ptvsd.enable_attach(secret=None,address=('0.0.0.0',5050))
    ptvsd.wait_for_attach()
except Exception as ex:
    print('Not working: ')

also i took the debug kwarg off the app.run() in app.py for the flask app. 我也把调试kwarg从app.py中的app.run()中取出了烧瓶app。 This all gave me the ability to connect the debugger, but the breakpoints where "Unverified", so the last thing that had to happen was a path to the app.py in the launch.json for the remoteRoot. 这一切都让我能够连接调试器,但断点是“未验证”,因此最后必须发生的是remote.Root的launch.json中的app.py路径。 I will say I created a small test api to get this working, and it only need the first level of pathing (ie. /app and not /app/app/app.py)Here is a github of the test api I made ( https://github.com/tomParty/docker_python ). 我会说我创建了一个小的测试api来使这个工作,它只需要第一级路径(即。/ app而不是/app/app/app.py)这里是我做的测试api的github( https://github.com/tomParty/docker_python )。 So if the debugger is attaching, but your breakpoints are unverified, play around with the path of the remoteRoot 因此,如果调试器正在附加,但您的断点未经验证,请使用remoteRoot的路径

"remoteRoot": "/nomz/nomz/app.py"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM