springboot_study/basic-environment at master · sergueik/springboot_study · GitHub
Skip to content

Latest commit

 

History

History

Folders and files

README.md

Info

This directory contains a basic exercise Java project to explore passthrough arguments for environment and variables settings into container hosted app in Docker

The application prints to STDERR what it gets in

String propertyKey ="setting_prop";
System.err.println(propertyKey + " = " + System.getProperty(propertyKey));

and

String envKey = "setting_env";
System.err.println(envKey + " = " + System.getenv(envKey));

Basics

  • pass through commandline
docker run -it -e VAR=value alpine:3.9 sh

in the container

/ # echo $var

will show

value
  • pass via caller environment
export VAR=value
docker run -it -e VAR alpine:3.9 sh

in the container

/ # echo $VAR

will show

value
  • wih environment file
echo VAR=value>env.txt
docker run -it --env-file=$(pwd)/env.txt alpine:3.9 sh

in the container

/ # echo $var

will show

value
  • with docker-compose.yml
version: '3.7'

services:
  example:
    image: alpine:3.9
    environment:
      - VAR
export VAR=value
docker-compose run  -e VAR  example sh

in the container

/ # echo $var

will show

value

alternativey define VAR in predefined filename .env

VAR=default

and then no need to set it as command argument

docker-compose run example sh

in the container

/ # echo $var

will show

value

The host enviroment value, if set, still overrides the one in .env

Testing

  • build app
mvn package
  • setting from Dockerfile
export IMAGE='basic-environment'
export NAME='example-environment'
docker build -t $IMAGE -f Dockerfile .
docker run --name $NAME --rm -it $IMAGE

to avoid browsing the whole output, may use the command

export SETTING='setting_env'
2>&1 docker run --name $NAME --rm -it $IMAGE | grep $SETTING

will show

setting_env = dockerfile_value
  • override value at build time
docker image rm -f $IMAGE
docker build -t $IMAGE -f Dockerfile --build-arg "setting_arg=build_time_value" .

then

docker run --name $NAME --rm -it $IMAGE

will print the value defined at image build time:

setting_env = build_time_value
  • override at container time (use same container as before)
2>&1 docker run --name $NAME --rm -e "setting_env=runtime_value" -it $IMAGE | grep $SETTING

will show

setting_env = runtime_value

TODO:

The property file argument in Dockerfile

ENTRYPOINT ["java", "-cp", "app.jar", "-Dsetting=${setting_env}", "example.EnvironmentPrinter"]

is not processed correctly, the log shows:

setting = ${setting_env}

need fix the interpolation to take place. e.g. adding an argument to the class

and

Dockerfile

ARG setting_name="setting_env"
ENTRYPOINT ["java", "-cp", "app.jar", "-Dsetting=${setting_env}", "example.EnvironmentPrinter", "${setting_name}"]

seems to not being interpolated:

docker build -t $IMAGE -f Dockerfile .
docker inspect $IMAGE | jq '.[].ContainerConfig.Cmd' 
[
  "/bin/sh",
  "-c",
  "#(nop) ",
  "ENTRYPOINT [\"java\" \"-cp\" \"app.jar\" \"-Dsetting=${setting_env}\" \"example.EnvironmentPrinter\" \"${setting_name}\"]"
]

and

docker run --name $NAME --rm -it $IMAGE

will show

Environment ${setting_name} = null
Property ${setting_name} = null

See Also:

Author

Serguei Kouzmine