(build-docker)=
This guide covers setting up a MaxText development environment and building container images for TPU and GPU workloads. These images can be used to run MaxText on GKE clusters with TPUs or GPUs, and are also required for running MaxText through XPK.
Before starting, ensure you have the following tools installed and configured:
-
Environment Prep: Install and configure all XPK prerequisites.
-
Docker Permissions: Follow the steps to configure sudoless Docker to run Docker without
sudo. -
Artifact Registry Access: Authenticate with Google Artifact Registry for permission to push your images and other access.
-
Authentication & Access: Run the following commands to authenticate your account and configure Docker:
# Authenticate your user account for gcloud CLI access
gcloud auth login
# Configure application default credentials for Docker and other tools
gcloud auth application-default login
# Configure Docker credentials and test your access
gcloud auth configure-docker
docker run hello-worldIf the `docker run hello-world` command fails with a permission denied error (e.g., `dial unix /var/run/docker.sock: connect: permission denied`), please ensure you have correctly followed step 2 of the prerequisites to configure sudoless Docker. Running `sudo usermod -aG docker $USER` followed by starting a new shell session is usually required to apply the group permissions.
Follow the instructions in the Installation guide to install the maxtext[runner] extra, which includes all necessary dependencies for building MaxText Docker images and running workloads through XPK. It automatically installs XPK, so you do not need to install it separately to manage your clusters and workloads.
MaxText publishes pre-built container images for TPU pre-training and TPU post-training workloads in Google Artifact Registry. You can use these images directly with Google Kubernetes Engine (GKE) or XPK without building images from scratch.
| MaxText Version | Container Image |
|---|---|
0.2.3 |
us-docker.pkg.dev/cloud-tpu-images/maxtext-images/tpu_pre_training:0.2.3 |
| MaxText Version | Container Image |
|---|---|
0.2.3 |
us-docker.pkg.dev/cloud-tpu-images/maxtext-images/tpu_post_training:0.2.3 |
If you need to customize dependencies or build from source, follow the instructions below to build your own MaxText Docker image.
Select the appropriate build commands based on your hardware (TPU or GPU) and your specific workflow (pre-training or post-training). Each of these commands will generate a local Docker image named maxtext_base_image.
# Option 1: Build with the stable versions of dependencies (default)
build_maxtext_docker_image
# Option 2: Build with latest nightly versions of jax/jaxlib
build_maxtext_docker_image MODE=nightly
# Option 3: Build with the specified jax/jaxlib version
build_maxtext_docker_image MODE=nightly JAX_VERSION=$JAX_VERSION# Option 1: Build with the stable versions of dependencies (default)
build_maxtext_docker_image DEVICE=gpu
# Option 2: Build with latest nightly versions of jax/jaxlib
build_maxtext_docker_image DEVICE=gpu MODE=nightly
# Option 3: Build with base image as `ghcr.io/nvidia/jax:base-2024-12-04`
build_maxtext_docker_image DEVICE=gpu MODE=pinned
# Option 4: Build with the specified jax/jaxlib version
build_maxtext_docker_image DEVICE=gpu MODE=nightly JAX_VERSION=$JAX_VERSION# This build process takes approximately 10 to 15 minutes.
build_maxtext_docker_image WORKFLOW=post-trainingYou can upload your built Docker image to either Google Artifact Registry (recommended) or Google Container Registry (GCR, legacy).
To upload to Artifact Registry, provide the full path of the target image (including the registry location, project, and repository) in CLOUD_IMAGE_NAME. The script will automatically tag it as :latest if no tag is specified.
# 1. Define the full Artifact Registry path
export CLOUD_IMAGE_NAME=<LOCATION>-docker.pkg.dev/<PROJECT_ID>/<REPOSITORY_NAME>/<IMAGE_NAME>
# 2. Upload the image (must be run inside your activated venv)
upload_maxtext_docker_image CLOUD_IMAGE_NAME=${CLOUD_IMAGE_NAME?}If you provide just a simple image name, the script will default to uploading to GCR under your current active gcloud project (gcr.io/<PROJECT_ID>/<IMAGE_NAME>:latest).
# 1. Define a simple image name
export CLOUD_IMAGE_NAME=<IMAGE_NAME>
# 2. Upload the image (must be run inside your activated venv)
upload_maxtext_docker_image CLOUD_IMAGE_NAME=${CLOUD_IMAGE_NAME?}**Virtual Environment Reminder:** The `upload_maxtext_docker_image` command is a console script installed inside your virtual environment. If you open a new terminal session to upload the image, you **must re-activate your virtual environment** first:
```bash
source ${VENV_NAME?}/bin/activate
```
If you get a `command not found: upload_maxtext_docker_image` error, it means your virtual environment is not activated.
**Note on Permissions:** You will need the [**Artifact Registry Writer**](https://docs.cloud.google.com/artifact-registry/docs/access-control#permissions) role to push Docker images to Artifact Registry. If you don't have this permission, contact your project administrator to grant you this role through "Google Cloud Console -> IAM -> Grant access".
- If you see the following error while building or uploading your Docker image, try adding the listed file path to
.dockerignore. Do not include the./prefix in the.dockerignorefile:
ERROR: Found symbolic links with absolute paths in the build context:
./<add_this_value_to_dockerignore>