tensorflow
gpu-support
docker-compose
machine-learning
containerization

How to run tensorflow with gpu support in docker-compose?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Running TensorFlow with GPU acceleration in Docker Compose is mostly an infrastructure problem, not a TensorFlow problem. You need compatible NVIDIA drivers on the host, the NVIDIA container runtime, and a Compose service definition that exposes GPUs to the container correctly. If any of those layers are misconfigured, TensorFlow will silently fall back to CPU or fail during startup.

A reliable setup should be reproducible for local development and CI runners. This guide focuses on the exact pieces you need, how to verify them quickly, and how to avoid the common traps around image tags and runtime compatibility.

Core Sections

1. Validate host and runtime prerequisites

Before touching Compose, verify host capabilities.

bash
nvidia-smi

docker run --rm --gpus all nvidia/cuda:12.4.1-base-ubuntu22.04 nvidia-smi

If nvidia-smi fails on the host, containers will not work either. If host is fine but Docker command fails, install or repair NVIDIA Container Toolkit and restart Docker daemon.

2. Configure docker-compose.yml for GPU access

Use an image that includes TensorFlow GPU support and explicitly request GPU devices.

yaml
1services:
2  trainer:
3    image: tensorflow/tensorflow:2.16.1-gpu
4    working_dir: /workspace
5    volumes:
6      - ./:/workspace
7    deploy:
8      resources:
9        reservations:
10          devices:
11            - driver: nvidia
12              count: 1
13              capabilities: [gpu]
14    environment:
15      - NVIDIA_VISIBLE_DEVICES=all
16      - TF_CPP_MIN_LOG_LEVEL=1
17    command: python verify_gpu.py

Depending on Compose implementation, deploy.resources may be ignored outside swarm mode. If that happens in your environment, use docker compose run --gpus all trainer for explicit CLI-level allocation.

3. Verify TensorFlow sees the GPU in-container

Create a small runtime check script:

python
1import tensorflow as tf
2
3print("TF version:", tf.__version__)
4print("GPUs:", tf.config.list_physical_devices("GPU"))
5
6if not tf.config.list_physical_devices("GPU"):
7    raise SystemExit("No GPU detected by TensorFlow")

This should run in every environment bootstrap. Do not trust logs alone. Explicit checks prevent long training jobs from accidentally running on CPU.

4. Tune performance and reliability

Enable memory growth to reduce OOM spikes when multiple processes share a GPU:

python
gpus = tf.config.list_physical_devices("GPU")
for gpu in gpus:
    tf.config.experimental.set_memory_growth(gpu, True)

Also pin exact image tags instead of latest, and align CUDA/cuDNN compatibility with the TensorFlow version. For production training pipelines, keep this matrix documented in repo to avoid accidental upgrade drift.

5. Build repeatable verification around TensorFlow GPU workloads in Docker Compose

After implementation works once, lock in behavior with repeatable verification artifacts. At minimum, maintain one baseline case, one edge case, and one failure-path case with expected outcomes written down in plain language. This prevents accidental regressions when dependencies, runtime versions, or surrounding infrastructure change.

Use lightweight automation for these checks so they run in local development and CI. A practical pattern is to keep a tiny fixture dataset and one command that executes the critical path end to end. If that command fails, engineers can reproduce issues quickly without rebuilding the entire environment from scratch.

text
1verification checklist
2- baseline scenario with expected output
3- edge scenario with constrained input
4- failure scenario with expected error behavior
5- runtime and dependency versions captured

Treat this checklist as versioned code-adjacent documentation. Updating TensorFlow GPU workloads in Docker Compose without updating its verification contract is a common source of drift and support incidents.

6. Operational guidance and maintenance strategy

The long-term reliability of TensorFlow GPU workloads in Docker Compose depends on observability and change discipline. Add structured logging and targeted metrics around the most failure-prone stages so you can answer quickly: what input was processed, what branch was taken, and why output changed. Incident response improves dramatically when these signals exist before the outage.

Also define ownership for changes. When libraries, runtime versions, or platform policies evolve, someone should review compatibility and re-run validation artifacts before rollout. Small proactive checks are cheaper than emergency rollback windows.

Finally, schedule periodic contract checks even when no incident is active. Silent drift accumulates over time through dependency updates and environment differences. Preventive checks keep TensorFlow GPU workloads in Docker Compose predictable and reduce production surprises.

Common Pitfalls

  • Assuming Docker Compose automatically passes GPUs without runtime or CLI configuration.
  • Using incompatible host driver and TensorFlow image CUDA versions.
  • Relying on latest image tags and getting unexpected runtime changes.
  • Skipping an explicit in-container GPU detection check before long jobs.
  • Treating deploy.resources as universally supported in all Compose execution modes.

Summary

TensorFlow GPU support in Docker Compose works reliably when host drivers, NVIDIA runtime, and Compose allocation are aligned. Start by validating host and container GPU visibility, then pin compatible images and add explicit runtime checks in your workflow. With these controls in place, your GPU training setup remains reproducible and predictable across developer machines and automation environments.


Course illustration
Course illustration

All Rights Reserved.