Deploying with Docker
Introduction
Docker is a popular platform used to develop, ship, and run applications inside lightweight containers. In the context of machine learning, Docker can be extremely useful for deploying models in a consistent and reproducible manner. This tutorial will guide you through the process of deploying a machine learning model using Docker.
Prerequisites
Before you start, make sure you have the following installed:
- Docker: You can download and install Docker from here.
- A machine learning model: We will assume you already have a trained model saved as a file (e.g., model.pkl).
Step 1: Create a Dockerfile
A Dockerfile is a text document that contains all the commands to assemble an image. Create a file named Dockerfile in your project directory with the following content:
FROM python:3.8-slim
# Set the working directory
WORKDIR /app
# Copy the requirements file
COPY requirements.txt .
# Install the dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the application code
COPY . .
# Run the application
CMD ["python", "app.py"]
This Dockerfile uses the official Python 3.8 image, installs the dependencies listed in requirements.txt, copies the application code, and finally runs the application using app.py.
Step 2: Create a Requirements File
Create a requirements.txt file to list all the dependencies required by your application. For example:
flask
pandas
scikit-learn
Make sure to include all libraries needed for your machine learning model and the web application.
Step 3: Create the Application Code
Let's create a simple Flask application to serve the machine learning model. Save the following code in a file named app.py:
import pickle
from flask import Flask, request, jsonify
app = Flask(__name__)
# Load the machine learning model
with open("model.pkl", "rb") as f:
model = pickle.load(f)
@app.route("/predict", methods=["POST"])
def predict():
data = request.json
prediction = model.predict([data["features"]])
return jsonify({"prediction": prediction.tolist()})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
This Flask application exposes a single endpoint /predict that accepts POST requests with JSON data containing the features for prediction.
Step 4: Build the Docker Image
Navigate to your project directory and build the Docker image using the following command:
docker build -t my-ml-model .
This command builds a Docker image named my-ml-model using the Dockerfile in the current directory.
Sending build context to Docker daemon 4.096kB Step 1/6 : FROM python:3.8-slim ---> f7a292bbb70c ... Successfully built d1f55d3e3a1b Successfully tagged my-ml-model:latest
Step 5: Run the Docker Container
Run the Docker container using the following command:
docker run -p 5000:5000 my-ml-model
This command maps port 5000 of the container to port 5000 on your host machine, allowing you to access the Flask application at http://localhost:5000.
* Serving Flask app "app" (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off * Running on all addresses. WARNING: This is a development server. Do not use it in a production deployment. * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
Step 6: Test the Deployment
To test the deployment, you can use a tool like Postman or curl to send a POST request to the /predict endpoint. For example:
curl -X POST http://localhost:5000/predict -H "Content-Type: application/json" -d '{"features": [5.1, 3.5, 1.4, 0.2]}'
You should receive a JSON response with the model's prediction.
{"prediction": [0]}
Conclusion
In this tutorial, we've covered the process of deploying a machine learning model using Docker. We created a Dockerfile, built a Docker image, and ran a containerized Flask application to serve the model. Docker simplifies the deployment process by ensuring consistency across different environments. Happy deploying!
