Database WebApp with Wildfly and mySQL in Docker container

  Docker, mySQL, Servlet, Wildfly

Wildfly Docker container

Remember that we wanted to create a second docker container for the wildfly web application container. The creation will be significantly more complex than for the mySQL container because we have to customize this container to suit our needs. Such customization will typically go into the Dockerfile. This file contains all customization commands that define the docker image that will be created from it.
In order to run our wildfly container and have it connect to the the mySQL container, see what customization tasks we have to do for the wildfly base docker image:

  • Starting WildFly server
  • Add a wildfly management user
  • Downloading the MySQL database driver
  • Adding a wildfly/JBoss module for the database driver
  • Add a corresponding datasource to the wildfly configuration
  • Setting the auto-deploy-zipped deployment scanner attribute to false in order to prevent a perpetuous loop of deploying and undeploying of web apps
  • Shutting down WildFly and Cleaning up
  • EXPOSE ports 8080 and 9990
  • Restarting wildfly server with optione

The following Dockerfile contains everything thats need to be done. When you build an image with it, the container will allow you to
– Connect to the Wildfly management console via http://localhost:9990/console
– Deploy a web application that requires a mySQL Datasource for establishing a connection to the mySQL container

FROM jboss/wildfly:latest

# Appserver
ENV WILDFLY_USER admin
ENV WILDFLY_PASS adminpw

# Database
ENV DB_NAME sakila
ENV DB_USER root
ENV DB_PASS my-secret-pw
ENV DB_URI 172.17.0.2:3306

ENV MYSQL_VERSION 8.0.23
ENV JBOSS_CLI /opt/jboss/wildfly/bin/jboss-cli.sh
ENV DEPLOYMENT_DIR /opt/jboss/wildfly/standalone/deployments/
#ENV JAVA_OPTS

# Setting up WildFly Admin Console
RUN echo "=> Adding WildFly administrator"
RUN $JBOSS_HOME/bin/add-user.sh --silent=true ${WILDFLY_USER} ${WILDFLY_PASS} ManagementRealm

# Configure Wildfly server
RUN echo "=> Starting WildFly server" && \
      bash -c '$JBOSS_HOME/bin/standalone.sh &' && \
    echo "=> Waiting for the server to boot" && \
      bash -c 'until `$JBOSS_CLI -c ":read-attribute(name=server-state)" 2> /dev/null | grep -q running`; do echo `$JBOSS_CLI -c ":read-attribute(name=server-state)" 2> /dev/null`; sleep 1; done' && \
    echo "=> Downloading MySQL driver" && \
      curl --location --output /tmp/mysql-connector-java-${MYSQL_VERSION}.jar --url http://search.maven.org/remotecontent?filepath=mysql/mysql-connector-java/${MYSQL_VERSION}/mysql-connector-java-${MYSQL_VERSION}.jar && \
    echo "=> Adding MySQL module" && \
      $JBOSS_CLI --connect --command="module add --name=com.mysql --resources=/tmp/mysql-connector-java-${MYSQL_VERSION}.jar --dependencies=javax.api,javax.transaction.api" && \
    echo "=> Adding MySQL driver" && \
      $JBOSS_CLI --connect --command="/subsystem=datasources/jdbc-driver=mysql:add(driver-name=mysql,driver-module-name=com.mysql,driver-class-name=com.mysql.cj.jdbc.Driver)" && \
    echo "=> Creating a new datasource" && \
      $JBOSS_CLI --connect --command="data-source add \
        --name=${DB_NAME}DS \
        --jndi-name=java:/jdbc/datasources/${DB_NAME}DS \
        --user-name=${DB_USER} \
        --password=${DB_PASS} \
        --driver-name=mysql \
        --connection-url=jdbc:mysql://${DB_URI}/${DB_NAME} \
        --use-ccm=false \
        --max-pool-size=25 \
        --blocking-timeout-wait-millis=5000 \
        --enabled=true" && \		
	echo "=> Setting auto-deploy-zip to false" && \
      $JBOSS_CLI --connect --command="/subsystem=deployment-scanner/scanner=default:write-attribute(name=auto-deploy-zipped,value=false)"  && \		
    echo "=> Shutting down WildFly and Cleaning up" && \
      $JBOSS_CLI --connect --command=":shutdown" && \
      rm -rf $JBOSS_HOME/standalone/configuration/standalone_xml_history/ $JBOSS_HOME/standalone/log/* && \
      rm -f /tmp/*.jar

# Expose http and admin ports
EXPOSE 8080 9990

#echo "=> Restarting WildFly"
# Set the default command to run on boot
# This will boot WildFly in the standalone mode and bind to all interfaces
CMD ["/opt/jboss/wildfly/bin/standalone.sh", "-b", "0.0.0.0", "-bmanagement", "0.0.0.0"]

We could create the docker image and container from the above Dockerfile in one single step, but let’s make it two. The first step is to create a docker image.
docker build -t myCustomWildfly25Image .
The above command requires that the corresponding Dockerfile is available in the current directory. That’s what the “.” is for. The -t parameter gives the docker image a name which is easier to handle than the default image ID. In the next step, we will create a docker container based on the image we just created.
docker run --name wildfly25 -p 8080:8080 -p 9990:9990 -v C:\[CUSTOM_PATH]\JBoss_deployments:/opt/jboss/wildfly/standalone/deployments/:rw -it myCustomWildfly25Image
We now have two container, one which holds a mySQL server and the other one holding the Wildfly application server. The next step is to link both container to the same virtual network.

LEAVE A COMMENT