DEV Community

Swapnil Patil
Swapnil Patil

Posted on

Containerizing Mobile ML Models: Running On-Device Inference with Docker and TensorFlow Lite

Modern mobile apps increasingly rely on on-device ML models - from fraud detection to face recognition and personalization.
But managing and testing these models before deploying them into mobile SDKs can be painful. Different developers use different OS setups, Python versions, and TensorFlow environments.

Here's where Docker shines: it lets you standardize ML model training and conversion pipelines, and easily deploy those models to mobile apps (iOS or Android) for inference testing.

The Problem: Environment Drift in ML Pipelines

When preparing models for mobile inference, developers typically go through these steps:

1. Train a model in TensorFlow or PyTorch
2. Convert it to TensorFlow Lite (.tflite) or Core ML (.mlmodel) format
3. Optimize and quantize it
4. Test on Android or iOS
Enter fullscreen mode Exit fullscreen mode

Without containers, this process is brittle — dependency versions differ, GPU drivers mismatch, and pipeline reproducibility breaks.

The Solution: Docker-Based ML Model Conversion

We’ll create a Docker container that handles:

  • Model conversion to ".tflite"
  • Quantization and optimization
  • Export for mobile SDK integration

Dockerfile

FROM tensorflow/tensorflow:2.16.1

# Install TensorFlow Lite converter and optimization toolkit
RUN pip install tensorflow==2.16.1 tensorflow-model-optimization

# Create working directory
WORKDIR /ml-model

# Copy model and scripts
COPY model.h5 .
COPY convert.py .

# Run conversion
CMD ["python", "convert.py"]
Enter fullscreen mode Exit fullscreen mode

convert.py

import tensorflow as tf
from tensorflow import keras
import tensorflow_model_optimization as tfmot

# Load model
model = keras.models.load_model("model.h5")

# Quantize the model for mobile efficiency
quantize_model = tfmot.quantization.keras.quantize_model(model)

# Convert to TensorFlow Lite
converter = tf.lite.TFLiteConverter.from_keras_model(quantize_model)
tflite_model = converter.convert()

# Save output
with open("model_quantized.tflite", "wb") as f:
    f.write(tflite_model)

print("Model converted and quantized for mobile inference!")
Enter fullscreen mode Exit fullscreen mode

Build and Run

docker build -t ml-converter .
docker run --rm -v $(pwd):/ml-model ml-converter
Enter fullscreen mode Exit fullscreen mode

Integrating with Mobile SDKs

Once the .tflite model is generated, you can drop it into your Android or iOS app.

Android Example (Kotlin)

val tflite = Interpreter(loadModelFile("model_quantized.tflite"))
val input = floatArrayOf(0.5f, 0.8f, 0.1f)
val output = Array(1) { FloatArray(1) }
tflite.run(input, output)
println("Prediction: ${output[0][0]}")
Enter fullscreen mode Exit fullscreen mode

iOS Example (Swift)

let modelPath = Bundle.main.path(forResource: "model_quantized", ofType: "tflite")!
let interpreter = try! Interpreter(modelPath: modelPath)
try! interpreter.allocateTensors()
try! interpreter.invoke()
let outputTensor = try! interpreter.output(at: 0)

Enter fullscreen mode Exit fullscreen mode

Security & Compliance Angle

For fintech and identity systems, you can also:
• Run the Docker container in isolated CI pipelines for scanning ML model metadata (e.g., no PII leaks).
• Use Docker layers for reproducible auditing — every model build is traceable and version-controlled.
• Integrate AI explainability dashboards via Streamlit or Flask inside the same container for fraud model insights.

Benefits

Key Benefits of Using Docker for Mobile ML Pipelines:
• Eliminates environment drift — every model build runs in a consistent containerized setup.
• Reduces setup time by reusing cached dependencies and shared base images.
• Improves compliance and auditability through signed and versioned Docker images.
• Enables faster integration testing for Android and iOS apps using unified pipelines.
• Simplifies collaboration between ML engineers, mobile developers, and DevOps teams.

Conclusion

By containerizing ML model preparation, mobile teams can create predictable, secure, and automated pipelines from training to deployment.
This approach bridges ML engineers, mobile SDK developers, and security reviewers — ensuring every mobile AI feature ships faster and safer.

Top comments (0)