How to Deploy a Docker Container to Google Cloud: Cloud Run, GKE and Compute Engine

How to Deploy a Docker Container to Google Cloud: Cloud Run, GKE and Compute Engine

Reading time1 min
#google-cloud#docker#cloud-run#gke#devops

How to Deploy a Docker Container to Google Cloud: Cloud Run, GKE and Compute Engine

Google Cloud has three common places to run a Docker image:

  • Cloud Run. Serverless containers for HTTP services and batch jobs. No cluster, scales to zero, pay per use. The default choice for most stateless apps.
  • GKE (Google Kubernetes Engine). Managed Kubernetes. Autopilot mode also manages the nodes.
  • Compute Engine. A VM running Docker. Full control and full responsibility.

Two changes make many older guides wrong. Container Registry (gcr.io) was shut down in 2025, so images now go to Artifact Registry at REGION-docker.pkg.dev. And gcloud compute instances create-with-container relied on a container startup agent that stopped working on July 31, 2026. VMs now start containers through cloud-init or a startup script.

Step 1: project setup and Artifact Registry

gcloud auth login
gcloud config set project my-project
gcloud services enable artifactregistry.googleapis.com run.googleapis.com

gcloud artifacts repositories create my-repo \
  --repository-format=docker \
  --location=europe-west1

gcloud auth configure-docker europe-west1-docker.pkg.dev

configure-docker registers gcloud as a credential helper for that registry host, so docker push authenticates with your gcloud login.

Build and push:

IMAGE=europe-west1-docker.pkg.dev/my-project/my-repo/my-app:1.0.0
docker build --platform linux/amd64 -t $IMAGE .
docker push $IMAGE

Use --platform linux/amd64 on Apple Silicon Macs, or you get an arm64 image that fails on x86 machines. Without Docker installed locally, Cloud Build can build and push from the source directory: gcloud builds submit --tag $IMAGE.

Option A: Cloud Run

gcloud run deploy my-app \
  --image $IMAGE \
  --region europe-west1 \
  --port 8080 \
  --allow-unauthenticated

