Containerizing and Deploying a Machine Learning Model
Deploying machine learning models in Docker containers simplifies the development, deployment, and management processes, offering benefits such as isolation, portability, scalability and efficient resource utilization.
In this section it is detailed how to containerize and deploy a machine learning model with Docker, using scikit-learn, a simple and efficient tool for predictive data analysis.
This will ensure seamless and efficient integration into production environments.
Step 1. Create a project directory
In your eManager use the following commands to create a project directory:
Step 2. Create the machine learning application script
Create the machine learning application script that we will run in our Docker container.
For this example, a script with a machine learning model that trains Iris dataset and make some predictions, is used.
Open a new file to implement the script:
Copy the following content:
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import joblib
# Load dataset
iris = datasets.load_iris()
X = iris.data
y = iris.target
# Split dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
# Create a Gaussian Classifier
clf = RandomForestClassifier()
# Train the model using the training sets
clf.fit(X_train, y_train)
# Predict the response for test dataset
y_pred = clf.predict(X_test)
# Model Accuracy, how often is the classifier correct?
print(f"Accuracy: {accuracy_score(y_test, y_pred)}")
# Save the trained model
joblib.dump(clf, 'iris_model.pkl')
print("Model saved!")
Step 3. Create a Dockerfile
In the same directory, create a Dockerfile.
Open a new Dockerfile:
Copy the following content:
FROM debian:12-slim
# Set the working directory in the container
WORKDIR /usr/src/app
# Copy the current directory contents into the container at /usr/src/app
COPY . .
RUN apt-get update -q
RUN DEBIAN_FRONTEND='noninteractive' apt-get install -y --no-install-recommends python3-sklearn python3-sklearn-lib python3-joblib
# Run app.py when the container launches
CMD ["python3", "./app.py"]
In this Dockerfile a debian Docker image is being pulled and used to install scickit-learn library and execute the machine learning script.
Step 4. Build the Docker image
Run the following command in your working directory to build the Docker image:
This may take several minutes.
Step 5. Run the Docker container
Once the image is built, run your application in a Docker container:
Once the image is running properly in the container you should see the accuracy of the model outputted, as shown below.