LoginSignup
4
3

More than 1 year has passed since last update.

Python: DockerでFastAPIによるRESTAPI環境を起動するサンプル一式

Posted at

FastAPIをDockerで実行するテンプレート一式です。
PythonとFastAPIを使ったRESTAPIサーバがすぐに試せます。
また、pytestを使ってAPIのテストコードをDockerで実行するサンプルも付いています。

必要環境

Windows10, Mac, Linux などに Docker + docker-composeとGitをインストールしてください。


Windows10

WSL2でUbuntuなどのLinux仮想環境を作って、そこにDocker + docker-composeをインストールするのがおすすめです。


Mac

Docker Desktopなどをインストールしてください。


FastAPI Dockerコンテナの起動

WindowsでWSL2を使用する場合

まず、スタートメニューなどから「Ubuntu 20.04 on Windows」などをクリックしてWSL2を起動してください。
WSL2内のLinuxシェル画面が起動したら、それを使ってこの後の作業を続けてください。

この一式を、Gitを使ってダウンロードします

git clone https://github.com/yagrush/myfastapi.git

ディレクトリの中に入ります

cd myfastapi

FastAPIコンテナを起動しましょう

docker-compose up --build -d

APIを叩く前にまずは、APIドキュメントにアクセスしてみます

ブラウザで↓を開きます。
http://localhost:8000/docs

  • FastAPIは自動的にAPIドキュメントを生成してくれます。
    FastAPIを起動中に利用することができます。
    ここからAPIを実際に実行することも可能です。

api_docs.png

APIをためしに実行してみます

  • root
    GET /
    を実行してみましょう。
    これはURLでいうところの http://localhost:8000/ です。

api_test.png

api_test2.png

api_test3.png

このAPIは、固定のJSON

{
  "message": "Hello World"
}

を返すAPIですが、その通りちゃんと返ってきたようです。

ためしに、ブラウザからもアクセスしてみます

↓をブラウザで開きます。
http://localhost:8000/

api_test4.png

無事、

{
  "message": "Hello World"
}

が返ってきました。

FastAPI Dockerコンテナの終了

終了したいときは、docker-compose.ymlがあるディレクトリで↓のコマンドを実行します。

docker-compose down

pytestをDockerで実行する

pytestは、ソースコードの健全性をテストするソースコードを実行するプログラムです。

これもDockerコンテナとして実行してみましょう。
そうすれば、手元のMacやWindowsやWSLにPythonやpytestをインストールする必要が無くなります。
今回は、Dockerとpytest設定ファイルをご用意してありますので、

  • pytest-docker-compose.yml
  • pytest.Dockerfile
  • src/pytest.ini

早速実行してみましょう。
実行すると、src/pytest.ini に書いてある通りにテストコードを探して自動的に実行してくれます。

今回のプロジェクトには、テストコード src/tests/api/v1/test_user.py があります。

内容は、API /v1/user/abcde を実行して、期待通りのレスポンス

{
  "user_name": "abcde 一郎さん",
  "age": 12
}

が返ってくるかどうかをチェックするテストコードです。

from fastapi.testclient import TestClient
from prog import app

client = TestClient(app, base_url="http://localhost:8100")


def test_get():
    user_id = "abcde"
    res = client.get(f"/v1/user/{user_id}")
    assert res.status_code == 200
    assert res.json()["user_name"] == f"{user_id} 一郎さん"
    assert res.json()["age"] == 12

pytestDockerコンテナを起動します。

$ docker-compose -f  pytest-docker-compose.yml up

(このコンテナは、pytestが完了次第終了します。)

↓ このように表示されればOKです。

WARNING: Found orphan containers (myfastapi) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
Starting myfastapi-pytest ... done
Attaching to myfastapi-pytest
myfastapi-pytest | ============================= test session starts ==============================
myfastapi-pytest | platform linux -- Python 3.10.4, pytest-7.1.1, pluggy-1.0.0 -- /usr/local/bin/python
myfastapi-pytest | cachedir: .pytest_cache
myfastapi-pytest | rootdir: /src, configfile: pytest.ini, testpaths: ./tests
myfastapi-pytest | plugins: anyio-3.5.0
myfastapi-pytest | collecting ... collected 1 item
myfastapi-pytest |
myfastapi-pytest | tests/api/v1/test_user.py::test_get API:v1/user/abcde 所要時間:0.0003070831298828125秒
myfastapi-pytest | PASSED
myfastapi-pytest |
myfastapi-pytest | ============================== 1 passed in 0.15s ===============================
myfastapi-pytest exited with code 0
4
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
4
3