简体   繁体   中英

FastAPI app running locally but not in Docker container

I have a FastAPI app that is working as expected when running locally, however, I get an 'Internal Server Error' when I try to run in a Docker container. Here's the code for my app:

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']]]

Which is working locally with uvicorn main:app , but not when I build using this Dockerfile:

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
COPY ./app /app
RUN pip install -r requirements.txt

and start with

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

I'm seeing Internal Server Error in Postman. Is there something wrong with my dockerfile or docker commands? Or else how do I debug this?

Run the docker without the -d parameter and you'll get more clues about it. If I were to guess, I might say that you're missing some python requirement.

There are a number of possible issues here.

  1. When running in a container, you need to tell Uvicorn to not care about the incoming host IP with option --host 0.0.0.0

  2. You have not specified the command executed when the image runs. Best to make this clear in the dockerfile eg CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

  3. Make sure when you run the image you map the ports correctly. From what you have given you are expecting the service in the container to be listening on port 80, that is OK but the default for uvicorn is 8000 (see point 2 for explicitly setting port).

got same problem coz --port 8000 was missing in

command: uvicorn main:app --host 0.0.0.0 --port 8000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM