An ML model is often just one part of a larger machine. Docker Compose is the tool that brings all the pieces together into a unified system.
1The YAML Blueprint
A docker-compose.yaml file allows you to define your entire infrastructure as code. Instead of juggling multiple Dockerfiles and complex docker run flags, you define 'Services' like an API, a Vector Database, and a Redis cache. This single file becomes the 'Source of Truth' for your stack, making it easy to share with other developers or deploy to cloud environments.
# Docker Compose for ML Apps
# Orchestrating Multi-Container Architectures2Automatic Networking
One of the most powerful features of Compose is Automatic Service Discovery. When you run docker-compose up, all services are placed on a private network. They can talk to each other using their service names as hostnames. For example, if you name your database service db, your model server can connect to postgresql://db:5432 without needing to know a specific IP address.
services:
model-api:
build: .
ports: ["8000:8000"]
database:
image: postgres:153Persistence with Volumes
Containers are ephemeralβif they are deleted, any data inside them is lost. In MLOps, we need to persist model weights and training logs. Volumes allow you to map a folder on your host machine to a folder inside the container. This ensures that even if you rebuild your image or restart your stack, your data remains safe and accessible.
# Inside app.py
DB_URL = "postgresql://user@database:5432/mydb"