gpt4 book ai didi

python - FastAPI 应用程序在本地运行但不在 Docker 容器中

转载 作者:行者123 更新时间:2023-12-02 19:48:12 24 4
gpt4 key购买 nike

我有一个 FastAPI 应用程序,在本地运行时按预期工作,但是,当我尝试在 Docker 容器中运行时出现“内部服务器错误”。这是我的应用程序的代码:

from fastapi import FastAPI
from pydantic import BaseModel
import pandas as pd
from fbprophet import Prophet

class Data(BaseModel):
length: int
ds: list
y: list
model: str
changepoint: float = 0.5
daily: bool = False
weekly: bool = False
annual: bool = False
upper: float = None
lower: float = 0.0
national_holidays: str = None

app = FastAPI()

@app.post("/predict/")
async def create_item(data: Data):

# Create df from base model
df = pd.DataFrame(list(zip(data.ds, data.y)), columns =['ds', 'y'])

# Add the cap and floor to df for logistic model
if data.model == "logistic":
df['y'] = 10 - df['y']
df['cap'] = data.upper
df['floor'] = data.lower

# make basic prediction
m = Prophet(growth=data.model,
changepoint_prior_scale=data.changepoint,
weekly_seasonality=data.weekly,
daily_seasonality=data.daily,
yearly_seasonality=data.annual
)

# Add national holidays
if data.national_holidays is not None:
m.add_country_holidays(country_name=data.national_holidays)

# Fit data frame
m.fit(df)

# Create data frame for future
future = m.make_future_dataframe(periods=data.length)

# Add the cap and floor to future for logistic model
if data.model == "logistic":
future['cap'] = 6
future['floor'] = 1.5

# forecast
forecast = m.predict(future)

# Print values
print(list(forecast[['ds']].values))

# Return results
# {'ds': forecast[['ds']], 'yhat': forecast[['yhat']], 'yhat_lower': forecast[['yhat_lower']], 'yhat_upper': forecast[['yhat_upper']] }
return [forecast[['ds']], forecast[['yhat']], forecast[['yhat_lower']], forecast[['yhat_upper']]]

uvicorn main:app 在本地合作,但不是当我使用这个 Dockerfile 构建时:
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
COPY ./app /app
RUN pip install -r requirements.txt

并从
docker run -d --name mycontainer -p 8000:80 myimage

我看到 Internal Server Error在 postman 。我的 dockerfile 或 docker 命令有问题吗?否则我该如何调试?

最佳答案

这里有许多可能的问题。

  • 在容器中运行时,您需要使用选项 --host 0.0.0.0 告诉 Uvicorn 不要关心传入的主机 IP。
  • 您没有指定镜像运行时执行的命令。最好在 dockerfile 中说明这一点,例如CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
  • 确保在运行镜像时正确映射端口。根据您给出的内容,您期望容器中的服务正在监听端口 80,这没关系,但 uvicorn 的默认值为 8000(有关显式设置端口,请参见第 2 点)。
  • 关于python - FastAPI 应用程序在本地运行但不在 Docker 容器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62351462/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com