Skip to main content

Command Palette

Search for a command to run...

Deploying an ML Model with FastAPI + Docker (Hands-On MLOps) flow to deploy the Ml Model

Published
โ€ข3 min read
Deploying an ML Model with FastAPI + Docker (Hands-On MLOps) flow to deploy the Ml Model
P

๐Ÿ‘‹ Hello! I'm passionate about DevOps and have over 1+ years of experience in the field. I'm proficient in a variety of cutting-edge technologies and always motivated to expand my knowledge and skills. Let's connect and grow together!

SKILLS:

๐Ÿ”น Languages & Runtimes: Python, Shell Scripting, HCL, YAML ๐Ÿ”น Cloud Technologies: AWS, Microsoft Azure, GCP ๐Ÿ”น Infrastructure Tools: Docker, Terraform, AWS CloudFormation ๐Ÿ”น Other Tools: Linux, Git and GitHub Actions, Jenkins, Jira, GitLab (beginner), Docker, AWS DevOps ๐Ÿ”น Web Development: HTML, CSS, Bootstrap, Python, SQL

Job & Responsibilities:

๐Ÿš€ Improved development efficiency by implementing CI/CD pipelines, resulting in a 30% reduction in deployment time on the test server. ๐Ÿ”’ Strengthened deployment and testing reliability by utilizing Docker containers and optimizing Dockerfile, reducing development issues on the test server by 20%. โš™๏ธ Automated S3 bucket log creation with Shell scripting, eliminating 100% of manual search and saving 2 hours per week. ๐Ÿ“… Scheduled EC2 instance start/stop using Lambda functions and Event Bridge, leading to a 25% decrease in infrastructure costs. ๐Ÿ”ง Utilized AWS, Linux, Python, Docker, Shell scripting, Terraform, Jenkins Pipelines, and automation to streamline workflows and improve overall system performance.

I'm very detail-oriented and possess strong written and verbal communication skills. As a high performer with a possibility mindset, I strive to solve problems using efficient approaches.

Let's Connect & Grow:

If you find my profile suitable for the role you are searching for, please feel free to reach out to me at sumanprasad9766@gmail.com.

  1. Train a small ML model

  2. Expose it as an API

  3. Package it with Docker

  4. Run it like a real production service

Step 1: Create a simple model and save it

Create a file called train.py

from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
import joblib

# load data
X, y = load_iris(return_X_y=True)

# train model
model = RandomForestClassifier()
model.fit(X, y)

# save model to file
joblib.dump(model, "model.joblib")

print("Model trained and saved as model.joblib")

Run it:

pip install scikit-learn joblib
python train.py

Youโ€™ll now have a file:

model.joblib

This is your trained model artifact.


Step 2: Create an API to serve predictions

Create app.py

from fastapi import FastAPI
import joblib
import numpy as np

app = FastAPI()

model = joblib.load("model.joblib")

@app.get("/")
def health():
    return {"status": "running"}

@app.post("/predict")
def predict(features: list[float]):
    data = np.array(features).reshape(1, -1)
    pred = model.predict(data)[0]
    return {"prediction": int(pred)}

Install API dependencies and run locally:

pip install fastapi uvicorn numpy
uvicorn app:app --host 0.0.0.0 --port 8000

Test it in another terminal:

curl -X POST "<http://localhost:8000/predict>" \\
     -H "Content-Type: application/json" \\
     -d "[5.1,3.5,1.4,0.2]"

You should get a JSON prediction back.

This is real-time inference via API.


Step 3: Containerize with Docker

Create a Dockerfile

FROM python:3.10-slim

WORKDIR /app

COPY model.joblib .
COPY app.py .

RUN pip install fastapi uvicorn scikit-learn joblib numpy

EXPOSE 8000

CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]

Build the image:

docker build -t iris-model-api .

Run the container:

docker run -p 8000:8000 iris-model-api

Now your model is running in a production-like container.

Test again:

curl -X POST "<http://localhost:8000/predict>" \\
     -H "Content-Type: application/json" \\
     -d "[6.7,3.1,4.7,1.5]"

Step 4: Simulate a simple rollout update

Train a new version:

python train.py
mv model.joblib model_v2.joblib

Update app.py to load model_v2.joblib instead, then rebuild:

docker build -t iris-model-api:v2 .
docker run -p 8001:8000 iris-model-api:v2

Now you have:

  • v1 running on port 8000

  • v2 running on port 8001

You can compare both before switching traffic.

This mimics blue-green or canary testing locally.


Step 5: (Optional) Run multiple replicas

Run two containers to simulate scaling:

docker run -d -p 8001:8000 iris-model-api
docker run -d -p 8002:8000 iris-model-api

You can now send requests to both ports to simulate load distribution.


What you just achieved

With only a few commands you have:

  • trained a model

  • served it via an API

  • packaged it with Docker

  • run real-time predictions

  • simulated version rollout

  • simulated horizontal scaling

This is the core practical foundation of model deployment in real products.

More from this blog

D

DeployToCloud

405 posts

๐Ÿ‘‹ Welcome to my Hashnode blog! I'm a DevOps Engineer with 2+ years of experience. Join ~5k followers and explore 320+ blogs on Python, AWS, Docker, Jenkins, Linux, and more. Let's connect & grow ๐Ÿš€