Deploying applications on your Docker WildFly image

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

This is the second tutorial about Docker. In the first one Getting started with Docker and WildFly we have learnt how to install Docker on a Linux machine and pull a WildFly image on the top of it. Now we will learn how to deploy applications by using Docker Files.

First of all, what is a Dockerfile? a Dockerfile has a special mission: automation of Docker image creation. Once, you write build instructions into Dockerfile, you can build your own Docker images with custom environment configuration. 

A Dockerfile consists of a set of commands which can be written in a text file named "Dockerfile". The purpose of our first Docker file will be adding an application named "helloworld.war" in the deployments folder of the standalone installation. This will trigger automatic deployment.

Here we go:

vi Dockerfile

And enter the following instructions: 

FROM jboss/wildfly

ADD helloworld.war /opt/jboss/wildfly/standalone/deployments/

Now make sure that the helloworld application is in the same folder as the Dockerfile:

[root@localhost docker]# ls

Dockerfile  helloworld.war

To build a new Docker image is pretty simple, you have to choose a name (actually a tag) and issue a docker build command:

[root@localhost docker]# docker build --tag=wildfly-helloworld .

Step 0 : FROM jboss/wildfly

 ---> 365390553f92

Step 1 : ADD helloworld.war /opt/jboss/wildfly/standalone/deployments/

 ---> Using cache

 ---> fc3e0048139e

Successfully built fc3e0048139e

So we have successfully built the wildfly-helloworld image. Let's check it by issuing a docker images command:

[root@localhost docker]# docker images 

wildfly-helloworld   latest              fc3e0048139e        2 minutes ago      948.7 MB

jboss/wildfly        latest              365390553f92        3 weeks ago         948.7 MB

So now you can start Docker with just:

[root@localhost docker]# docker run -it wildfly-helloworld

The application server will start. Verify that the console that the application has been started:

18:18:47,682 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 28) JBAS0

18559: Deployed "helloworld.war" (runtime-name : "helloworld.war")

Fine, in order to test it, we need to find the IP address which has been chosen by Docker to bind the application server:

[root@localhost docker]# docker ps

CONTAINER ID        IMAGE                       COMMAND                CREATED             STATUS              PORTS                NAMES

b63e7345bf81        wildfly-helloworld:latest   "/opt/jboss/wildfly/   18 minutes ago      Up 18 minutes       9990/tcp, 8080/tcp   cranky_pasteur  

And then:

[root@localhost docker]# docker inspect -f '{{ .NetworkSettings.IPAddress }}' b63e7345bf81

172.17.0.4

 

Your application is ready to be tested at the address http://172.17.0.4:8080/helloworld

docker wildfly tutorial

Deploying applications using the Web Admin console

In order to deploy applications using the Web console we will need to add an user with add-user.sh script which is included in the bin folder of the application server. There are several way to do that, but we can have it done while building our image, so let's add to our Dockerfile the following command which will create an user in non interactive mode:

FROM jboss/wildfly

RUN /opt/jboss/wildfly/bin/add-user.sh admin Admin#70365 --silent

Now let's rebuild our image:

docker build --tag=wildfly-helloworld .

We will start now the application server. Since we need to access the management interfaces from our host machine, we need to bind them as well on the 0.0.0.0 IP (all available IP Addresses).

docker run -it wildfly-helloworld /opt/jboss/wildfly/bin/standalone.sh -b 0.0.0.0 -bmanagement=0.0.0.0

Fine. Inspect for the Container ID using docker ps and find the IP Address used by the application server:

[root@localhost docker]# docker ps

CONTAINER ID        IMAGE                       COMMAND                CREATED             STATUS              PORTS                NAMES

5935f7beb7d0        wildfly-helloworld:latest   "/opt/jboss/wildfly/   50 seconds ago      Up 49 seconds       9990/tcp, 8080/tcp   stupefied_bell   

And then: 

docker inspect -f '{{ .NetworkSettings.IPAddress }}' 5935f7beb7d0

172.17.0.5

Now log into the Admin Web console at 172.17.0.4:9990 and enter the credentials we have included into the Dockerfile

Great! we are into the Docker Admin Console. 

From there we can access the Runtime tab and add applications which are hosted on our machine.

docker tutorial wildfly

Further customization of the Docker file

So far we have learnt about the RUN command to execute the add-user.sh script. We will learn another command which is CMD. The main purpose of a CMD is to provide defaults for an executing container.

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

The advantage of including this command in the Docker images is that we don't need to specify every time the port bindings.

By the way, what is the difference between RUN and CMD ? With RUN the docker build will execute the commands. With CMD docker run will run this command by default.

So here is the full Dockerfile:

FROM jboss/wildfly

RUN /opt/jboss/wildfly/bin/add-user.sh admin Admin#70365 --silent

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

Once you have rebuilt your image, you can start your docker image with just:

docker run -it wildfly-helloworld

Referenceshttps://goldmann.pl/blog/2014/07/23/customizing-the-configuration-of-the-wildfly-docker-image/

I would like to thanks Marek Goldmann for the awesome contribution he is providing to the WildFly Docker Project. He will be soon here for an interview so stay tuned!

Follow us on Twitter