Web Services: Deploying Models with Flask and Docker

Web Services: Deploying Models with Flask and Docker

We use a small Flask web service for the first deployment. It loads the model artifacts created during training, accepts a ride record, and returns a predicted duration.

Load the artifacts

The example stores the dictionary vectorizer and the linear regression model as pickle files. Load them when the service starts rather than for every request. This keeps request handling focused on feature preparation and prediction.

The model file loaded by the service code

The service should expose a narrow endpoint. A request contains the fields needed by the feature function, and the response contains a JSON prediction. Keep the input schema and output shape documented so clients can test the service without reading its implementation.

Run the service

Flask can serve the application during development. Use a WSGI server such as Gunicorn for a production-style process.

Bind the server to an address reachable from the container:

gunicorn --bind=0.0.0.0:9696 predict:app

Test the endpoint with a representative request and verify both the status code and the prediction value. The code in web-service includes the application and a small test client.

The service running behind Gunicorn

Package it with Docker

The container needs the correct Python base image, the dependency lock file, the model artifacts, and the application code.

Use a relative path inside the image:

FROM python:3.9-slim
WORKDIR /app
COPY ["Pipfile", "Pipfile.lock", "predict.py", "lin_reg.bin", "./"]
RUN pip install pipenv && pipenv install --system --deploy
CMD ["gunicorn", "--bind=0.0.0.0:9696", "predict:app"]

Build and run the image:

docker build -t ride-duration-model .
docker run --rm -p 9696:9696 ride-duration-model

The containerized service and its runtime dependencies

Optional analytics

Optional analytics are off unless you allow them. Your choice does not affect access to DataTalks.Club, Slack, events, or courses.

Current choice: not chosen.

Privacy Policy

If you allow analytics, self-hosted Umami counts anonymous public pageviews without analytics cookies. Private account and learning pages are excluded. Read the Privacy Policy for the complete data boundary.