LoginSignup
7
11

More than 1 year has passed since last update.

Dockerコンテナ内でFastAPIアプリケーションの起動エラーの解決

Posted at

概要

本稿では、Dockerコンテナ内でFastAPIアプリケーションを起動する際に発生したエラーとその解決方法について論じる。アプリケーションがIPv6アドレスでバインドしようとして失敗した時の対処方法を以下に説明する。

本件のエラー

ssh-python | ERROR:    [Errno 99] error while attempting to bind on address ('::1', 8080, 0, 0): 

IPv6アドレスでのバインドエラー:

エラー内容:FastAPIがIPv6アドレス(::1)でポート8080をバインドしようとしたが、そのアドレスが利用できないことを示していた。

解決策:FastAPIがIPv4アドレス(0.0.0.0)でポートをリッスンするように変更する。main.py(またはFastAPIを実行しているファイル)で、uvicorn.run()関数のhost引数を"0.0.0.0"に設定する。この変更により、FastAPIはすべてのIPv4アドレスでリッスンし、Dockerコンテナ内で正常に起動できる。

from fastapi import FastAPI

app = FastAPI()

# ... その他のルートや設定 ...

if __name__ == "__main__":
    uvicorn.run("main:app", host="0.0.0.0", port=8080, reload=True)
7
11
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
7
11