合理主義的グルメブログ

学生起業家の日常をツラツラと書いています。主に食事情報です。

VSCodeとDockerを使って,リモートデバッグしてみた

さて,VSCodeになれたので, ついにリモートデバッグをしてみたいと思います.

基本的には,このDonJayamanneさんのコードをもとに作業しました.

github.com

状況的には,dockerコンテナ内のpythonプログラムに対して,リモートデバッグしたいです.

とりあえず,フォークして実験してみました.

同じPC上で動かした場合

windowsの場合

以下のようなエラーが出ました.
いろいろ調べましたが,原因がわからなかったです.
f:id:GoKIDS:20181017192603p:plain

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.jsonremoteRootを共有フォルダ先に変えておくと,ハッピーになれます.

{
    // 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に従えばできます.

まずは,デバッグメニューをクリック!
f:id:GoKIDS:20181018084708p:plain

次に,赤で囲んだところから,Attach (Remote Debug)を選ぶ(launch.jsonnameによって変わる).
っで,右側の実行ボタンを押せば,実行される.
f:id:GoKIDS:20181018084829p:plain

実行画面はこんな感じ
f:id:GoKIDS:20181018084937p:plain