LoginSignup
1
3

More than 3 years have passed since last update.

PyCharmでFastAPI (on Docker)をステップデバッグする。

Posted at

環境

  • Docker 20.10.5
  • docker-compose 1.28.5
  • PyCharm 2020.3.5 (Professional Edition)

コード

事前準備

コンテナのビルド

$ make build

起動

$ make dev

動作確認

$ $ curl localhost:8000
{"status":"ok"}

停止

$ make down

PyCharm の設定

インタプリタの設定

  • PyCharm > Preferences > Project > Python Interpreter
  • 歯車アイコンをクリックして Add
    1.png

  • Docker Compose を選んで画像のように入力して OK
    2.png

  • フォルダのアイコンをクリックしてパスをマッピングする。
    3.png

  • Local Path にローカルのプロジェクトのパス、 Remote Path には Docker コンテナ内のコードを配置するパスを入力して OK
    4.png

  • Apply して OK
    5.png

デバッグ設定

  • Run > Edit Configurations
  • + をクリックして Python を選択
    6.png

  • 画像の内容で入力して Apply して OK
    7.png

    • Name に適当に名前を入力。今回は api
    • Script path にはローカルのエントリーポイントとなるファイルのパスを入力。
    • Working directory はローカルのプロジェクトルートを入力。

デバッグ実行

  • ブレイクポイントを付ける。
    8.png

  • 実行する対象に api を選択。
    9.png

  • デバッグアイコンをクリックすると、コンテナが起動する
    10.png

  • http://localhost:8000 で api を実行するとブレイクポイントで止まる。
    11.png

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3