如何使用Docker在Wildfly中将变量作为属性传递给xml配置文件 [英] How to pass variable as attribute to xml configuration file in Wildfly with Docker

查看:166
本文介绍了如何使用Docker在Wildfly中将变量作为属性传递给xml配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将值从docker-compose.yml文件动态传递到Wildfly配置。
我想灵活地配置邮件-只是为了快速更改地址,用户名或端口。

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..

在这种情况下,我尝试做通过转发来自docker-compose.yml的环境变量,通过dockerfile作为参数 -Dargumentname = $ environmentvariable。
当前以错误开始的野生蝇间断:

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线程
池-45)WFLYCTL0013:操作(添加)失败-地址:([[
( subsystem => mail),
( mail-session => default)])-失败描述: WFLYCTL0097:错误的ssl类型。预期为[BOOLEAN],但为STRING。

[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"

相同的情况,如果我尝试将PORT作为值传递到出站套接字绑定块中。

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

我不知道如何从docker-compose文件中将整数/布尔值传递给Wildfly配置。

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

docker -compose.yml(部分)

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:

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:

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>
...


推荐答案

几乎在那里。在docker文件中,您已经定义了环境变量,因此您需要在wildfly配置中将它们作为环境变量引用。最简单的方法是用 env。前缀为环境变量添加前缀。因此,在您的示例中,您具有环境变量 HOST SSL USERNAME ...,您可以在standalone.xml中像这样引用:

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>

没有 env。前缀,jboss / wildfly将尝试将表达式解析为jvm属性,您必须将其指定为jvm -D 标志。

您还可以使用默认值fallback在您的表达式中,例如:

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}"

这样,ssl将被设置为名为 SSL 的环境变量的值,如果不存在此类var,则服务器将回退到 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

这篇关于如何使用Docker在Wildfly中将变量作为属性传递给xml配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