Docker Jboss/wildfly: How to add datasources and MySQL connector

I am learning Docker which is completely new to me. I already was able to create an jboss/wildfly image, and then i was able to start jboss with my application using this Dockerfile:

FROM jboss/wildfly

CMD ["/opt/jboss/wildfly/bin/standalone.sh", "-c", "standalone-full.xml", "-b", "0.0.0.0"]

ADD mywebapp-web/target/mywebapp-1.0.war /opt/jboss/wildfly/standalone/deployments/mywebapp-1.0.war

Now i would like to add support for a MySQL Database by adding a datasource to the standalone and the mysql connector. For that i am following this example:

Following is my dockerfile and my execute.sh script

Dockerfile:

FROM jboss/wildfly:latest

ADD customization /opt/jboss/wildfly/customization/

CMD ["/opt/jboss/wildfly/customization/execute.sh"]

execute script code:

#!/bin/bash

# Usage: execute.sh [WildFly mode] [configuration file]
#
# The default mode is 'standalone' and default configuration is based on the
# mode. It can be 'standalone.xml' or 'domain.xml'.

echo "=> Executing Customization script"

JBOSS_HOME=/opt/jboss/wildfly
JBOSS_CLI=$JBOSS_HOME/bin/jboss-cli.sh
JBOSS_MODE=${1:-"standalone"}
JBOSS_CONFIG=${2:-"$JBOSS_MODE.xml"}

function wait_for_server() {
  until `$JBOSS_CLI -c ":read-attribute(name=server-state)" 2> /dev/null | grep -q running`; do
    sleep 1
  done
}

echo "=> Starting WildFly server"

echo "JBOSS_HOME  : " $JBOSS_HOME
echo "JBOSS_CLI   : " $JBOSS_CLI
echo "JBOSS_MODE  : " $JBOSS_MODE
echo "JBOSS_CONFIG: " $JBOSS_CONFIG

echo $JBOSS_HOME/bin/$JBOSS_MODE.sh -b 0.0.0.0 -c $JBOSS_CONFIG &
$JBOSS_HOME/bin/$JBOSS_MODE.sh -b 0.0.0.0 -c $JBOSS_CONFIG &

echo "=> Waiting for the server to boot"
wait_for_server

echo "=> Executing the commands"
$JBOSS_CLI -c --file=`dirname "$0"`/commands.cli

# Add MySQL module
module add --name=com.mysql --resources=/opt/jboss/wildfly/customization/mysql-connector-java-5.1.39-bin.jar --dependencies=javax.api,javax.transaction.api

# Add MySQL driver
/subsystem=datasources/jdbc-driver=mysql:add(driver-name=mysql,driver-module-name=com.mysql,driver-xa-datasource-class-name=com.mysql.jdbc.jdbc2.optional.MysqlXADataSource)

# Deploy the WAR
#cp /opt/jboss/wildfly/customization/leadservice-1.0.war $JBOSS_HOME/$JBOSS_MODE/deployments/leadservice-1.0.war

echo "=> Shutting down WildFly"
if [ "$JBOSS_MODE" = "standalone" ]; then
  $JBOSS_CLI -c ":shutdown"
else
  $JBOSS_CLI -c "/host=*:shutdown"
fi

echo "=> Restarting WildFly"
$JBOSS_HOME/bin/$JBOSS_MODE.sh -b 0.0.0.0 -c $JBOSS_CONFIG

But I get a error when i run the image complaining that a file or directory is not found:

Building Image

$ docker build -t mpssantos/leadservice:latest .
Sending build context to Docker daemon 19.37 MB

Step 1 : FROM jboss/wildfly:latest
 ---> b8279b641e82

Step 2 : ADD customization /opt/jboss/wildfly/customization/
 ---> aea03d4f2819

Removing intermediate container 0920e2cd97fd

Step 3 : CMD /opt/jboss/wildfly/customization/execute.sh
 ---> Running in 8a0dbcb01855
 ---> 10335320b89d

Removing intermediate container 8a0dbcb01855

Successfully built 10335320b89d

SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

Running image

$ docker run mpssantos/leadservice
no such file or directory
Error response from daemon: Cannot start container 5d3357ba17afa36e81d8794f2b0cd45cc00dde955b2b2054282c4ef17dd4f265: [8] System error: no such file or directory

Can someone let me know how can i access the filesystem so i can check which file or directory is complaining? Is there a better way to debug this?

I believe that is something related with the bash which is referred on first line of the script because the following echo is not printed

Thank you so much

You should try to remove this for the debugging purpose. This file is probably missing / somewhere else and docker is not able to find it.

I am not familiar with Wildfly - Is it correct that you first run the standalone command and than add the warfile? And the cp is comment out.

Thank you for your response.

Yes i commented out the cp to try to find out what is going wrong. I found something strange:

  1. On my 1st Docker file that is working, with the ADD command i copy the war file and then i start the application server, and it works fine.

  2. I tried to copy to the deployments directory (yes it is stupid but it was just for test purpose) the execute.sh file and then execute from there because i know that i can copy the war. But when i try to execute the execute.sh file i get that same message: “no such file or directory”.

Basically i dont know what’s going on when i copy these files.

I made it to ssh the container.

  1. ssh to the docker machine: docker-machine ssh default
  2. checked the container id with the command: docker ps -a
  3. ssh to the container with the command: sudo docker exec -i -t 665b4a1e17b6 /bin/bash
  4. i can check that the “/opt/jboss/wildfly/customization/” directory exists with the expected files

The customization dir have the following permissions and is listed like this:

drwxr-xr-x   2 root    root    4096 Jun 12 23:44 customization
drwxr-xr-x 10 jboss jboss   4096 Jun 14 00:15 standalone

and the files inside the customization dir

drwxr-xr-x  2 root  root    4096 Jun 12 23:44 .
drwxr-xr-x 12 jboss jboss   4096 Jun 14 00:15 ..
-rwxr-xr-x  1 root  root    1755 Jun 12 20:06 execute.sh
-rwxr-xr-x  1 root  root  989497 May  4 11:11 mysql-connector-java-5.1.39-bin.jar

if i try to execute the file i get this error

[jboss@d68190e4f0d8 customization]$ ./execute.sh
bash: ./execute.sh: /bin/bash^M: bad interpreter: No such file or directory

Does this bring light to anything?

Thank you so much again

I found the issue. The execute.sh file was with windows eof. i converted to UNIX And start to work.

Thank you very much for your attention

I tried the exact scripts and steps shared here.

I am getting commands.cli not found