How to pass variable as attribute to xml configuration file in Wildfly with Docker

时光总嘲笑我的痴心妄想 提交于 2019-12-08 04:55:41

问题


I'm trying to pass values from docker-compose.yml file to Wildfly configuration dynamically. I want to have flexibility of mail configuration - just for quick change of addres, or username, or port..

In this case, I tried to do that by forwarding environment variables from docker-compose.yml, by dockerfile as arguments "-Dargumentname=$environmentvariable. Currently wildfly interupts on start with error:

[org.jboss.as.controller.management-operation] (ServerService Thread Pool -- 45) WFLYCTL0013: Operation ("add") failed - address: ([ ("subsystem" => "mail"), ("mail-session" => "default") ]) - failure description: "WFLYCTL0097: Wrong type for ssl. Expected [BOOLEAN] but was STRING"

Same situation, if I try to pass PORT as value in outbound-socket-binding block.

I have no idea how to pass integers/booleans from docker-compose file to Wildfly configuration.

docker-compose.yml (part)

...
    services:
        some_service:
            image: image_name:tag
            environment:
             - USERNAME=some_username@...
             - PASSWORD=some_password
             - SSL=true // I also tried with value 1
             - HOST=smtp.gmail.com
             - PORT=465 // also doesn't work
...

Dockerfile:

FROM some_wildfly_base_image

# install cgroup-bin package
USER root
RUN apt-get update
RUN apt-get install -y cgroup-bin
RUN apt-get install -y bc

USER jboss
ADD standalone-myapp.xml /opt/jboss/wildfly/standalone/configuration/
ADD standalone.conf /opt/jboss/wildfly/bin/
ADD modules/ /opt/jboss/wildfly/modules/

RUN wildfly/bin/add-user.sh usr usr --silent

# Set the default command to run on boot
# This will boot WildFly in the standalone mode and bind to all interface
CMD [ "/opt/jboss/wildfly/bin/standalone.sh", "-c", "standalone-myapp.xml", "-Dmail.username=$USERNAME", "-Dmail.password=$PASSWORD", "-Dmail.ssl=$SSL", "-Drm.host=$HOST", "-Drm.port=$PORT" ]

standalone-myapp.xml:

...
    <subsystem xmlns="urn:jboss:domain:mail:2.0">
        <mail-session name="default" jndi-name="java:jboss/mail/Default">
            <smtp-server password="${mail.password}" username="${mail.username}" ssl="${mail.ssl}" outbound-socket-binding-ref="mail-smtp"/>
        </mail-session>
    </subsystem>
...

    <outbound-socket-binding name="mail-smtp">
        <remote-destination host="${rm.host}" port="465"/>
    </outbound-socket-binding>
...

回答1:


Almost there. In your docker file, you have defined environmental variables therefore you need to reference them as environmental variables in your wildfly config. The easiest way is to prefix your env var with env. prefix. So in your example, you have env variables HOST, SSL, USERNAME... which you can reference in standalone.xml like this:

<smtp-server password="${env.PASSWORD}" username="${env.USERNAME}" ssl="${env.SSL}" outbound-socket-binding-ref="mail-smtp"/> </mail-session>

Without env. prefix, jboss/wildfly will try to resolve the expression as jvm property, which you'd have to specify as jvm -D flag.
You can also use default value fallback in your expressions such as:

ssl="${env.SSL:true}"

This way, the ssl will be set the the value of environmental variable named SSL, and if such var does not exist, server will fallback to true.
Happy hacking



来源:https://stackoverflow.com/questions/44760127/how-to-pass-variable-as-attribute-to-xml-configuration-file-in-wildfly-with-dock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!