The command prints the service URL (https://my-app-....run.app) with a managed TLS certificate. What the container must do:

  • Listen on 0.0.0.0 on the port in the PORT environment variable (8080 unless you change it with --port).
  • Be stateless. The local filesystem lives in memory and disappears with the instance. Use Cloud SQL, Firestore or Cloud Storage for data.
  • Start quickly, because instances start on demand.

Useful flags:

gcloud run deploy my-app --image $IMAGE --region europe-west1 \
  --min-instances 1 \
  --memory 512Mi --cpu 1 \
  --set-env-vars LOG_LEVEL=info \
  --set-secrets DB_PASSWORD=db-password:latest

--min-instances 1 keeps one instance warm and avoids cold starts, at the cost of paying for it while idle. --set-secrets mounts a Secret Manager secret as an environment variable. The service's runtime service account needs roles/secretmanager.secretAccessor on it.

Every deploy creates a new revision and, by default, moves all traffic to it. To roll back, send traffic to an earlier revision:

gcloud run revisions list --service my-app --region europe-west1
gcloud run services update-traffic my-app --region europe-west1 --to-revisions my-app-00002-abc=100

If the repository has a Dockerfile, gcloud run deploy my-app --source . --region europe-west1 builds it with Cloud Build, stores the image in Artifact Registry and deploys it in one step. If --allow-unauthenticated fails, an organization policy is probably blocking public access, and someone with the right permissions has to allow it.

Option B: GKE Autopilot

Use GKE when you need Kubernetes features: several cooperating services, operators, custom networking, or a platform shared across teams.

gcloud services enable container.googleapis.com
gcloud components install gke-gcloud-auth-plugin

gcloud container clusters create-auto my-cluster --region europe-west1
gcloud container clusters get-credentials my-cluster --region europe-west1

kubectl create deployment my-app --image=$IMAGE --port=8080
kubectl expose deployment my-app --type=LoadBalancer --port=80 --target-port=8080
kubectl get service my-app --watch

When EXTERNAL-IP appears, the app is reachable on port 80. For HTTPS and host-based routing, use an Ingress or the Gateway API instead of a plain LoadBalancer Service. kubectl needs gke-gcloud-auth-plugin to authenticate to GKE. Without it you get an error about the missing plugin.

Autopilot bills by the CPU and memory your pods request, so set resources.requests in real manifests. The Kubernetes deployment guide covers manifests, probes and rollouts. If pods sit in ImagePullBackOff with a 403 error, the node service account lacks roles/artifactregistry.reader on the repository.

Option C: a Compute Engine VM

Container-Optimized OS (COS) images come with Docker preinstalled and a read-only root filesystem. Start the container with a systemd unit delivered through cloud-init. Save this as cloud-init.yaml:

#cloud-config
write_files:
- path: /etc/systemd/system/my-app.service
  permissions: "0644"
  owner: root
  content: |
    [Unit]
    Description=my-app container
    Wants=gcr-online.target
    After=gcr-online.target

    [Service]
    Environment="HOME=/home/my-app"
    ExecStartPre=/usr/bin/docker-credential-gcr configure-docker --registries europe-west1-docker.pkg.dev
    ExecStart=/usr/bin/docker run --rm --name my-app -p 80:8080 europe-west1-docker.pkg.dev/my-project/my-repo/my-app:1.0.0
    ExecStop=/usr/bin/docker stop my-app
    Restart=always

runcmd:
- systemctl daemon-reload
- systemctl start my-app.service

HOME has to point to a writable directory, because docker-credential-gcr cannot write to /root on COS. Create the VM with a service account that can read the repository, and open port 80:

gcloud compute instances create my-app-vm \
  --zone europe-west1-b \
  --machine-type e2-small \
  --image-family cos-stable --image-project cos-cloud \
  --service-account my-app-vm@my-project.iam.gserviceaccount.com \
  --scopes cloud-platform \
  --tags http-server \
  --metadata-from-file user-data=cloud-init.yaml

gcloud compute firewall-rules create allow-http --allow tcp:80 --target-tags http-server

To deploy a new version, change the image tag in the unit and restart it (sudo systemctl restart my-app over SSH), or recreate the VM from an updated instance template. A managed instance group gives you rolling replacement across several VMs. At that point, Cloud Run or GKE is usually less work.

Which one to choose

  • HTTP API, web app, webhook or scheduled job with no local state: Cloud Run.
  • Many services, Kubernetes tooling, or workloads Cloud Run cannot run (for example, specific networking or long-lived non-HTTP protocols): GKE Autopilot.
  • Software that expects a normal Linux host, special hardware, or full OS control: Compute Engine.

Quick reference

gcloud artifacts repositories create my-repo --repository-format=docker --location=REGION
gcloud auth configure-docker REGION-docker.pkg.dev
docker build --platform linux/amd64 -t REGION-docker.pkg.dev/PROJECT/my-repo/my-app:TAG .
docker push REGION-docker.pkg.dev/PROJECT/my-repo/my-app:TAG

# Cloud Run
gcloud run deploy my-app --image IMAGE --region REGION --allow-unauthenticated
gcloud run deploy my-app --source . --region REGION

# GKE Autopilot
gcloud container clusters create-auto my-cluster --region REGION
kubectl create deployment my-app --image=IMAGE --port=8080
kubectl expose deployment my-app --type=LoadBalancer --port=80 --target-port=8080

# Compute Engine (COS + cloud-init)
gcloud compute instances create my-app-vm --image-family cos-stable --image-project cos-cloud \
  --metadata-from-file user-data=cloud-init.yaml
    How to Deploy a Docker Container to Google Cloud: Cloud Run, GKE and Compute Engine