简体   繁体   中英

Applying environment variables to JBoss Bootable Jar in OpenShift/Docker?

I have an application that I am testing in Docker as a JBoss bootable jar. The dockerfile adds the jar to the container, and then runs it. I'd like to pass the database credentials as environment variables for testing, and then as secrets in OpenShift. I tried adding the jar and manipulating it prior to running the CMD java -jar myapp.jar, but the path varies from build to build, and my attempt to capture the path doesn't work when I build the image. I also considered injecting the variables after the application is deployed. It worked, but it was a manual process.

Pre-altering the file did not work because as I was unzipping the files, I was unable to set a variable to store the random path.

FROM registry.redhat.io/ubi8/openjdk-17-runtime

USER root

ENV envhostname=localhost envusername=myappuser envpassword=myapppassword envSID=myappsid

RUN microdnf install fontconfig &&\
    microdnf install zip-3.0-23.el8.x86_64

USER 185

ADD myapp-0.0.1-SNAPSHOT-bootable.jar myapp-0.0.1-SNAPSHOT-bootable.jar

RUN unzip myapp-0.0.1-SNAPSHOT-bootable.jar
RUN     unzip -n wildfly.zip
RUN     export contentpath=$(find . -name "content" | grep -E 'content.+content')
RUN     unzip $contentpath
RUN     contentpath=${contentpath::-7}
RUN     sed -i "s/envhostname/$envhostname/i" $contentpath/WEB-INF/classes/myapp/common/bc4j.xcfg
RUN     sed -i "s/envusername/$envusername/i" $contentpath/WEB-INF/classes/myapp/common/bc4j.xcfg
RUN     sed -i "s/envpassword/$envpassword/i" $contentpath/WEB-INF/classes/myapp/common/bc4j.xcfg
RUN     sed -i "s/envSID/$envSID/i" $contentpath/WEB-INF/classes/myapp/common/bc4j.xcfg
RUN     zip -f -Ar $contentpath/content $contentpath/WEB-INF/classes/myapp/common/bc4j.xcfg
RUN     zip -f wildfly.zip $contentpath/content
RUN     zip -f myapp-0.0.1-SNAPSHOT-bootable.jar wildfly.zip
    
CMD java -jar myapp-0.0.1-SNAPSHOT-bootable.jar

And I don't know how to automatically trigger a shell script to run after the CMD.

There has to be some better way to handle this.

Oof. I just needed to think this through more. I solved this by modifying my Dockerfile as such

FROM registry.redhat.io/ubi8/openjdk-17-runtime

USER root

ENV envhostname=localhost envusername=myappuser envpassword=mapppw envSID=myapp

RUN microdnf install fontconfig &&\
    microdnf install zip-3.0-23.el8.x86_64

ADD myapp-0.0.1-SNAPSHOT-bootable.jar myapp-0.0.1-SNAPSHOT-bootable.jar
ADD prepcfg.sh prepcfg.sh

RUN chmod 777 myapp-0.0.1-SNAPSHOT-bootable.jar

USER 185

CMD ["./prepcfg.sh"]

and creating prepcfg.sh

#!/bin/sh

unzip myapp-0.0.1-SNAPSHOT-bootable.jar
unzip -n wildfly.zip
export contentpath=$(find . -name "content" | grep -E 'content.+content')
contentpath=${contentpath::-7}
cd $contentpath
unzip content
sed -i "s/envhostname/$envhostname/i" WEB-INF/classes/myapp/common/bc4j.xcfg
sed -i "s/envusername/$envusername/i" WEB-INF/classes/myapp/common/bc4j.xcfg
sed -i "s/envpassword/$envpassword/i" WEB-INF/classes/myapp/common/bc4j.xcfg
sed -i "s/envSID/$envSID/i" WEB-INF/classes/myapp/common/bc4j.xcfg
zip -f -Ar content WEB-INF/classes/myapp/common/bc4j.xcfg
cd ~
zip wildfly.zip $contentpath/content
zip myapp-0.0.1-SNAPSHOT-bootable.jar wildfly.zip
java -jar myapp-0.0.1-SNAPSHOT-bootable.jar 

Because I don't need to worry about running two commands when I can just initialize my JBoss server at the end of the script called in the Dockerfile.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM