/web-to-ai-ml-integrations

Machine Learning Model Deployment with Docker

Deploy your ML model with Docker using our easy step-by-step guide. Achieve scalable, efficient deployment with expert tips.

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

Machine Learning Model Deployment with Docker

Preparing Your Machine Learning Web Service Application

 
  • Develop your inference code: Write a script (for example, app.py) that loads your trained ML model (which could be serialized using pickle, joblib, or any other format) and exposes an API endpoint to accept data and return predictions. In this example, we will use Python with the Flask framework to serve our model.
  • Example code snippet for app.py:

// Import essential libraries
from flask import Flask, request, jsonify
import pickle // For model deserialization

app = Flask(**name**)

// Load the pre-trained ML model from disk
with open('model.pkl', 'rb') as file:
    model = pickle.load(file)

// Define an API endpoint for predictions
@app.route('/predict', methods=['POST'])
def predict():
    data = request.get\_json() // Expecting JSON data
    features = data.get('features')
    prediction = model.predict([features])
    return jsonify({'prediction': prediction.tolist()})

// Run the server on specified host and port
if **name** == '**main**':
    app.run(host='0.0.0.0', port=5000)
  • This code defines a simple API: it loads the ML model on startup, waits for POST requests with feature data at the /predict endpoint, and returns the prediction in JSON format.

 

Constructing a Dockerfile for Containerization

 
  • Base Image Selection: Choose an appropriate base image that has Python installed. The official Python image is a popular choice.
  • Copy and Install Dependencies: Copy your application code as well as the ML model file (e.g. model.pkl) into the container, and install the dependencies listed in a requirements.txt file.
  • Dockerfile Explanation: The Dockerfile describes the steps needed to package your application, system libraries (if any), and your ML model into an isolated container. This ensures that the application works the same way on all systems.

// Use an official lightweight Python image as the base image
FROM python:3.9-slim

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

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

// Install the Python dependencies
RUN pip install --upgrade pip && pip install -r requirements.txt

// Copy the rest of the application code and model
COPY . /app

// Expose the container's port where the application runs
EXPOSE 5000

// Command to run the application
CMD ["python", "app.py"]
  • Note: The EXPOSE instruction informs Docker that the container listens on the specified network port at runtime.

 

Writing the Requirements File

 
  • Example requirements.txt:

// Flask for the web API
Flask==2.1.0

// Any additional libraries required for your ML model
numpy==1.21.0
scikit-learn==1.0.2
  • This file should list all dependencies needed by your application, ensuring they are installed in the Docker environment.

 

Building and Running Your Docker Container

 
  • Build the Docker image: Use Docker’s command-line interface to build the image from your Dockerfile. In your terminal, run:

// Navigate to your project directory containing the Dockerfile
cd path/to/your/project

// Build the Docker image with a tag, for example, ml-model-deployment
docker build -t ml-model-deployment .
  • Run the Docker container: Once the Docker image is built, run a container from that image while binding the container’s port to a port on your local machine.

// Run the container and map port 5000 of the container to port 5000 on your host machine
docker run -p 5000:5000 ml-model-deployment
  • This command starts the container and makes your prediction API accessible at http://localhost:5000/predict.

 

Handling Model Versioning and Updates

 
  • Model Versioning: To manage multiple versions of your ML model, consider including a version identifier in the file name (e.g. model\_v1.pkl) and updating your code to refer to the correct version.
  • Cache Busting: When the model file changes, Docker might use cached layers. To prevent this, force a cache-busting step by initially copying the model file separately after installing dependencies.

// Example snippet in your Dockerfile to avoid cache issues:
COPY requirements.txt /app/
RUN pip install --upgrade pip && pip install -r requirements.txt

// Copy model file after dependency installation
COPY model\_v1.pkl /app/model.pkl
COPY app.py /app
  • This ensures the container rebuilds layers related to your application logic when the model file is updated.

 

Testing Your Deployment

 
  • API Testing: Use tools such as curl or Postman to test that the API is working as expected. For example, using curl:

// Command to test the prediction API via curl
curl -X POST http://localhost:5000/predict -H "Content-Type: application/json" -d '{"features": [1.2, 3.4, 5.6]}'
  • This command sends a JSON payload to your deployed application and should return a prediction.

 

Troubleshooting and Optimization Tips

 
  • Logging: Make sure to add sufficient logging in your application to monitor both Docker logs and API behavior. Logs can be viewed using docker logs [container\_id].
  • Resource Monitoring: Run your container with resource limits if needed (using Docker options like --memory and --cpus) to prevent resource exhaustion in production environments.
  • Security Considerations: Avoid running containers as the root user, and set up appropriate user privileges within your Dockerfile. For example, consider adding a non-root user and switching to it using the USER directive.

// Example of adding a non-root user
RUN useradd -ms /bin/bash myuser
USER myuser
  • This enhances container security by minimizing potential vulnerabilities.

 

Deploying to a Container Orchestration Platform

 
  • Kubernetes or Docker Swarm: For production deployments, consider using orchestration platforms like Kubernetes or Docker Swarm. These platforms help manage multiple container instances, load balancing, scaling, and rolling updates.
  • Configuration: Write deployment manifests (YAML files for Kubernetes) that describe how your containerized application should run, specify required resources, and manage service discovery. These configurations ensure that your ML model service can handle production traffic efficiently.
  • You can use health checks and readiness probes in Kubernetes to monitor your application's health, ensuring that the deployed container responding correctly to API requests.

 


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.Â