How to Deploy Docker Images to Cloud Run
What is Cloud Run
Google Cloud Run is a fully managed serverless platform that enables you to deploy and scale containerized applications quickly and cost-effectively. Built on the open-source Knative framework, Cloud Run automatically handles infrastructure management, scaling, and load balancing, allowing developers to focus on writing code rather than managing servers.
Key benefits of Google Cloud Run include:
- Serverless scaling: Automatically scales from zero to thousands of instances based on traffic
- Pay-per-use: You only pay for the compute time you actually use
- Container support: Deploy any containerized application that listens for requests
- Fast deployments: Deploy new versions in seconds with zero downtime
- Integration: Seamlessly integrates with other Google Cloud services
- Portability: Uses standard containers, so your apps can run anywhere
Cloud Run is perfect for web applications, APIs, microservices, and batch jobs. Whether you're building a simple web service or a complex distributed system, Cloud Run provides the flexibility and reliability you need.
In this comprehensive guide, we'll walk through the complete process of taking a Docker image you built locally and deploying it to Google Cloud Run. You'll learn how to use gcloud CLI, Cloud Build, and Artifact Registry to create a production-ready containerized service.
Prerequisites
Before you start:
- You have installed and initialized the gcloud CLI. See How to Install gcloud CLI.
- Docker is installed and working on your local machine.
1. Log In and Set Your Project
Authenticate with Google Cloud and pick your project:
gcloud auth login
Choose your project
gcloud config set project YOUR_PROJECT_ID
PROJECT_ID=$(gcloud config get-value project)
2. Choose a Region
Set a default region for your Cloud Run service. For example:
REGION=us-central1
3. Enable Required APIs
Enable the APIs you need for Cloud Run and Artifact Registry:
gcloud services enable \
artifactregistry.googleapis.com \
cloudbuild.googleapis.com \
run.googleapis.com
4. Build and Test Your Docker Image Locally
- Build the image:
docker build -t my-app:latest . - Run it locally to make sure it works:
docker run --rm -p 8080:8080 my-app:latest
If your app runs on port 8080 locally, you’re good to go.
5. Push to Artifact Registry via Cloud Build
Google recommends using Cloud Build to build your image and store it in Artifact Registry:
- Configure Docker authentication:
gcloud auth configure-docker ${REGION}-docker.pkg.dev - Set a repository path:
REPO=${REGION}-docker.pkg.dev/${PROJECT_ID}/my-repo - Create a
cloudbuild.yamlfile:steps: - name: 'gcr.io/cloud-builders/docker' args: ['build', '-t', '${REPO}/my-app:latest', '.'] images: - '${REPO}/my-app:latest' - Submit the build:
gcloud builds submit --region=$REGION --config=cloudbuild.yaml
This command builds your container and pushes it to Artifact Registry.
6. Deploy to Cloud Run
Deploy the image to Cloud Run and allow public access:
gcloud run deploy my-app \
--image ${REPO}/my-app:latest \
--region $REGION \
--platform managed \
--allow-unauthenticated
After a moment, you will see a URL for your live service.
7. Test Your Cloud Run Service
List your services and find the URL:
gcloud run services list --region $REGION
Open the URL in your browser or use curl to test your API.
Tips and Best Practices
- Keep your
.envfile out of Git, but ensure needed variables are set during build. - Use Cloud Build secrets or KMS to manage sensitive data.
- Monitor logs with
gcloud logs read - Clean up unused services and images to avoid extra costs.
- Configuration files: If your application uses YAML configuration files that are excluded in
.gitignore, Cloud Build will also ignore them. Temporarily comment out those lines in.gitignoreto ensure your configuration files are included in the container build. - Debugging containers: For troubleshooting, pull the deployed container image to your local system, run it locally, and use
docker exec -it <container_id> /bin/bashto inspect the container's internal state and file system.
You’ve successfully built, pushed, and deployed a Docker image to Google Cloud Run! Explore custom domains, scaling options, and traffic splitting next.



