如何用vscode在remote docker container 内 debug python

Prerequisites:

local host(win10)

  • vscode
  • vscode extension:
    • remote-SSH
    • remote-container
    • docker
  • docker desktop

remote host(Ubuntu)

  • docker

Procedures

  • 先在local host机器上安装好vscode和各个extension

  • 用remote-SSH extension ssh连接到remote host(Ubuntu)上的代码文件夹, 比如~/develop/test_project

  • 在remote host(Ubuntu)上启动container

  • 打开vscode docker extension:

    image.png

  • 在弹出的container列表中选择自己想要连接的container, 右键 -> Attach to vscode

  • 则会直接打开一个新的vscode, terminal会自动连接到container里

  • ctrl+P, 在顶部的栏里输入>Python: Configure Tests

  • 在顶部的command栏里选择pytest -> 文件夹 tests

  • 此时 Testing 工具会刷新出pytest文件:

    image.png

  • 设置好pytest中的断点

  • 点击瓢虫小图标/右键debug test 就可以顺利debug了:

    image.png

  • 图:

    image.png

  • 如果需要测试django启动, 直接在debug配置选择django即可:

    image.png

ref: https://code.visualstudio.com/docs/python/testing

你可能感兴趣的:(如何用vscode在remote docker container 内 debug python)