Using Environment Variables

When you set environment variables, they are injected into the container and are accessible to your code.

Like any configuration change, setting an environment variable leads to the creation of a new revision. Subsequent revisions will also automatically get this environment variable unless you make explicit updates to change it.

Valid variable name characters

You can use only alphanumeric characters and underscores in environment variable names.

Reserved names

The environment variables defined in the container runtime contract are reserved and cannot be set. In particular, the PORT environment variable is injected inside your container by Cloud Run. You should not set it yourself.

Setting environment variables on a service

You can set environment variables using the GCP Console or the gcloud command line when you create a new service or deploy a new revision:

Console

  1. Go to Cloud Run

  2. Click CREATE SERVICE if you are setting environment variables on a new service you are deploying to. If you are setting variables on an existing service, then click on the service, then click DEPLOY NEW REVISION.

  3. Click SHOW OPTIONAL SETTINGS:

    Add environment variable

    • If you are adding a variable, click Add Variable, and specify the name you want for the variable and its value in the Name and Value text boxes.

    • If you are changing a value for a variable, replace the current value in the Value text box with the one you want.

    • If you are removing one or more environment variables, hover your cursor to the left of the Value textbox of the variable you are removing to display the Trash icon, and click it.

  4. Click Create or Deploy.

Command line

To set, update, or remove environment variables of an existing service, use the gcloud beta run services update command. You can use any of the following flags, as needed:

For example to add or update the environment variables:

gcloud beta run services update [SERVICE] --update-env-vars KEY1=VALUE1,KEY2=VALUE2
  • Replace [SERVICE] with the name of your service.
  • Replace KEY1=VALUE1,KEY2=VALUE2, with the comma separated list of desired variable name and value.

You can also set environment variables during deployment using the command:

gcloud beta run deploy [SERVICE] --image gcr.io/[PROJECT-ID]/[IMAGE] --update-env-vars KEY1=VALUE1,KEY2=VALUE2

Replace

  • Replace [SERVICE] with the name of your service.
  • [PROJECT-ID] with your GCP project ID.
  • [IMAGE] with the name of your image.

Setting default environment variables in the container

You can use the ENV statement in a Dockerfile to set default values for environment variables:

ENV KEY1=VALUE1,KEY2=VALUE2

Order of precedence: container vs service variables

If you set a default environment variable in the container and also set an environment variable with the same name on the Cloud Run service, the value set on the service takes precedence.

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Run Documentation