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.
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:
mkdir my-container-service && cd my-container-servicepulumi new container-gcp-typescriptmkdir my-container-service && cd my-container-servicepulumi new container-gcp-pythonmkdir my-container-service && cd my-container-servicepulumi new container-gcp-gomkdir my-container-service && cd my-container-servicepulumi new container-gcp-csharpmkdir my-container-service && cd my-container-servicepulumi new container-gcp-hclpulumi 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:
| Key | Description | Default |
|---|---|---|
| gcp:region | The Google Cloud region to deploy into | us-central1 |
| gcp:project | The Google Cloud project to deploy into | — |
| appPath | The path to the container application to deploy | app |
| containerPort | The port to expose on the container | 8080 |
| cpu | The number of virtual CPUs to allocate per container instance | 1 |
| memory | The amount of memory to allocate per container instance | 1Gi |
| concurrency | The maximum number of concurrent requests to serve per container instance | 50 |
pulumi config set:
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>| Key | Description | Default |
|---|---|---|
| gcp:region | The Google Cloud region to deploy into | us-central1 |
| gcp:project | The Google Cloud project to deploy into | — |
| appPath | The path to the container application to deploy | app |
| containerPort | The port to expose on the container | 8080 |
| cpu | The number of virtual CPUs to allocate per container instance | 1 |
| memory | The amount of memory to allocate per container instance | 1Gi |
| concurrency | The maximum number of concurrent requests to serve per container instance | 50 |
pulumi config set:
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>| Key | Description | Default |
|---|---|---|
| gcp:region | The Google Cloud region to deploy into | us-central1 |
| gcp:project | The Google Cloud project to deploy into | — |
| appPath | The path to the container application to deploy | app |
| containerPort | The port to expose on the container | 8080 |
| cpu | The number of virtual CPUs to allocate per container instance | 1 |
| memory | The amount of memory to allocate per container instance | 1Gi |
| concurrency | The maximum number of concurrent requests to serve per container instance | 50 |
pulumi config set:
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>| Key | Description | Default |
|---|---|---|
| gcp:region | The Google Cloud region to deploy into | us-central1 |
| gcp:project | The Google Cloud project to deploy into | — |
| appPath | The path to the container application to deploy | app |
| containerPort | The port to expose on the container | 8080 |
| cpu | The number of virtual CPUs to allocate per container instance | 1 |
| memory | The amount of memory to allocate per container instance | 1Gi |
| concurrency | The maximum number of concurrent requests to serve per container instance | 50 |
pulumi config set:
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>| Key | Description | Default |
|---|---|---|
| gcp:project | The Google Cloud project to deploy into | — |
| region | The Google Cloud region to deploy into | us-central1 |
| image_name | The name to give the container image | my-app |
| app_path | The path to the container application to deploy | ./app |
| container_port | The port to expose on the container | 8080 |
| cpu | The number of vCPUs to allocate per container instance | 1 |
| memory | The amount of memory to allocate per container instance | 1Gi |
| concurrency | The maximum number of concurrent requests per container instance | 80 |
pulumi config set:
pulumi config set gcp:project <value>pulumi config set region <value>pulumi config set image_name <value>pulumi config set app_path <value>pulumi config set container_port <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.yamlnames the project and the language it’s written in.Pulumi.dev.yamlholds thedevstack’s settings, including the values you just entered.index.tsindex.js__main__.pymain.goProgram.csProgram.fsProgram.vbApp.javaPulumi.yamlmain.tfis the program: the code that declares your cloud resources.appholds a small web app that answers with a “Hello, world!” message, and the Dockerfile that builds it.
When setup completes, you’ll see a few key files:
Pulumi.yamlnames the project and the language it’s written in.Pulumi.dev.yamlholds thedevstack’s settings, including the values you just entered.index.tsindex.js__main__.pymain.goProgram.csProgram.fsProgram.vbApp.javaPulumi.yamlmain.tfis the program: the code that declares your cloud resources.appholds a small web app that answers with a “Hello, world!” message, and the Dockerfile that builds it.
When setup completes, you’ll see a few key files:
Pulumi.yamlnames the project and the language it’s written in.Pulumi.dev.yamlholds thedevstack’s settings, including the values you just entered.index.tsindex.js__main__.pymain.goProgram.csProgram.fsProgram.vbApp.javaPulumi.yamlmain.tfis the program: the code that declares your cloud resources.appholds a small web app that answers with a “Hello, world!” message, and the Dockerfile that builds it.
When setup completes, you’ll see a few key files:
Pulumi.yamlnames the project and the language it’s written in.Pulumi.dev.yamlholds thedevstack’s settings, including the values you just entered.index.tsindex.js__main__.pymain.goProgram.csProgram.fsProgram.vbApp.javaPulumi.yamlmain.tfis the program: the code that declares your cloud resources.appholds a small web app that answers with a “Hello, world!” message, and the Dockerfile that builds it.
When setup completes, you’ll see a few key files:
Pulumi.yamlnames the project and the language it’s written in.Pulumi.dev.yamlholds thedevstack’s settings, including the values you just entered.index.tsindex.js__main__.pymain.goProgram.csProgram.fsProgram.vbApp.javaPulumi.yamlmain.tfis the program: the code that declares your cloud resources.appholds 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:
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.devgcloud 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.devgcloud 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.devgcloud 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.devgcloud services enable run.googleapis.com artifactregistry.googleapis.com --project $(pulumi config get gcp:project)gcloud auth configure-docker $(pulumi config get region)-docker.pkg.devThen 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.
pulumi upPulumi 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:
open $(pulumi stack output url)To check it from the terminal, call the service with curl:
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
1CPU.- 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
appfolder, which contains a “Hello World” example app.
- 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
1CPU.- 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
appfolder, which contains a “Hello World” example app.
- 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
1CPU.- 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
appfolder, which contains a “Hello World” example app.
- 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
1CPU.- 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
appfolder, which contains a “Hello World” example app.
- region
The Google Cloud region to deploy into. Defaults to
us-central1.- image_name
The name of the container image deployed to your Cloud Run service. Defaults to
my-app.- app_path
The location of the Dockerfile used to build the container image. Defaults to the
appfolder, which contains a “Hello World” example app.- container_port
The port mapping for the container service. Defaults to port
8080.- cpu
The amount of CPU to allocate to each container instance. Defaults to
1CPU.- 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
80.
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:
pulumi config set containerPort 3000pulumi upgcloud 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)"pulumi config set containerPort 3000pulumi upgcloud 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)"pulumi config set containerPort 3000pulumi upgcloud 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)"pulumi config set containerPort 3000pulumi upgcloud 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)"pulumi config set container_port 3000pulumi upgcloud run services list --project $(pulumi config get gcp:project) --region $(pulumi config get 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:
pulumi destroyLearn more#
- Browse other architecture templates in the Templates gallery.
- Explore the Google Cloud provider API docs in the Pulumi Registry.
- Walk through Pulumi from the ground up in Pulumi Tutorials.
- Read the latest container posts on the Pulumi blog.