# Run TPU workloads in a Docker container

> [!CAUTION]
> **Caution:** This page applies to the Cloud TPU API, which is no longer under active development. The Cloud TPU API will receive bug fixes and security updates only. For the latest features and support for the latest TPU versions, we recommend using Compute Engine or Google Kubernetes Engine to manage TPU resources. For more information, see [TPU resources in Compute Engine](https://docs.cloud.google.com/tpu/docs/tpus-in-compute-engine.md.txt) and [About TPUs in GKE](https://docs.cloud.google.com/kubernetes-engine/docs/concepts/tpus.md.txt). The Cloud TPU API is not supported on TPU7x and later TPU versions.

[Docker containers](https://www.docker.com/resources/what-container/) make
configuring applications easier by combining your code and all needed
dependencies in one distributable package. You can run Docker containers within
TPU VMs to simplify configuring and sharing your Cloud TPU applications. This
document describes how to set up a Docker container for each ML framework
supported by Cloud TPU.

> [!IMPORTANT]
> **Important:** You can run a maximum of 256 Docker pods on a TPU VM.


## Train a PyTorch model in a Docker container

[PyTorch on Cloud TPU](https://docs.cloud.google.com/tpu/docs/pytorch-on-tpu.md.txt) describes the PyTorch
libraries for TPU and links to documentation for getting the Python packages and
Docker images. You can use
[TorchTPU](https://torchtpu.readthedocs.io/en/latest/) directly (see
[Get started on GKE](https://torchtpu.readthedocs.io/en/latest/get_started_on_gke/)
for the public release container image) or use the
[TorchTitan](https://github.com/google-pytorch/torchtitan) reference library to
build and run containerized training workloads.

## Train a JAX model in a Docker container

Before running the following commands, create the following environment variables:


```bash
export PROJECT_ID=your-project-id
export TPU_NAME=your-tpu-name
export ZONE=us-west4-a
export ACCELERATOR_TYPE=v5litepod-8
export RUNTIME_VERSION=v2-alpha-tpuv5-lite
```

#### Environment variable descriptions

- `PROJECT_ID`: Your Google Cloud project ID. Use an existing project or [create a new one.](https://docs.cloud.google.com/tpu/docs/setup-gcp-account.md.txt#set-up-project)
- `TPU_NAME`: The name of the TPU.
- `ZONE`: The zone in which to create the TPU VM. For more information about supported zones, see [TPU regions and zones](https://docs.cloud.google.com/tpu/docs/regions-zones.md.txt).
- `ACCELERATOR_TYPE`: The accelerator type specifies the version and size of the Cloud TPU you want to create. For more information about supported accelerator types for each TPU version, see [TPU versions](https://docs.cloud.google.com/tpu/docs/system-architecture-tpu-vm.md.txt#versions).
- `RUNTIME_VERSION`: The Cloud TPU [software version](https://docs.cloud.google.com/tpu/docs/runtimes.md.txt).

<br />

### TPU device

1. Create the TPU VM

   ```bash
   gcloud compute tpus tpu-vm create $TPU_NAME \
   --zone=$ZONE \
   --accelerator-type=$ACCELERATOR_TYPE \
   --version=$RUNTIME_VERSION
   ```
2. Connect to the TPU VM using SSH

   ```bash
   gcloud compute tpus tpu-vm ssh $TPU_NAME --zone=$ZONE
   ```
3. Start Docker daemon in TPU VM

   ```bash
   sudo systemctl start docker
   ```
4. Start Docker container

   ```bash
   sudo docker run --net=host -ti --rm --name your-container-name \
   --privileged us-central1-docker.pkg.dev/tpu-pytorch-releases/docker/xla:r2.6.0_3.10_tpuvm_cxx11 \
   bash
   ```
5. Install JAX

   ```bash
   pip install jax[tpu]
   ```
6. Install FLAX

   ```bash
   pip install --upgrade clu
   git clone https://github.com/google/flax.git
   pip install --user -e flax
   ```
7. Install `tensorflow` and `tensorflow-datasets` packages

   ```bash
   pip install tensorflow
   pip install tensorflow-datasets
   ```
8. Run the FLAX MNIST training script

   ```bash
   cd flax/examples/mnist
   python3 main.py --workdir=/tmp/mnist \
   --config=configs/default.py \
   --config.learning_rate=0.05 \
   --config.num_epochs=5
   ```

When the training script completes, clean up the resources.

1. Type `exit` to exit from the Docker container
2. Type `exit` to exit from the TPU VM
3. Delete the TPU VM

   ```bash
   gcloud compute tpus tpu-vm delete $TPU_NAME --zone=$ZONE
   ```

### TPU slice

When you run JAX code on a TPU slice, you must run your JAX code on all TPU
workers at the same time. One way to do this is to use the `gcloud compute tpus tpu-vm ssh`
command with the `--worker=all` and `--command` flags. The following
procedure shows you how to create a Docker image to make setting up each TPU
worker easier.

1. Create a file named `Dockerfile` in your current directory and paste the
   following text

   ```bash
   FROM python:3.10
   RUN pip install jax[tpu]
   RUN pip install --upgrade clu
   RUN git clone https://github.com/google/flax.git
   RUN pip install --user -e flax
   RUN pip install tensorflow
   RUN pip install tensorflow-datasets
   WORKDIR ./flax/examples/mnist
   ```
2. Prepare an Artifact Registry

   ```bash
   gcloud artifacts repositories create your-repo \
   --repository-format=docker \
   --location=europe-west4 --description="Docker repository" \
   --project=$PROJECT_ID

   gcloud artifacts repositories list \
   --project=$PROJECT_ID

   gcloud auth configure-docker europe-west4-docker.pkg.dev
   ```
3. Build the Docker image

   ```bash
   docker build -t your-image-name .
   ```
4. Add a tag to your Docker image before pushing it to the Artifact Registry.
   For more information on working with Artifact Registry, see
   [Work with container images](https://docs.cloud.google.com/artifact-registry/docs/docker.md.txt).

   ```bash
   docker tag your-image-name europe-west4-docker.pkg.dev/$PROJECT_ID/your-repo/your-image-name:your-tag
   ```
5. Push your Docker image to the Artifact Registry

   ```bash
   docker push europe-west4-docker.pkg.dev/$PROJECT_ID/your-repo/your-image-name:your-tag
   ```
6. Create a TPU VM

   ```bash
   gcloud compute tpus tpu-vm create $TPU_NAME \
   --zone=$ZONE \
   --accelerator-type=$ACCELERATOR_TYPE \
   --version=$RUNTIME_VERSION
   ```
7. Pull the Docker image from the Artifact Registry on all TPU workers

   ```bash
   gcloud compute tpus tpu-vm ssh $TPU_NAME --worker=all \
   --zone=$ZONE \
   --command='sudo usermod -a -G docker ${USER}'
   ```

   ```bash
   gcloud compute tpus tpu-vm ssh $TPU_NAME --worker=all \
   --zone=$ZONE \
   --command="gcloud auth configure-docker europe-west4-docker.pkg.dev --quiet"
   ```

   ```bash
   gcloud compute tpus tpu-vm ssh $TPU_NAME --worker=all \
   --zone=$ZONE \
   --command="docker pull europe-west4-docker.pkg.dev/$PROJECT_ID/your-repo/your-image-name:your-tag"
   ```
8. Run the container on all TPU workers

   ```bash
   gcloud compute tpus tpu-vm ssh $TPU_NAME --worker=all \
   --zone=$ZONE \
   --command="docker run -ti -d --privileged --net=host --name your-container-name europe-west4-docker.pkg.dev/$PROJECT_ID/your-repo/your-image-name:your-tag bash"
   ```
9. Run the training script on all TPU workers

   ```bash
   gcloud compute tpus tpu-vm ssh $TPU_NAME --worker=all \
   --zone=$ZONE \
   --command="docker exec --privileged your-container-name python3 main.py --workdir=/tmp/mnist \
   --config=configs/default.py \
   --config.learning_rate=0.05 \
   --config.num_epochs=5"
   ```

When the training script completes, clean up the resources.

1. Shut down the container on all workers

   ```bash
   gcloud compute tpus tpu-vm ssh $TPU_NAME --worker=all \
   --zone=$ZONE \
   --command="docker kill your-container-name"
   ```
2. Delete the TPU VM

   ```bash
   gcloud compute tpus tpu-vm delete $TPU_NAME \
   --zone=$ZONE
   ```

## What's next

- [Cloud TPU tutorials](https://docs.cloud.google.com/tpu/docs/tutorials)
- [Manage TPUs](https://docs.cloud.google.com/tpu/docs/managing-tpus-tpu-vm.md.txt)
- [Cloud TPU architecture](https://docs.cloud.google.com/tpu/docs/system-architecture-tpu-vm.md.txt)
- [Run JAX code on TPU slices](https://docs.cloud.google.com/tpu/docs/jax-pods.md.txt)