VSCodeとDockerを使って,リモートデバッグしてみた
さて,VSCodeになれたので, ついにリモートデバッグをしてみたいと思います.
基本的には,このDonJayamanneさんのコードをもとに作業しました.
状況的には,dockerコンテナ内のpythonプログラムに対して,リモートデバッグしたいです.
とりあえず,フォークして実験してみました.
同じPC上で動かした場合
windowsの場合
以下のようなエラーが出ました.
いろいろ調べましたが,原因がわからなかったです.
Macの場合
README通りに進めて動きました.
サーバー上のDockerコンテナで,リモートデバッグ
launch.json
ファイルのhost
を変えるだけで簡単にできました.
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Attach (Remote Debug)", "type": "python", "request": "attach", "port": 3000, "host": "ip_address", "pathMappings": [{ "localRoot": "${workspaceFolder}", "remoteRoot": "/src/" }], "redirectOutput": false } ] }
共有フォルダにして実行
Dockerのコンテナ生成時に共有フォルダ設定にして,デバッグを行ってみました.
この時にlaunch.json
のremoteRoot
を共有フォルダ先に変えておくと,ハッピーになれます.
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Attach (Remote Debug)", "type": "python", "request": "attach", "port": 3000, "host": "ip_address", "pathMappings": [{ "localRoot": "${workspaceFolder}", "remoteRoot": "/root/src/" }], "redirectOutput": false } ] }
この時の,docker run
は,こんな感じです.
docker run -it --rm \ -p 3000:3000 \ -v ${PWD}:/root/work \ -w /root/work \ remote-debugging-docker \ python sample.py
デバッグ操作
最後に自分用のメモとして,VSCodeの方でどのようにデバッグを行うかを書いておきます.
基本的には,DonJayamanneさんのREADME.mdに従えばできます.
まずは,デバッグメニューをクリック!
次に,赤で囲んだところから,Attach (Remote Debug)
を選ぶ(launch.json
のname
によって変わる).
っで,右側の実行ボタンを押せば,実行される.
実行画面はこんな感じ