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. For example, suppose you are
running a service that enables additional logging when it reads
LOGGING_VERBOSE: true
in the environment. In this scenario, you set
the environment variable key/value pair as LOGGING_VERBOSE=true
. The
exact commands or UI are shown in the following sections.
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 for Anthos on Google Cloud. You should not set
it yourself.
Setting environment variables on a service
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 Cloud Console, the gcloud command line, or a YAML file when you create a new service or deploy a new revision:
Console
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.
Under Advanced Settings, click Variables.
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.
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,gcr.io/myproject/my-image:latest
.
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"
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=..."
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 beta run services replace
command.
Make sure you only modify fields as documented.
To view and download the configuration:
gcloud run services describe SERVICE --format export > service.yaml
Update the
name
andvalue
attributes under theenv
attribute undercontainers:
:apiVersion: serving.knative.dev/v1 kind: Service metadata: name: SERVICE spec: template: spec: containers: - env: - name: KEY-1 value: VALUE-1 - name: KEY-N value: VALUE-N
Replace
- SERVICE with the name of your Cloud Run for Anthos service
- KEY-1, VALUE-1 with the environment variable and value. Optionally add more variables and values as desired.
Replace the service with its new configuration using the following command:
gcloud beta run services replace service.yaml
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 for Anthos service, the value set on the service takes precedence.
Enabling Kubernetes service links in Cloud Run for Anthos
If you use Kubernetes service links in Cloud Run for Anthos and want to continue using them, you must manually enable support for that feature before January 2021. Due to the performance issues seen in namespaces with thousands of services and revisions, starting in January 2021, Kubernetes service links will be disabled by default.
To enable Kubernetes service links, run the following command to set
data.enable-service-links
to true
in your knative-serving/config-defaults
ConfigMap:
kubectl patch cm -n knative-serving config-defaults -p '{"data":{"enable-service-links":"true"}}