Use environment variables

When you set environment variables, they are injected into the container and are accessible to your code. Environment variables are set as key/value pairs.

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.

Set environment variables

You can set environment variables for Cloud Run services or jobs.

For Cloud Run services

Any configuration change leads to the creation of a new revision. Subsequent revisions will also automatically get this configuration setting unless you make explicit updates to change it.

You can set environment variables using the Google Cloud console, the gcloud command line, or a YAML file when you create a new service or deploy a new revision:

Console

  1. Go to Cloud Run

  2. Click Create Service if you are configuring a new service you are deploying to. If you are configuring an existing service, click on the service, then click Edit and Deploy New Revision.

  3. If you are configuring a new service, fill out the initial service settings page as desired, then click Next > Container, Variables & Secrets, Connections, Security to reach the service configuration page.

  4. Click the Variables & Secrets tab.

    image

  5. Do the following:

    • 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.

  6. Click Create or Deploy.

Command line

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

You can specify environment variables while deploying a service, or update them after the service is created:

gcloud run deploy [SERVICE] --image IMAGE_URL --update-env-vars KEY1=VALUE1,KEY2=VALUE2

gcloud 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 names and their values.
  • Replace IMAGE_URL with a reference to the container image, for example, us-docker.pkg.dev/cloudrun/container/hello:latest.

YAML

You can download and view existing service configuration using the gcloud run services describe --format export command, which yields cleaned results in YAML format. You can then modify the fields described below and upload the modified YAML using the gcloud run services replace command. Make sure you only modify fields as documented.

  1. To view and download the configuration:

    gcloud run services describe SERVICE --format export > service.yaml
  2. Update the name and value attributes under the env attribute under containers::

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: SERVICE
    spec:
      template:
        metadata:
          name: REVISION
        spec:
          containers:
          - image: IMAGE
            env:
            - name: KEY-1
              value: VALUE-1
            - name: KEY-N
              value: VALUE-N

    Replace

    • SERVICE with the name of your Cloud Run service
    • IMAGE_URL with a reference to the container image, for example, us-docker.pkg.dev/cloudrun/container/hello:latest
    • KEY-1, VALUE-1 with the environment variable and value. Optionally add more variables and values as desired.
    • REVISION with a new revision name or delete it (if present). If you supply a new revision name, it must meet the following criteria:
      • Starts with SERVICE-
      • Contains only lowercase letters, numbers and -
      • Does not end with a -
      • Does not exceed 63 characters
  3. Replace the service with its new configuration using the following command:

    gcloud run services replace service.yaml

For Cloud Run jobs

To set an environment variable for a Cloud Run job:

Console

  1. Go to Cloud Run

  2. If you are configuring a new job, click the Jobs tab and fill out the initial job settings page as desired. If you are configuring an existing job, click the job, then click Edit.

  3. Click Container, Variables, Connections, Security to expand the job properties page.

  4. Click the Variables tab.

    image

  5. Do the following:

    • 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.

  6. Click Create or Update.

Command line

Set, update, or remove environment variables of an existing service, using the gcloud alpha run jobs update command. You can use any of the following flags, as needed:

  • --set-env-vars
  • --update-env-vars
  • --remove-env-vars
  • --clear-env-vars

    You can specify environment variables while creating a job or when updating a job:

    gcloud beta run jobs create JOB_NAME --image IMAGE_URL --update-env-vars KEY1=VALUE1,KEY2=VALUE2
    
    gcloud beta run jobs update JOB_NAME --update-env-vars KEY1=VALUE1,KEY2=VALUE2
  • Replace JOB_NAME with the name of your job.

  • Replace KEY1=VALUE1,KEY2=VALUE2, with the comma separated list of desired variable names and their values.

  • Replace IMAGE_URL with a reference to the container image, for example, us-docker.pkg.dev/cloudrun/container/job:latest.

Set many environment variables

If you have too many environment variables that cannot be easily listed in KEY1=VALUE1,KEY2=VALUE2 format, you can alternatively repeat the flags listed above multiple times:

   [...]
   --set-env-vars "KEY1=VALUE1" \
   --set-env-vars "KEY2=VALUE2" \
   --set-env-vars "KEY3=VALUE3"

Escape comma characters

Because the comma character , is used to split environment variables, if your environment variable contains comma characters as values, you need to escape those delimiters by specifying a different delimiter character, for example, @:

--set-env-vars "^@^KEY1=value1,value2,value3@KEY2=..."

Set 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 or job 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 or job, the value set on the service takes precedence.

View environment variables settings

To view the current environment variables settings for your Cloud Run service:

Console

  1. Go to Cloud Run

  2. Click the service you are interested in to open the Service details page.

  3. Click the Revisions tab.

  4. In the details panel at the right, the environment variables setting is listed under the Variables tab.

Command line

  1. Use the following command:

    gcloud run services describe SERVICE
  2. Locate the environment variables setting in the returned configuration.

To view the current environment variables settings for your Cloud Run job:

Console

  1. Go to Cloud Run jobs

  2. Click the job you are interested in to open the Job details page.

  3. Click the Configuration tab.

  4. Locate the environment variables setting in the configuration details.

Command line

  1. Use the following command:

    gcloud beta run jobs describe JOB_NAME
  2. Locate the environment variables setting in the returned configuration.

Sample code

For code sample that show how to access environment variables in your code, refer to Handling sensitive configuration with Secret Manager in the End user authentication tutorial.