Building AI Solutions with Docker Compose and Kubernetes Expertise
By Vladimir Mikhalev Docker Captain · CNCF Ambassador · IBM Champion
You’re building AI workloads. So you’re juggling Python packages, GPU drivers, REST APIs, databases, maybe a Kafka pipeline on top of all that. And if you’re still herding it with bash scripts and hope?
You’re doing it wrong.
This guide is about how Docker Compose tames the chaos of a modern AI project. Not the toy version. The version someone runs when they’ve actually shipped containers in production instead of poking at notebooks.
I’ll get into the real usage. Environment handling. Image pull policies. Secrets, resource limits, and the one command, docker compose watch, that saves your sanity during dev cycles. There’s also the part nobody enjoys: handing your Compose stack off to Kubernetes without rage-quitting.
Compose 2.x: A Dev Tool That Grew Up
Compose is still your best friend for local work. No argument there. But it’s also a serious CI/CD asset now, and a good staging orchestrator, if you know how to drive it.
Enough preamble.
Environment Variable Precedence: Know Who Wins the Fight
By default, Compose favors your shell environment over whatever sits in your .env file.
So this:
export DATABASE_URL=postgres://prod.db…will override this in .env:
DATABASE_URL=postgres://dev.dbGood. That’s exactly the behavior you want in CI/CD, where secrets have no business touching source control.
Controlling Image Pulls: Don’t Get Burned by Stale Containers
There are two clean ways to pull fresh images.
-
Force it every time:
Terminal window docker compose up --pull always -
Lock it in the Compose file:
services:app:image: my-image:latestpull_policy: always
pull_policy supports: always, if_not_present, and never.
Use it. Skip it and you’ll burn an afternoon debugging a bug that doesn’t exist, because CI quietly served a stale image from cache while you screamed at your pipeline.
SSH & Secrets: Handling Sensitive Stuff Like an Adult
BuildKit is the default now. That buys you real control over build-time secrets and SSH access.
SSH During Builds
Need to clone a private repo mid-build?
services: app: build: context: . ssh: - default=/home/user/.ssh/id_rsaThe key never lands in the image. No more “oops I leaked my SSH key to Docker Hub”.
Runtime Secrets
Compose has secrets of its own, no Swarm required. Declare them in a top-level secrets: block, sourced from a file or an environment variable, and grant them per service. Each one lands inside the container as a file at /run/secrets/<name>, not as an env var:
services: app: image: my-image:latest secrets: - db_password
secrets: db_password: file: ./db_password.txtNeed rotation and an audit trail on top of that? Wire in a vault.
Live Reloads with docker compose watch: Real Dev Speed
You want a tight feedback loop. That’s what the watch command is for.
Real-World Example: Node.js App
Watch works on services you build from local source, so the service needs a build: section and a Dockerfile that copies the app into /app:
services: app: build: . command: npm start environment: NODE_ENV: development ports: - "3000:3000" develop: watch: - action: sync path: . target: /app ignore: - node_modules/ - action: rebuild path: package.jsonThen run:
docker compose up --watchChange a source file and Compose copies it into the running container. No rebuild. No restart. Pair it with a dev server that hot-reloads and the change is live. Touch package.json and Compose rebuilds the image for you. No staring at the screen wondering why your fix didn’t take.
Override Files: Keep Dev and Prod from Colliding
Still cramming every config into one compose.yaml? Stop. Split it:
compose.override.yamlfor local tweaksdocker compose -f base.yaml -f prod.yamlto layer configsinclude:blocks, if you’re on Compose v2+ and feeling fancy
It’s cleaner, it’s safer, and it’s a lot easier to debug when something breaks at 2am.
YAML Anchors: DRY or Die
Compose files turn into spaghetti fast. Anchors fix that:
x-default-env: &default-env NODE_ENV: production
services: web: image: webapp environment: *default-env
api: image: apiserver environment: <<: *default-env DEBUG: trueLess repetition, fewer bugs, and a file you can actually read six months from now.
Resource Limits: Be a Good Container Citizen
Even on a dev cluster, don’t let one container eat the whole node.
services: ai-worker: image: my-ai-image deploy: resources: limits: cpus: "1.0" memory: "1G" reservations: cpus: "0.5" memory: "512M"And no, docker compose doesn’t ignore deploy on a single host. Compose v2 applies the CPU and memory limits, the memory reservation and GPU device reservations, and reads deploy.replicas as the service’s scale. The CPU reservation is the one line here it skips. Hand the same file to Swarm or translate it to Kubernetes and the numbers come along.
Compose to Kubernetes: The Good, The Bad, and The “Use Kompose”
Want to turn a Compose stack into Kubernetes YAMLs? Kompose does it, and the output isn’t terrible.
kompose convertYou’ll still have homework:
- Set up Ingress and controllers by hand
- Configure PVCs and storage
- Handle secrets the Kubernetes way
For MVPs and small internal tools, though, it works fine.
Just don’t ship the raw output to production. Clean it up first.
Final Take
Docker Compose stopped being a one-trick Redis-launcher a while ago. Driven well, it’s a serious tool, and it earns its keep most in AI workflows, the kind that sprawl across a dozen services.
It lets you:
- Develop locally at full speed
- Keep secrets out of your images
- Cut downtime during builds
- Hand off to Kubernetes when the time comes
If you’re shipping AI services and you’re not using Compose properly, you’re just making the job harder than it is.