Skip to content

Wildfly custom Docker image prepared for MySQL connection.

License

Notifications You must be signed in to change notification settings

esign-consulting/wildfly-mysql

Repository files navigation

Wildfly custom Docker image prepared for MySQL connection

License: MIT Docker Build status Docker Pulls

This custom Wildfly image is built based on the official jboss/wildfly:13.0.0.Final image.

MySQL connection

The image brings the version 8.0.12 of the official JDBC driver for MySQL (Connector/J).

During the setup, the MySQL instance is checked until it is ready for connection. This is done during the execution of wait-for-mysql.sh, the image entrypoint.

A very simple Java class is responsible for testing the connection: TestConnection.java. The class is compiled inside the image, during its building process.

Required environment variables

The following environment variables are mandatory in order to run appropriately the image:

  • MYSQL_HOST - The name of the MySQL host. If the MySQL port is other than the default 3306, add :<port_number>.
  • MYSQL_DATABASE - The name of the database used by the application deployed in Wildfly.
  • MYSQL_ROOT_PASSWORD - The MySQL root user password.
  • MYSQL_USER - The database superuser.
  • MYSQL_PASSWORD - The database user password.

If you are using this image together with the official MySQL image, set the same values to MYSQL_DATABASE, MYSQL_ROOT_PASSWORD, MYSQL_USER and MYSQL_PASSWORD for both images.

Wildfly datasource

The image is also built with a pre-configured datasource, defined in the file standalone.xml. As can be seen in the xml below, the configuration depends on the environment variables.

<datasource jndi-name="java:jboss/${env.MYSQL_DATABASE}" pool-name="${env.MYSQL_DATABASE}">
    <connection-url>jdbc:mysql://${env.MYSQL_HOST}/${env.MYSQL_DATABASE}?serverTimezone=UTC</connection-url>
    <driver>mysql</driver>
    <security>
        <user-name>${env.MYSQL_USER}</user-name>
        <password>${env.MYSQL_PASSWORD}</password>
    </security>
    <validation>
        <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
        <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
    </validation>
</datasource>

Notice that both jndi-name and pool-name depend on the name of the database, defined in MYSQL_DATABASE. You must take it into consideration for JNDI bindings within your application code.

Extending this image

If your Java application is deployed in Wildfly and uses a MySQL database, create a Dockerfile like the example:

FROM esignbr/wildfly-mysql

ADD target/application.war /opt/jboss/wildfly/standalone/deployments/

I'm assuming you've used Maven to build your application, thus the war file is in the target folder. Wildfly expects the packaged application file inside /opt/jboss/wildfly/standalone/deployments/, for automaticaly deploying it on boot.

You can then build your own application Docker image:

docker build -t myimages/myapplication .

Running your own image

Don't forget to define appropriately the environment variables during the image initialization. If your environment is whole Docker, all of your services run in containers, you can use a docker-compose.yml like the example:

version: '3'
services:
  myapp:
    image: myimages/myapplication
    container_name: myapp
    environment:
      - MYSQL_HOST=mysql
      - MYSQL_DATABASE=myapp
      - MYSQL_ROOT_PASSWORD=secret
      - MYSQL_USER=dbuser
      - MYSQL_PASSWORD=dbpass
    depends_on:
      - mysql
  mysql:
    image: mysql:8
    container_name: mysql
    environment:
      - MYSQL_DATABASE=myapp
      - MYSQL_ROOT_PASSWORD=secret
      - MYSQL_USER=dbuser
      - MYSQL_PASSWORD=dbpass

You can then run your application and MySQL together:

docker-compose up -d

Releases

No releases published

Packages

No packages published