Google Cloud Run is commonly used to deploy containerized applications without managing servers or Kubernetes clusters. It handles infrastructure provisioning, scaling, and request routing, making it useful for APIs, background services, and event-driven applications.
However, applications that perform AI inference, image processing, video analysis, or other compute-intensive operations may benefit from GPU acceleration.
Cloud Run supports GPU workloads, allowing developers to run GPU-enabled containers without managing a traditional virtual machine environment.
This article explains how to prepare a GPU-enabled container, deploy it to Google Cloud Run, configure the required resources, and handle common deployment problems.
When Does a Cloud Run Application Need a GPU?
A CPU is sufficient for most web applications, database operations, and lightweight background tasks.
A GPU becomes useful when an application performs large amounts of parallel computation.
Common examples include:
Running machine learning inference.
Processing images and videos.
Generating embeddings.
Running computer vision models.
Performing audio transcription.
Executing GPU-accelerated scientific workloads.
Consider an image processing API that receives uploaded product images and uses a machine learning model to identify objects.
A CPU-based implementation may take several seconds to process each image. A compatible GPU can reduce processing time, depending on the model, batch size, memory requirements, and workload.
However, GPU acceleration does not automatically make every application faster. The model and its dependencies must support GPU execution, and the workload must be large enough to benefit from it.
Understand How GPU Workloads Run on Cloud Run
Cloud Run runs container images and manages the underlying infrastructure.
When deploying a GPU-enabled service, you configure the GPU resource alongside the normal container settings.
The general architecture looks like this:
Client Application
|
v
Cloud Run Service
|
v
GPU-Enabled Container
|
v
Machine Learning Model
|
v
GPU-Accelerated Processing
|
v
API ResponseThe application still uses the normal Cloud Run deployment model.
You do not need to create and manage a separate virtual machine just to run the GPU workload.
However, GPU availability depends on the Cloud Run configuration, supported region, quota, GPU type, and current service limitations. Check the supported configuration before designing the deployment.
Prepare the Application
For this example, create a Python application that loads a machine learning model and exposes an HTTP endpoint.
The application will use FastAPI and PyTorch.
Create a project structure:
gpu-image-service/
│
├── app.py
├── requirements.txt
└── DockerfileAdd the dependencies:
fastapi
uvicorn
torch
torchvision
pillowFor production, pin compatible dependency versions and use the PyTorch installation instructions appropriate for the selected CUDA environment. Do not assume that installing any PyTorch package automatically enables GPU execution.
Create the application:
from fastapi import FastAPI
import torch
app = FastAPI()
device = torch.device(
"cuda" if torch.cuda.is_available() else "cpu"
)
@app.get("/health")
async def health():
return {
"status": "healthy",
"device": str(device)
}
@app.post("/process")
async def process():
tensor = torch.randn(
1000,
1000,
device=device
)
result = torch.matmul(tensor, tensor)
return {
"device": str(device),
"result_shape": list(result.shape)
}This example performs a matrix multiplication to demonstrate device selection.
A real application would load a trained model and process an image, audio file, or other input.
The health endpoint reports whether PyTorch detected a CUDA-compatible GPU.
Do not rely only on successful container startup to confirm GPU availability. Verify that the actual model and inference operation execute on the expected device.
Create the Dockerfile
The container image must include the application dependencies and a compatible GPU runtime environment.
A basic Dockerfile can look like this:
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"]This is a basic application container, not a complete GPU-ready image.
For a production GPU workload, use a compatible CUDA-enabled base image or a suitable framework image. The CUDA runtime, PyTorch build, Python version, and GPU driver environment must be compatible.
Avoid installing unnecessary development packages in the final image. A smaller container is easier to build, scan, transfer, and deploy.
You should also test the container locally with an available compatible GPU before deploying it.
Configure Google Cloud
Before deployment, configure the Google Cloud project.
Install the Google Cloud CLI if it is not already available, then authenticate:
gcloud auth loginSelect the project:
gcloud config set project YOUR_PROJECT_IDEnable the required services:
gcloud services enable \
run.googleapis.com \
artifactregistry.googleapis.com \
cloudbuild.googleapis.comReplace YOUR_PROJECT_ID with your actual project ID.
Create an Artifact Registry repository:
gcloud artifacts repositories create gpu-apps \
--repository-format=docker \
--location=us-central1The region is an example. Select a region that supports the required Cloud Run GPU configuration and is appropriate for your users and data residency requirements.
Configure Docker authentication:
gcloud auth configure-docker us-central1-docker.pkg.devThis allows Docker to push images to the Artifact Registry repository.
Join the conversation! Your thoughts help the community grow.