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));- pass through commandline
docker run -it -e VAR=value alpine:3.9 shin the container
/ # echo $varwill show
value
- pass via caller environment
export VAR=value
docker run -it -e VAR alpine:3.9 shin the container
/ # echo $VARwill show
value
- wih environment file
echo VAR=value>env.txt
docker run -it --env-file=$(pwd)/env.txt alpine:3.9 shin the container
/ # echo $varwill show
value
- with
docker-compose.yml
version: '3.7'
services:
example:
image: alpine:3.9
environment:
- VARexport VAR=value
docker-compose run -e VAR example shin the container
/ # echo $varwill show
value
alternativey define VAR in predefined filename .env
VAR=defaultand then no need to set it as command argument
docker-compose run example shin the container
/ # echo $varwill show
value
The host enviroment value, if set, still overrides the one in .env
- 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 $IMAGEto 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 $SETTINGwill show
setting_env = runtime_valueThe 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 $IMAGEwill show
Environment ${setting_name} = null
Property ${setting_name} = null- How To Pass Environment Docker
- Docker ARG, ENV and .env - a Complete Guide
- Understanding Docker Build Args, Environment Variables and Docker Compose Variables
- stackoverflow
- Docker & ENV: Methods for Passing Variables through Docker-Compose
- stackoverflow discussion on best approach for yaml file configuration passthrough
