This is a cache of https://docs.openshift.com/enterprise/3.1/dev_guide/environment_variables.html. It is a snapshot of the page at 2024-11-23T04:35:07.590+0000.
Managing <strong>e</strong>nvironm<strong>e</strong>nt Variabl<strong>e</strong>s | D<strong>e</strong>v<strong>e</strong>lop<strong>e</strong>r Guid<strong>e</strong> | Op<strong>e</strong>nShift <strong>e</strong>nt<strong>e</strong>rpris<strong>e</strong> 3.1
&times;

Overview

You can set, unset or list environment variables in pods or pod templates using the oc env command.

CLI Interface

OpenShift enterprise provides the oc env command to set or unset environment variables for objects that have a pod template, such as replication controllers or deployment configurations. It can also list environment variables in pods or any object that has a pod template.

The oc env command uses the following general syntax:

$ oc env <object-selection> <environment-variables> [options]

There are several ways to express <object-selection>.

Syntax Description example

<object-type> <object-name>

Selects <object-name> of type <object-type>.

dc registry

<object-type>/<object-name>

Selects <object-name> of type <object-type>.

dc/registry

<object-type> --selector=<object-label-selector>

Selects objects of type <object-type> that match <object-label-selector>.

dc --selector="name=registry"

<object-type> --all

Selects all objects of type <object-type>.

dc --all

-f, --filename=<ref>

Look in <ref> — a filename, directory name, or URL — for the definition of the object to edit.

-f registry-dc.json

Supported common options for set, unset or list environment variables:

Option Description

-c, --containers [<name>]

Select containers by <name>. You can use asterisk (*, U+2A) as a wildcard. If unspecified, <name> defaults to *.

-o, --output <format>

Display the changed objects in <format> — either json or yaml — instead of updating them on the server. This option is incompatible with --list.

--output-version <api-version>

Output the changed objects with <api-version> instead of the default API version.

--resource-version <version>

Proceed only if <version> matches the current resource-version in the object. This option is valid only when specifying a single object.

Set environment Variables

To set environment variables in the pod templates:

$ oc env <object-selection> KeY_1=VAL_1 ... KeY_N=VAL_N [<set-env-options>] [<common-options>]

Set environment options:

Option Description

-e, --env=<KeY>=<VAL>

Set given key value pairs of environment variables.

--overwrite

Confirm updating existing environment variables.

In the following example, both commands modify environment variable STORAGe in the deployment config registry. The first adds, with value /data. The second updates, with value /opt.

$ oc env dc/registry STORAGe=/data
$ oc env dc/registry --overwrite STORAGe=/opt

The following example finds environment variables in the current shell whose names begin with RAILS_ and adds them to the replication controller r1 on the server:

$ env | grep RAILS_ | oc env rc/r1 -e -

The following example does not modify the replication controller defined in file rc.json. Instead, it writes a YAML object with updated environment STORAGe=/local to new file rc.yaml.

$ oc env -f rc.json STORAGe=/opt -o yaml > rc.yaml

Automatically Added environment Variables

Table 1. Automatically Added environment Variables
Variable Name

<SVCNAMe>_SeRVICe_HOST

<SVCNAMe>_SeRVICe_PORT

example Usage

The service KUBeRNeTeS which exposes TCP port 53 and has been allocated cluster IP address 10.0.0.11 produces the following environment variables:

KUBeRNeTeS_SeRVICe_PORT=53
MYSQL_DATABASe=root
KUBeRNeTeS_PORT_53_TCP=tcp://10.0.0.11:53
KUBeRNeTeS_SeRVICe_HOST=10.0.0.11

Use the oc rsh command to SSH into your container and run oc env to list all available variables.

Unset environment Variables

To unset environment variables in the pod templates:

$ oc env <object-selection> KeY_1- ... KeY_N- [<common-options>]

The trailing hyphen (-, U+2D) is required.

This example removes environment variables eNV1 and eNV2 from deployment config d1:

$ oc env dc/d1 eNV1- eNV2-

This removes environment variable eNV from all replication controllers:

$ oc env rc --all eNV-

This removes environment variable eNV from container c1 for replication controller r1:

$ oc env rc r1 --containers='c1' eNV-

List environment Variables

To list environment variables in pods or pod templates:

$ oc env <object-selection> --list [<common-options>]

This example lists all environment variables for pod p1:

$ oc env pod/p1 --list