Docker is an invaluable tool to create portable images of Enterprise Resources. This opens to endless opportunities: for example, what if you need a database for WildFly but you don't want to install it on your local machine ? simply pull a Docker image of it and connect it to WildFly! It takes just a minute to do it. Let's see how to do it with MySQL Docker image.

First you need to install Docker on your machine. Once done, pull the latest MySQL database to your repository:

$ sudo docker pull mysql

 Great. Now start MySQL passing some environment arguments:

$ sudo docker run --name mysqldb -p \
-e MYSQL_USER=mysql -e MYSQL_PASSWORD=mysql -e MYSQL_DATABASE=demodb \
-e MYSQL_ROOT_PASSWORD=secret -d mysql

With the above command we are starting mysql image, tagging it as "mysqldb" and providing some evironment variables such as username, password, database. Also notice the port translation (-p) which will let you work on MySQL form your Host Machine on the address and port 3306 just like if it's running locally.

Great! Check that the Container has been created and that the process is running:

$ sudo docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                      NAMES
24b636c3e73b        mysql               "/ mysql"   About a minute ago   Up About a minute>3306/tcp   mysqldb

 As the image contains an address and port mapping, by executing an inspect over the Container ID should return the loopback address:

$ sudo docker inspect -f '{{ .NetworkSettings.IPAddress }}' 24b636c3e73b

Finally, you can import a dump or initial SQL script into your database, of course even if you don't have a MySQL client. Just inject the SQL into Docker!

$ sudo docker exec -i mysqldb mysql demodb -umysql -pmysql < dump.sql

 That's all! the database is now populated and you can check the Connection from WildFly. Install it as a module from WildFly CLI:

[standalone@localhost:9990 /] module add --name=com.mysql –resources=/var/mysql-connector-java-5.1.24-bin.jar --dependencies=javax.api,javax.transaction.api
[standalone@localhost:9990 /] /subsystem=datasources/jdbc-driver=mysql:add(driver-name=mysql,driver-module-name=com.mysql)
[standalone@localhost:9990 /] data-source add --jndi-name=java:/MySQLDS --name=MySQLPool --connection-url=jdbc:mysql:// --driver-name=mysql--user-name=mysql --password=mysql

 And verify that the Connections are valid:

[standalone@localhost:9990 /] /subsystem=datasources/data-source=MySQLPool:test-connection-in-pool
    "outcome" => "success",
    "result" => [true]

 Great! with little effort, you have a portable image of your database connected to WildFly!