/web-to-ai-ml-integrations

Deploy ML Models with Docker and Flask

Deploy ML models with Docker and Flask. Step-by-step guide for containerizing your ML app and launching scalable APIs.

Book a free  consultation
4.9
Clutch rating 🌟
600+
Happy partners
17+
Countries served
190+
Team members
Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

Deploy ML Models with Docker and Flask

Introduction to Deploying ML Models with Docker and Flask

 
  • Description: This guide explains how to integrate a machine learning model with a Flask web application and containerize the complete solution using Docker. The ML model is served via an API, making it accessible in any environment that supports Docker.
  • Key Components:
    • Flask: A lightweight Python web framework used to create RESTful APIs.
    • Docker: A tool for packaging an application and its dependencies into a container that can run reliably in different computing environments.
    • ML Model: For demonstration, a pre-trained model stored as a serialized file (e.g., model.pkl) is used to make predictions.

Creating the Flask Application for ML Inference

 
  • Flask App Structure: The primary file (e.g., app.py) will load the ML model and define endpoints for predictions.
  • Model Loading: Use Python libraries such as pickle to deserialize the trained model.
  • API Endpoint: Define a POST route that accepts input data, performs inference, and returns the prediction result.

// Import necessary modules
from flask import Flask, request, jsonify   // Flask framework for API creation
import pickle                                   // To load the ML model
import numpy as np                              // For numerical operations

// Initialize the Flask application
app = Flask(**name**)

// Load the pre-trained ML model
with open('model.pkl', 'rb') as model\_file:
    model = pickle.load(model\_file)             // Model deserialization

// Define an API route for prediction
@app.route('/predict', methods=['POST'])
def predict():
    // Retrieve JSON data from the request
    input_data = request.get_json()             // Input should be a dictionary containing features
    
    // Convert input data to a suitable format (e.g., NumPy array)
    features = np.array(input\_data['data']).reshape(1, -1)  // Adjust shape based on model input requirements
    
    // Make prediction using the loaded model
    prediction = model.predict(features)          // Use the model's predict method
    
    // Return the prediction as a JSON response
    return jsonify({'prediction': prediction.tolist()})

// Start the Flask application when the container is executed
if **name** == '**main**':
    // Set host to '0.0.0.0' to make the app externally visible
    app.run(host='0.0.0.0', port=5000, debug=True)

Containerizing the Flask Application with Docker

 
  • Dockerfile Creation: Write a Dockerfile that describes how to build an image for the Flask app. This file instructs Docker on what base image to use, what dependencies to install, and how to run the application.
  • Multi-stage or Single-stage: For simplicity, a single-stage Dockerfile is used. In production, multi-stage builds help minimize image size by separating build-time and runtime requirements.

// Use an official Python runtime as a parent image
FROM python:3.9

// Set environment variables to prevent Python from writing pyc files and buffering stdout/stderr
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

// Set a working directory inside the container
WORKDIR /app

// Copy the requirements file to the container
COPY requirements.txt /app/requirements.txt

// Install any needed packages
RUN pip install --upgrade pip && pip install -r requirements.txt

// Copy the rest of the application code into the container
COPY . /app

// Expose port 5000, so it is accessible outside the container
EXPOSE 5000

// Define the command to run the Flask app when the container starts
CMD ["python", "app.py"]
  • Explanation:
    • FROM python:3.9: Uses Python 3.9 as the base image.
    • WORKDIR /app: Sets the working directory inside the container to /app.
    • COPY: Transfers necessary files into the container.
    • RUN pip install: Installs all required Python packages listed in requirements.txt.
    • EXPOSE 5000: Informs Docker that the container listens on port 5000.
    • CMD: Specifies the command that starts the Flask application.

Building and Running the Docker Container

 
  • Image Building: Execute the Docker build command to create an image from the Dockerfile. This image will include the Flask app and its dependencies.
  • Container Execution: Run the Docker container while mapping the container's port to the host's port to allow external access.

// From the directory containing the Dockerfile, run:
docker build -t ml-flask-app:latest   // Build the Docker image and tag it as ml-flask-app

// Run the Docker container and map port 5000 of the container to port 5000 on the host machine
docker run -p 5000:5000 ml-flask-app:latest
  • Accessing the Application:
    • After the container is running, the Flask app is accessible via http://localhost:5000.
    • The /predict endpoint can be called with appropriate JSON data to get model predictions.

Integrating and Testing the Deployed Model

 
  • API Testing: Use tools like cURL, Postman, or even a simple Python script to send POST requests and verify the predictions.

// Example Python script for testing the /predict endpoint

import requests
import json

// Define the API URL
url = "http://localhost:5000/predict"

// Example input data matching the expected model features
data = {"data": [5.1, 3.5, 1.4, 0.2]}   // Adjust values based on your model's requirements

// Send a POST request with JSON data
response = requests.post(url, json=data)

// Print the JSON response from the API
print(response.json())
  • Troubleshooting:
    • If the container does not start properly, check the container logs using docker logs <container\_id>.
    • Ensure that all file paths in the Dockerfile and Flask app match the actual project layout.
    • Adjust the CORS configuration if the API is accessed from a browser.

Conclusion: Best Practices and Further Enhancements

 
  • Best Practices:
    • Keep your Docker images lightweight by minimizing the number of dependencies.
    • Implement proper error handling in your Flask routes to manage unexpected input or server errors.
    • For production, consider setting up a reverse proxy like Nginx in front of your Flask app for better performance and security.
  • Further Enhancements:
    • Consider using Docker Compose if your application requires additional services like a database.
    • Integrate logging and monitoring tools to keep track of API usage and performance within the containerized environment.
    • Secure your API endpoints with authentication mechanisms if needed.


Recognized by the best

Trusted by 600+ businesses globally

From startups to enterprises and everything in between, see for yourself our incredible impact.

RapidDev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with.

They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

Arkady
CPO, Praction
Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost.

He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Donald Muir
Co-Founder, Arc
RapidDev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space.

They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Mat Westergreen-Thorne
Co-CEO, Grantify
RapidDev is an excellent developer for custom-code solutions.

We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Emmanuel Brown
Co-Founder, Church Real Estate Marketplace
Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 

This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Samantha Fekete
Production Manager, Media Production Company
The pSEO strategy executed by RapidDev is clearly driving meaningful results.

Working with RapidDev has delivered measurable, year-over-year growth. Comparing the same period, clicks increased by 129%, impressions grew by 196%, and average position improved by 14.6%. Most importantly, qualified contact form submissions rose 350%, excluding spam.

Appreciation as well to Matt Graham for championing the collaboration!

Michael W. Hammond
Principal Owner, OCD Tech

We put the rapid in RapidDev

Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We’ll discuss your project and provide a custom quote at no cost.Â