# How to Build and Deploy a Containerized App to Google Cloud Run
Deploying applications in the cloud has historically required managing virtual machines, configuring orchestration clusters like Kubernetes, and constantly tuning infrastructure to handle traffic spikes.
Google Cloud Run dramatically simplifies this process. As a fully managed serverless platform, Cloud Run abstracts away all infrastructure management. It automatically scales your application from zero to thousands of instances based on incoming traffic, and you only pay for the exact compute time your code uses.
The only requirement is that your application must be packaged into a Docker container.
This guide provides a complete workflow for taking a basic application, containerizing it using Docker, pushing it to Google Artifact Registry, and deploying it securely to Google Cloud Run.
## Prerequisites
1. A **Google Cloud Platform (GCP)** account with an active billing project.
2. The **Google Cloud CLI (`gcloud`)** installed and authenticated on your local machine.
3. **Docker Desktop** (or the Docker Engine) installed and running locally.
4. Basic familiarity with terminal commands.
## Step 1: Create a Simple Application and Dockerfile
For this guide, we will use a minimal Node.js Express application, but the Cloud Run process is identical whether you use Python, Go, Java, or Ruby.
1. **Create the application files:**
Create a new directory and create a file named `index.js`.
“`javascript
const express = require(‘express’);
const app = express();
app.get(‘/’, (req, res) => {
res.send(‘Hello from Google Cloud Run!’);
});
// Cloud Run injects the PORT environment variable.
// Default is 8080.
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
“`
2. **Create the `package.json` file:**
“`json
{
“name”: “cloud-run-demo”,
“version”: “1.0.0”,
“main”: “index.js”,
“scripts”: {
“start”: “node index.js”
},
“dependencies”: {
“express”: “^4.18.2”
}
}
“`
3. **Create the `Dockerfile`:**
This file tells Docker how to build the environment for your application.
“`dockerfile
# Use the official lightweight Node.js image
FROM node:20-slim
# Set the working directory inside the container
WORKDIR /usr/src/app
# Copy package files and install dependencies
COPY package*.json ./
RUN npm install –only=production
# Copy the local application code to the container
COPY . .
# Run the web service on container startup
CMD [ “npm”, “start” ]
“`
## Step 2: Configure Google Artifact Registry
Before Cloud Run can deploy your container, the container image must be stored securely within Google Cloud. Artifact Registry is the successor to Container Registry and is the recommended storage solution.
1. **Enable the necessary GCP APIs:**
“`bash
gcloud services enable artifactregistry.googleapis.com run.googleapis.com cloudbuild.googleapis.com
“`
2. **Create a Docker repository in Artifact Registry:**
Replace `my-project-id` with your actual GCP Project ID.
“`bash
gcloud artifacts repositories create cloud-run-repo \
–repository-format=docker \
–location=us-central1 \
–description=”Docker repository for Cloud Run deployments” \
–project=my-project-id
“`
3. **Authenticate Docker to your new repository:**
This allows your local Docker daemon to push images to GCP.
“`bash
gcloud auth configure-docker us-central1-docker.pkg.dev
“`
## Step 3: Build and Push the Container Image
Google Cloud Build allows you to build your Docker image directly in the cloud, removing the need for a powerful local machine.
Run the following command from the directory containing your `Dockerfile`. Replace `my-project-id` with your Project ID.
“`bash
gcloud builds submit –tag us-central1-docker.pkg.dev/my-project-id/cloud-run-repo/demo-app:v1
“`
### What this command does:
1. It packages your current local directory into a compressed file.
2. It uploads the file to a temporary Google Cloud Storage bucket.
3. It provisions a temporary VM in Google Cloud Build.
4. It executes the instructions in your `Dockerfile` to build the image.
5. It automatically pushes the resulting, tagged container image into the Artifact Registry repository you created in Step 2.
## Step 4: Deploy to Cloud Run
With the image stored in Artifact Registry, you are ready to deploy it to Cloud Run.
Execute the deploy command:
“`bash
gcloud run deploy demo-app-service \
–image us-central1-docker.pkg.dev/my-project-id/cloud-run-repo/demo-app:v1 \
–region us-central1 \
–allow-unauthenticated
“`
### Explaining the deployment flags:
– `demo-app-service`: The name of your Cloud Run service.
– `–image`: The exact URI of the container image you pushed in Step 3.
– `–region`: The geographical location where your app will be hosted. This should ideally match your Artifact Registry location.
– `–allow-unauthenticated`: This tells Cloud Run to create a public HTTPS endpoint. If you omit this, the service will be locked down, and only authenticated IAM users or service accounts will be able to trigger it.
## Step 5: Verify and Manage Your Deployment
Once the deployment finishes (usually within 15 to 30 seconds), the terminal will output a **Service URL**, which looks similar to `https://demo-app-service-abcdef123-uc.a.run.app`.
1. Open this URL in your web browser. You should see “Hello from Google Cloud Run!”.
2. Cloud Run automatically provisions an SSL/TLS certificate for this domain and forces HTTPS traffic.
### Updating Your Application
When you need to update your code:
1. Modify `index.js`.
2. Run `gcloud builds submit` again, incrementing the tag (e.g., `:v2`).
3. Run `gcloud run deploy` pointing to the new `:v2` image.
Cloud Run will automatically perform a zero-downtime rolling update, shifting traffic seamlessly from the old container to the new one.
By mastering this workflow, you can bypass the complexity of server management and focus entirely on writing application logic, letting Google’s infrastructure handle the scaling and availability.