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#

Make sure you’ve installed Pulumi and configured your Google Cloud credentials.

Create a new project#

Create a new project using the template in the language of your choice:

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

pulumi new walks you through creating and configuring a 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. The template also includes some additional settings:

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
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 these settings 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 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.

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

  • Pulumi.yaml names the project and the language it’s written in.
  • Pulumi.dev.yaml holds the dev stack’s settings, including the values you just entered.
  • index.ts is the program: the 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.

Deploy the project#

Before the first deploy, turn on the Cloud Run and Artifact Registry APIs in your project, and let Docker push images to Artifact Registry in your region:

Terminal window
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 HTTP 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!"}

Customize the project#

Projects created with the Container Service 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 curl $(pulumi stack output url) still gets the same “Hello, world!” reply.

To see what else you can add, browse the Google Cloud provider in the Pulumi Registry.

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 as code platform for any cloud.