TradeLabs AILearn

Docker and Kubernetes

How containers with Docker package trading bots and research environments, when Kubernetes helps, and when simpler setups are better for latency and reliability.

Advanced3 min readUpdated 3 Oct 2026
Markdown
Lesson 5 of 16

Containers package an application together with everything it needs to run: the right Python version, libraries and system tools. Docker is the most common tool for building and running containers. Kubernetes is a system for running many containers across many machines, handling restarts, scaling and deployment. In trading, containers make research reproducible and deployments consistent. They also add complexity, and for latency critical systems, some firms avoid them on the trading path itself.

Why containers help traders#

BenefitExample
Same environment everywhereA bot that runs on your laptop runs identically on the server
Reproducible researchA backtest can be rerun years later with the same library versions. See Backtest Reproducibility
Easy deploymentShip one image instead of installing packages by hand
IsolationSeparate bots do not conflict over library versions
RollbackReturn to the previous image if a release misbehaves

A simple Dockerfile for a Python bot#

FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "main.py"]

Build with docker build -t mybot:1.4.0 . and run with docker run -d --restart unless-stopped --env-file .env mybot:1.4.0. The --restart flag restarts the container after crashes or reboots. Keep secrets in environment files or a secrets manager, never inside the image.

Docker Compose for small stacks#

A retail setup often has a bot, a database and a monitoring tool. Docker Compose describes them in one file and starts them together, which suits a single server well. See Redis and PostgreSQL for Trading.

What Kubernetes adds#

FeatureWhat it does
SchedulingPlaces containers on machines with spare capacity
Self healingRestarts failed containers and moves them off failed machines
Rolling deploymentsUpdates services gradually with rollback
ScalingRuns more copies of a service under load
Configuration and secretsCentral management

Kubernetes shines for research platforms, data pipelines and many services. For one or two bots, it is usually more complexity than needed.

Containers and latency#

Containers on Linux share the host's kernel, so their compute overhead is small. Networking can add overhead depending on configuration, and orchestrators may move or throttle workloads. For latency critical trading, firms often run on dedicated, tuned machines, sometimes with containers using host networking and pinned CPUs, sometimes without containers at all. See CPU Affinity, NUMA and Cache Optimization and Kernel Bypass and Low-Latency Networking.

Stateful trading systems#

Trading bots hold state: positions, open orders and recent data. Orchestrators that restart or move containers freely must be paired with persistent storage and startup reconciliation, so a restarted bot never forgets what it holds. Running two copies of a bot by accident can double orders; use leader election or locks so only one instance trades. See Fault Tolerance, High Availability and Redundancy and Trade Accounting and Reconciliation.

Common mistakes#

  1. Secrets baked into images that get pushed to shared registries.
  2. Using the "latest" tag, so you do not know which version is running.
  3. No resource limits, letting one container starve others.
  4. Two bot instances running after a careless redeploy.
  5. Over engineering a single bot with a full cluster.

Frequently asked questions#

Should I use Docker for my trading bot?#

It is a good choice for consistent deployments and easy rollback, especially when running several services, though a simple systemd service also works well.

Do trading firms use Kubernetes?#

Many use it for research, data pipelines and internal services; latency critical trading systems often run on dedicated tuned machines instead.

Do containers slow down trading systems?#

Compute overhead is small; networking and orchestration can add latency, which matters only for very latency sensitive strategies.

Next, learn how components pass messages to each other in Message Queues.

Check your understanding

3 quick questions on this lesson. Get them all right to finish it.

Turn on JavaScript to take the quiz.

Finished this lesson?Sign in to save your progress across devices.
Next lessonMessage QueuesMessage queues pass market data, signals and orders between trading components. Learn pub sub and queues, tools like Kafka, Redis and ZeroMQ, and design trade offs.

Mentioned in