-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-basic
More file actions
228 lines (157 loc) · 5.08 KB
/
Copy pathdocker-basic
File metadata and controls
228 lines (157 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
Let's extend our previous deployment by containerizing the machine learning model using **Docker**.
This approach makes it easier to deploy and scale the application across different environments.
### **Steps to Deploy the ML Model in a Docker Container on Azure VM**
#### **Prerequisites**
- An Azure VM set up (as in the previous guide).
- **Docker** installed on your Azure VM.
- Basic familiarity with Docker and Python.
---
### **1. SSH into the Azure VM**
First, log in to your Azure VM:
```bash
ssh azureuser@<Public-IP-of-VM>
```
### **2. Install Docker on the Azure VM**
If Docker is not already installed, you can install it using the following commands:
```bash
# Update the package list
sudo apt update
# Install Docker
sudo apt install docker.io -y
# Start Docker service
sudo systemctl start docker
# Enable Docker to start on boot
sudo systemctl enable docker
# Verify Docker installation
docker --version
```
### **3. Prepare the Project Files**
You'll need three files for this project:
- `train_model.py`: For training and saving the ML model.
- `app.py`: The Flask application to serve predictions.
- `Dockerfile`: To define the Docker image.
#### **A. `train_model.py`**
This script will train the model and save it as a `.pkl` file.
```python
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
import joblib
# Load dataset
data = load_iris()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.3, random_state=42)
# Train model
model = RandomForestClassifier()
model.fit(X_train, y_train)
# Save the model
joblib.dump(model, 'iris_model.pkl')
print("Model trained and saved as iris_model.pkl")
```
Run this script to generate `iris_model.pkl`:
```bash
python3 train_model.py
```
#### **B. `app.py`**
This is the Flask application that will load the model and handle prediction requests.
```python
from flask import Flask, request, jsonify
import joblib
app = Flask(__name__)
# Load the model
model = joblib.load('iris_model.pkl')
@app.route('/predict', methods=['POST'])
def predict():
data = request.json['data']
prediction = model.predict([data])
return jsonify({'prediction': int(prediction[0])})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
```
#### **C. `Dockerfile`**
Create a `Dockerfile` to containerize the application:
```dockerfile
# Use an official Python runtime as a parent image
FROM python:3.9-slim
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir flask scikit-learn joblib
# Expose port 5000 for the Flask app
EXPOSE 5000
# Run app.py when the container launches
CMD ["python", "app.py"]
```
#### **D. `requirements.txt` (Optional)**
Alternatively, you can use a `requirements.txt` file for dependencies:
```
flask
scikit-learn
joblib
```
And modify the Dockerfile to:
```dockerfile
# Install dependencies from requirements.txt
COPY requirements.txt /app
RUN pip install --no-cache-dir -r requirements.txt
```
### **4. Build the Docker Image**
Run the following commands to build your Docker image:
```bash
# Build the Docker image
docker build -t iris-flask-app .
# Verify the image is created
docker images
```
### **5. Run the Docker Container**
After building the image, you can run it:
```bash
# Run the Docker container
docker run -d -p 5000:5000 --name iris-container iris-flask-app
```
- The `-d` flag runs the container in detached mode.
- The `-p 5000:5000` maps the container’s port 5000 to the VM’s port 5000.
### **6. Test the Flask API**
Use `curl` or Postman to test the API:
```bash
curl -X POST http://<Public-IP-of-VM>:5000/predict \
-H "Content-Type: application/json" \
-d '{"data": [5.1, 3.5, 1.4, 0.2]}'
```
Expected Output:
```json
{
"prediction": 0
}
```
### **7. (Optional) Push the Docker Image to Azure Container Registry**
If you want to scale your solution further, you can push your Docker image to the **Azure Container Registry (ACR)** for easier deployment on other Azure services like **Azure Kubernetes Service (AKS)**.
1. **Login to Azure Container Registry**:
```bash
az acr login --name <your_acr_name>
```
2. **Tag the Image**:
```bash
docker tag iris-flask-app <your_acr_name>.azurecr.io/iris-flask-app:v1
```
3. **Push the Image**:
```bash
docker push <your_acr_name>.azurecr.io/iris-flask-app:v1
```
### **8. Stop and Clean Up Resources**
To stop the container and clean up:
```bash
# Stop the running container
docker stop iris-container
# Remove the container
docker rm iris-container
# Remove the Docker image
docker rmi iris-flask-app
```
To delete the Azure VM and associated resources:
```bash
az group delete --name MLResourceGroup --yes --no-wait
```
---
By following these steps, you've successfully containerized a machine learning model using Docker and deployed it on an Azure Virtual Machine. This setup allows for scalable, isolated, and consistent deployments.