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

时间:2020-06-12 19:47:47

标签: python docker fastapi facebook-prophet

我有一个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。我的dockerfile或docker命令有问题吗?否则我该如何调试呢?

1 个答案:

答案 0 :(得分:0)

在不使用-d参数的情况下运行docker,您将获得更多线索。如果我猜到了,我可能会说您缺少一些python要求。