How to configure application properties file dynamically in docker

4k Views Asked by At

I have a jar file that contains the application.properties file. can we configure the IP address and port number & username & password while running the docker image

Properties file location

App/bin/config/application.properties

Following are the application.properties

driverClassName = org.postgresql.Driver
url = jdbc:postgresql://localhost:5432/sakila
username = root
password = root
1

There are 1 best solutions below

0
Abdennour TOUMI On

entrypoint is the secret.

You have two solutions:

  • design the image to receive these parameters thru environment variables, and let the ENTRYPOINT injects them inside App/bin/config/application.properties

  • design the image to listen to a directory. If this directory contains *.properties files, the ENTRYPOINT will collect these files and combine them into one file and append the content with App/bin/config/application.properties

Both solutions have the same Dockerfile

From java:x

COPY jar ...
COPY myentrypoint /
ENTRYPOINT ["bash", "/myentrypoint"]

But not the same ENTRYPOINT (myentrypoint)

solution A - entrypoint:

#!/bin/bash
# if the env var DB_URL is not empty
if [ ! -z "${DB_URL}" ]; then
  
   echo "url=${DB_URL}" >> App/bin/config/application.properties
fi
# do the same for other props
#...
exec call-the-main-entry-point-here $@

To create a container from this solution :

 docker run -it -e DB_URL=jdbc:postgresql://localhost:5432/sakila myimage

solution B - entrypoint:

#!/bin/bash

# if /etc/java/props.d/ is a directory
if [ -d "/etc/java/props.d/" ]; then
   cat /etc/java/props.d/*.properties
   awk '{print $0}' /etc/java/props.d/*.properties >> App/bin/config/application.properties
fi

#...
exec call-the-main-entry-point-here $@

To create a container from this solution :

 docker run -it -v ./folder-has-props-files:/etc/java/props.d myimage