For Docker images, environment variables can be defined directly in Dockerfiles with the ENV  directive. You can define them line by line or all in a single line, as shown in Example 18-1. 

FROM openjdk:11 ENV PATTERN "EnvVar Configuration" ENV LOG_FILE "/tmp/random.log" ENV SEED "1349093094" # Alternatively: ENV PATTERN="EnvVar Configuration" LOG_FILE=/tmp/random.log SEED=1349093094 ... 

Then a Java application running in such a container can easily access the variables with a call to the Java standard library, as shown in Example 18-2. 

public  Random initRandom() { long  seed = Long.parseLong(System.getenv("SEED")); return new  Random(seed); } 

Initializes a random-number generator with a seed from an EnvVar 

Directly running such an image will use the default hardcoded values. But in most cases, you want to override these parameters from outside the image. 

When running such an image directly with Docker, environment variables can be set from the command line by calling Docker, as in Example 18-3. 

docker run -e PATTERN="EnvVarConfiguration" \ -e LOG_FILE="/tmp/random.log" \ -e SEED="147110834325" \ k8spatterns/random-generator:1.0 

For Kubernetes, these types of environment variables can be set directly in the Pod specification of a controller like Deployment or ReplicaSet (as in Example 18-4). 

apiVersion : v1 kind : Pod 

