Step-by-Step Guide to Deploying Machine Learning Models with FastAPI and Docker

Step-by-Step Guide to Deploying Machine Learning Models with FastAPI and Docker

Step-by-Step Guide to Deploying Machine Learning Models with FastAPI and Docker
Image by Editor | Midjourney

You’ve trained your machine learning model, and it’s performing great on test data. But here’s the truth: a model sitting in a Jupyter notebook isn’t helping anyone. It’s only when you deploy it to production real users can benefit from your work.

In this article we’re building a diabetes progression predictor on a sample dataset from scikit-learn. We’ll take it from raw data all the way to a containerized API that’s ready for the cloud.

By coding along to this tutorial, you’ll have:

  • A trained Random Forest model that predicts diabetes progression scores
  • A REST API, built using FastAPI, that accepts patient data and returns predictions
  • A fully containerized application ready for deployment

Let’s get started.

🔗 Link to the code on GitHub.

Setting Up Your Development Environment

Before we start coding, let’s get your dev environment  ready. You’ll need:

  • Python 3.11+ (though 3.9+ works fine, too)
  • Docker installed and running
  • Basic familiarity with Python and APIs (I’ll explain the non-trivial parts)

Project Structure

Here’s how we’ll organize everything in the project directory:

Installing Dependencies

Let’s create a clean virtual environment:

Now install the required libraries:

Building a Machine Learning Model for Predicting Diabetes Progression

Let’s start by creating our machine learning model. Create train_model.py:

We’ve chosen Random Forest because it’s robust, handles different feature scales well, and gives us feature importance insights as well.

Let’s load and explore our diabetes dataset:

The diabetes dataset is a collection of 442 patient records with 10 physiological features. The target is a quantitative measure of disease progression one year after baseline: higher numbers indicate more advanced progression.

Output:

Now let’s prepare our data:

The 80/20 split gives us enough training data while reserving a solid test set. Using random_state=42 ensures reproducible results.

Output:

Time to train our model:

We’ve set max_depth=10 to prevent overfitting on this relatively small dataset. With 100 trees, we get good performance without excessive computation time.

Let’s evaluate our model:

The R² score tells us what percentage of variance in disease progression our model explains. Anything above 0.4 is pretty good for this dataset!

Output:

Finally, let’s save our trained model:

Run this script to train your model:

You should see output showing your model’s performance and confirmation that it’s been saved.

Creating the FastAPI Application

Now for the exciting part: turning our model into a web API.

If you haven’t already, create the app directory and an empty __init__.py file:

Now create app/main.py with our API code:

FastAPI uses Pydantic for request validation. Meaning it automatically validates incoming data and provides clear error messages if something’s wrong.

Let’s define our input data structure:

The example values help API users understand the expected input format. Note that the diabetes dataset features are already normalized.

Next, we initialize FastAPI app and load the model into the FastAPI environment:

Finally, let’s create our prediction endpoint:

The interpretation function helps make our API more user-friendly by providing context for the numerical predictions.

Let’s also add a health check endpoint:

Testing the API Locally

Before containerizing, let’s test our API locally. Run the following command from your project’s root directory:

Open your browser to http://localhost:8000/ and you’ll see the FastAPI app running. Try making a prediction with the example data.

You can also test with curl:

This should give you the following result:

Containerizing with Docker

Now let’s package everything into a Docker container. First, create requirements.txt:

We’ve pinned specific versions to ensure consistency across environments.

Now create the Dockerfile:

The slim image keeps our container small, and --no-cache-dir prevents pip from storing cached packages, further reducing size.

Build your Docker image:

Run the container:

Your API is now running in a container! Test it the same way as before.

Publishing to Docker Hub

Now that your containerized API is working locally, let’s share it with the world through Docker Hub. This step is necessary for cloud deployment. Most cloud platforms can pull directly from Docker Hub, making deployment seamless.

Setting Up Docker Hub

First, you’ll need a Docker Hub account if you don’t have one:

  1. Go to hub.docker.com and sign up
  2. Choose a username you’re happy with. It’ll be part of your image URLs

Logging Into Docker Hub

From your terminal, log into Docker Hub:

You’ll be prompted for your Docker Hub username and password. Enter them carefully. This creates an authentication token that lets you push images.

Tagging Your Image

Before pushing, we need to tag our image with your Docker Hub username. Docker uses a specific naming convention:

Replace your-username with your actual Docker Hub username. The v1.0 is a version tag. It’s good practice to version your images so you can track changes and roll back if needed.

Let’s also create a latest tag, which many deployment platforms use by default:

Check your tagged images:

You should see three entries: your original image and the two newly tagged versions.

Pushing to Docker Hub

Now let’s push your image to Docker Hub:

The first push might take a few minutes as Docker uploads all the layers. Subsequent pushes should be substantially faster.

You can verify everything works by pulling and running your published image:

Test the API again to make sure everything still works. If it does, your model is now publicly available and ready for cloud deployment.

Wrapping Up

Congratulations! You’ve just built a complete machine learning deployment pipeline:

  • Trained a robust Random Forest model on medical data
  • Created a working REST API with FastAPI
  • Containerized the application with Docker

Your model is now ready for cloud deployment! You could deploy this to AWS ECS, Fargate, Google Cloud, or Azure.

Want to take it further? You can consider adding the following:

  • Authentication and rate limiting
  • Model monitoring and logging
  • Batch prediction endpoints

You now have all the basics to deploy any machine learning model to production. Happy coding!

One Response to Step-by-Step Guide to Deploying Machine Learning Models with FastAPI and Docker

  1. Temitope June 15, 2025 at 11:48 am #

    Impressive 👏
    Thank you for this

Leave a Reply

Machine Learning Mastery is part of Guiding Tech Media, a leading digital media publisher focused on helping people figure out technology. Visit our corporate website to learn more about our mission and team.