How to write a Dockerfile to deploy my war file into jboss 7.2?

Viewed 569

I want to write a Dockerfile to deploy my war file into jboss7.2. In my existing jboss 7.2 we have used keystore files as well as database server configuration. So, how do my dockerfile will do those configurations ?

1 Answers

Actually, the Dockerfile is responsible just for the image build, it can't include all the configurations mentioned.

Here is a Dockerization Design in your case

  1. Dockerfile

Your Dockerfile needs to have a base image with JDK and JBoss with your preferred Linux distro (check the DockerHub for this) and you need to copy your application war and your standalone.xml configuration (adapted in the 2nd section) in the target directories then finally set an Entrypoint or a Command to launch the application server.

Here is an Example :

FROM jboss/wildfly:16.0.0.Final

USER jboss
WORKDIR /home/jboss

#Copy your application war and configuration file  :  
COPY ./app_runtime_resources  <runtime_path_in_container>

#Launch the application server with the appropriate options 
CMD ${JBOSS_HOME}/bin/standalone.sh  -c app-standalone.xml \
                 -Djboss.server.base.dir=<runtime_path_in_container> ${ADDITIONAL_OPTS}
  1. Database credentials

For the Database credentials the easiest way is to pass them to the container as environment variables :

 docker run -e DB_URL=<myURL>  -e DB_USERNAME=<myuser> \
            -e DB_PASSWORD=<mypassword>  myAppImage:latest

and then you need to change the properties values in the standalone.xml to point to the environment variables, Jboss supports environment variables as properties in the configuration file using the "env" keyWord as follow ${env.ENV_NAME}. Here is an example of a Datasource configuration :

     <datasources>
        <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true" statistics-enabled="${wildfly.datasources.statistics-enabled:${wildfly.statistics-enabled:false}}">
            <connection-url>${env.DB_URL}</connection-url>
            <driver>h2</driver>
            <security>
                <user-name>${env.DB_USERNAME}</user-name>
                <password>${env.DB_PASSWORD}</password>
            </security>
        </datasource>
        <drivers>
            <driver name="h2" module="com.h2database.h2">
                <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
            </driver>
        </drivers>
    </datasources>

you can add default values too, for more info about the "env.ENV" property option check this Guide How to use environment variables in WildFly configuration (standalone.xml or host.xml).

IMPORTANT: in case you are going to deploy your application as a service check if your orchestrator supports secrets for better security. both Docker swarm and Kubernetes support secrets for sensitive data (Here is an example for docker swarm ).

  1. Keystore files

For the Keystore, you can create a shared volume to handle the Keystore files in the docker run command : -v <keystore_files_path_localy>:/app/Keystore

 docker run -e DB_URL=<myURL>  -e DB_USERNAME=<myuser> \
            -e DB_PASSWORD=<mypassword>  \
            -v <keystore_files_path_localy>:/app/keystore \
            myAppImage:latest

you are going to use volumes in log persistence too in production check docker volumes doc for more info Use volumes, Docker docs.

Related