dockerpasswordsdatasourcewebsphere-libertyserver.xml

How to pass datasource password to websphere liberty container during runtime


I am trying to run my app in websphere liberty profile container. I want to have 1 image which can be run on diff env(dev, st, sit etc). I can use env variable to pass the values at runtime to the container. How to use those in wlp settings? Is this possible?

In the server.xml, I have defined the datasource with all config properties like connection string, username and password. I have built the image using this file. Now, I want to test the same image in different env by passing the values as env variable instead of hardcoding in the server.xml. I couldn't find a way where server.xml can read the env var directly and replace the password variable. Other way I tried is using the bootstrap.properties and so server.xml can read the values from that file. But here also, I have to provide the bootstrap.properties during image building and I can't change the values during runtime.

lines in server.xml:

<dataSource name="XYZ" jndiName="jdbc/xyz" transactional="false"> <jdbcDriver id="OracleJdbcDriver" libraryRef="xyzLib"/> <properties.oracle URL="${db.url}" user="${db.username}" password="${db.password}"/> </dataSource>

db.url, db.username and db.password are defined in bootstrap.properties which is packaged in the image during build time.


Solution

  • From this:

    The following predefined variables can be referenced:
    * directory properties, see Liberty: Directory locations and properties
    * JVM system properties
    * process environment variables

    If the same variable is specified in multiple places, the precedence is as follows:
    * variables in bootstrap.properties override the process environment variables
    * variables in server.xml, or included XML files, override the variables in bootstrap.properties and process environment variables

    Use process environment variables in the configuration. Process environment variables are available if you use the env. configuration variable prefix, for example:

    <fileset dir="${env.LIBRARY_DIR}" includes="*.jar"/>
    

    For more information on specifying environment variables, see Customizing the Liberty environment.

    Then, a probable solution is:

    server.xml:

    <properties.oracle URL="${env.db_url}" user="${env.db_username}" password="${env.db_password}"/>
    

    Pass environment to container just when run it:

    docker run -d -e db_url=xxx -e db_username=xx -e db_password=x your_image
    

    Then, different values will be passed to different containers & finally referenced by server.xml with env. format.

    UPDATE:

    From @Lata 's try as next:

    I tried the scenario with capital and small letters. What I got is it doesn't matter which casing you use in server.xml. But while calling the docker run, you have to pass the variables in CAPS only to work.

    So, the final conclusion should be: no matter CAPS or LOW case letter in server.xml, but need to pass CAPS env to docker run. As docker not have such limits, so definitely websphere enforce this limit.