Passing environment variables to a docker image to a file in tomcat

Expected behavior

When passing Environment variables via docker -d -e var1=value1 -e var2=value2 -e var3=value3 image_tag_name
It should pass those variables to container and whichso ever file we are using them like in xyz.properties if we use /usr/local/tomcat/webapps/applicationname/context/class/xyz.properties

and run smoothly

Actual behavior

docker -d -e var1=value1 -e var2=value2 -e var3=value3 image_tag_name
if not setting value in xyz.properties file

Information

while passing environment variables in xyz.properties they are not getting reflected.

But when i exec the container i am able to see environment variable set in container.
using docker exec -it container id sh
and then env to show environment variables.
entries in xyz.properties
database host = ${DB_HOST}
database username = ${DB_USER}
database password = ${DB_PASSWORD}

Variables passed are of database host name, userid and password .

Tomcat does not pass environment variables to an Application that way.

You need to specify the environment variables in the setenv.sh script (Linux) or setenv.bat (Windows).

Linux

cat setenv.sh
export DB_HOST="myhost"
export DB_USER="my-user"
export DB_PASSWORD="secret"

And your Java Application must retrieve them from environment variables.

database_host=System.getenv("DB_HOST")
database_username=System.getenv("DB_USER")
database_password=System.getenv("DB_PASSWORD")

You can then map that setenv.sh file as a volume to the container.

./setenv.sh:/usr/local/tomcat/bin/setenv.sh

For more reference:

Deploying a Java Tomcat App stack with environment variables

The Java Application Code example