Skip to main content

Search the Dev Center

Search tutorials, templates, examples, and more.

Container Service on Google Cloud

The Google Cloud Container Service template scaffolds a Pulumi project that deploys a containerized service to Google Cloud Run for serverless container execution, with the container image stored in Artifact Registry. The template ships with placeholder app content so the project deploys end to end out of the box.

Architecture diagram of the Google Cloud Container Service template Google Cloud Cloud Run Service Container Registry

Before you begin#

For this walkthrough, you'll need:

Create a new project#

Create a new project from the template:

Terminal window
mkdir my-container-service; cd my-container-service
pulumi new container-gcp-typescript

The pulumi new command walks you through creating and configuring the new project and stack. A stack is an instance of a project, and you can have many of them (dev, staging, prod), each with its own configuration settings. Depending on the language, you may also be asked for your preferred package manager or toolchain.

This template’s settings include:

KeyDescriptionDefault
gcp:regionThe Google Cloud region to deploy intous-central1
gcp:projectThe Google Cloud project to deploy into—
appPathThe path to the container application to deployapp
imageNameThe name to give the container imagemy-app
containerPortThe port to expose on the container8080
cpuThe number of virtual CPUs to allocate per container instance1
memoryThe amount of memory to allocate per container instance1Gi
concurrencyThe maximum number of concurrent requests to serve per container instance50
You can change values like these later with pulumi config set :
Terminal window
pulumi config set gcp:region <value>
pulumi config set gcp:project <value>
pulumi config set appPath <value>
pulumi config set imageName <value>
pulumi config set containerPort <value>
pulumi config set cpu <value>
pulumi config set memory <value>
pulumi config set concurrency <value>

For gcp:project, enter the ID of an existing Google Cloud project. gcloud config get-value project prints the one gcloud is using. Press Enter at the others to keep the defaults.

Review the code#

When setup completes, you’ll see a few files:

  • Pulumi.yaml contains the project name and the language it’s written in.
  • Pulumi.dev.yaml contains the dev stack’s settings, including the values you just entered.
  • index.ts contains the program code that declares your cloud resources.
  • app holds a small web app that answers with a “Hello, world!” message, and the Dockerfile that builds it.

Take a moment to open them up and have a look. Here’s what’s in index.ts:

index.ts
import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";
import * as dockerbuild from "@pulumi/docker-build";
import * as random from "@pulumi/random";
// Import the program's configuration settings.
const config = new pulumi.Config();
const imageName = config.get("imageName") || "my-app";
const appPath = config.get("appPath") || "./app";
const containerPort = config.getNumber("containerPort") || 8080;
const cpu = config.getNumber("cpu") || 1;
const memory = config.get("memory") || "1Gi";
const concurrency = config.getNumber("concurrency") || 50;
// Import the provider's configuration settings.
const gcpConfig = new pulumi.Config("gcp");
const location = gcpConfig.require("region");
const project = gcpConfig.require("project");
// Generate a unique Artifact Registry repository ID
const uniqueString = new random.RandomString("unique-string", {
length: 4,
lower: true,
upper: false,
numeric: true,
special: false,
})
let repoId = uniqueString.result.apply(result => "repo-" + result);
// Create an Artifact Registry repository
const repository = new gcp.artifactregistry.Repository("repository", {
description: "Repository for container image",
format: "DOCKER",
location: location,
repositoryId: repoId,
});
// Form the repository URL
let repoUrl = pulumi.concat(location, "-docker.pkg.dev/", project, "/", repository.repositoryId);
// Create a container image for the service.
// Before running `pulumi up`, configure Docker for authentication to Artifact Registry
// as described here: https://cloud.google.com/artifact-registry/docs/docker/authentication
const image = new dockerbuild.Image("image", {
tags: [pulumi.concat(repoUrl, "/", imageName)],
context: {
location: appPath,
},
// Cloud Run currently requires x86_64 images
// https://cloud.google.com/run/docs/container-contract#languages
platforms: ["linux/amd64"],
push: true,
});
// Create a Cloud Run service definition.
const service = new gcp.cloudrun.Service("service", {
location,
template: {
spec: {
containers: [
{
image: image.ref,
resources: {
limits: {
memory,
cpu: cpu.toString(),
},
},
ports: [
{
containerPort,
},
],
}
],
containerConcurrency: concurrency,
},
},
});
// Create an IAM member to allow the service to be publicly accessible.
const invoker = new gcp.cloudrun.IamMember("invoker", {
location,
service: service.name,
role: "roles/run.invoker",
member: "allUsers",
});
// Export the URL of the service.
export const url = service.statuses.apply(statuses => statuses[0]?.url);

Deploy the project#

Before the first deploy, sign in the gcloud CLI, turn on the Cloud Run and Artifact Registry APIs in your project, and let Docker push images to Artifact Registry in your region. Pulumi uses your application default credentials, but these commands, and Docker’s push, use the gcloud CLI’s own sign-in:

Terminal window
gcloud auth login
gcloud services enable run.googleapis.com artifactregistry.googleapis.com --project $(pulumi config get gcp:project)
gcloud auth configure-docker "$(pulumi config get gcp:region)-docker.pkg.dev"

Then deploy with pulumi up. It starts with a preview of what it’s about to create and asks before it changes anything. Choose yes to deploy. Pulumi builds the container image with Docker on your machine, so Docker needs to be running.

Terminal window
pulumi up

Pulumi adds a random suffix to most resource names, so the Cloud Run service service becomes something like service-4f8a2c1. This is autonaming. It lets you deploy the same program to more than one stack, and lets Pulumi create a replacement before it deletes the old resource, without names colliding.

When it finishes, Pulumi prints the stack’s outputs, values it only knows once the resources exist:

url

The HTTPS URL of your Cloud Run service.

Read one any time with pulumi stack output <name>, or pass it to another command. This opens the service in your browser:

Terminal window
open $(pulumi stack output url)

To check it from the terminal, call the service with curl:

Terminal window
curl $(pulumi stack output url)
{"message":"Hello, world!"}
Need help?

If pulumi up reports failed to load application credentials, sign in again:

Terminal window
gcloud auth application-default login

Still stuck? Let us know.

Customize the project#

Projects created with this template expose the following configuration settings:

containerPort

The port mapping for the container service. Defaults to port 8080.

cpu

The amount of CPU to allocate to each container instance. Defaults to 1 CPU.

memory

The amount of memory to allocate to each container instance. Defaults to 1Gi.

concurrency

The maximum number of concurrent requests per container instance. Defaults to 50.

imageName

The name of the container image deployed to your Cloud Run service. Defaults to my-app.

appPath

The location of the Dockerfile used to build the container image. Defaults to the app folder, which contains a “Hello World” example app.

All of these settings are optional and may be adjusted either by editing the stack configuration file directly (by default, Pulumi.dev.yaml) or by changing their values with pulumi config set.

For example, move the app to port 3000, then list the project’s Cloud Run services with their container port to check that yours now shows 3000:

Terminal window
pulumi config set containerPort 3000
pulumi up
gcloud run services list --project $(pulumi config get gcp:project) --region $(pulumi config get gcp:region) \
--format="table(metadata.name, spec.template.spec.containers[0].ports[0].containerPort)"

The app picks up the new port too, so the curl command above still gets the same “Hello, world!” reply.

Cleaning up#

You can cleanly destroy the stack and all of its infrastructure with pulumi destroy:

Terminal window
pulumi destroy

Learn more#

Related

The infrastructure platform for humans and agents.