Running Java on JBoss Docker Container

Henry Delgado, BCS, MBA
3 min readJan 7, 2020

--

I have previously created a blog where I explain how to set up a local environment to work with JBoss/WildFly server. At the end of the second part of that tutorial, I asked the question “What is Next?” Well, this is an idea of what to do next: let us create a Docker container from a JBoss/WildFly image where we can run our Java web applications.

Docker allows us to bundle software, libraries and configuration files so that we can easily replicate any environment. It takes a lot of time and effort to set up our Java environment. Every new developer or QA engineer would have to set up the same local environment. What if they use more than one computer or what if they switch to a new one? There multiple scenarios where we can anticipate the need for a better solution to easily develop and test against a particular environment. For that reason, let us use Docker.

Requirements

  1. You should have a WAR file already created before starting this tutorial. Follow my blog on creating a basic Java web application if you do not have a WAR file already.
  2. You should also have Docker Desktop already installed on your computer. Switch to use Linux containers if you are using Windows.

How to run your Java web application on a JBoss/WildFly container:

  • Create a “dockerfile” in the same folder where you have your .war file.
  • Using your terminal (command prompt), navigate to where the dockerfile is located.
  • Run the following Docker command to build the JBoss Docker container based on the dockerfile that we just created.
docker build --rm -f "dockerfile" -t my-wildfly:latest "."

“dockerfile” refers to your dockerfile. That is the name and there is no extension. However, I used the name “jboss.dockerfile” in my previous snippet. In other words, use whatever name you used for your docker file.

  • Run a new Docker container based on the build that we just created (my-wildfly) using the following command:
docker run -p 8080:8080 -p 9990:9990 -it my-wildfly /opt/jboss/wildfly/bin/standalone.sh -b 0.0.0.0 -bmanagement 0.0.0.0

The previous command will expose the ports 8080 and 9990

  • Run the following Docker command to see the container id for the new container (“my-wildfly” is what we named the container in the previous snippet).
docker ps
  • Copy the container id for “my-wildfly”.
  • Run the following Docker command to inspect the container and get the public IP-address. Scroll down the results of that command until you see the IP-address.
docker inspect <your container id goes here>
  • Navigate to the container IP-address on port 8080 and use the proper URL to navigate to your web application. For example:
http://127.0.0.2:8080/student-app/index.html

You should see your web application!

On this basic tutorial, I explained how to create a docker file that would download a copy of the JBoss/WildFly image. The docker file also instructed to copy your Java application (.war file) to the container. Then we created and run a docker container based on that image. We inspected our container to find out the IP-address so that we could navigate to our web application inside the new Docker container.

--

--

Henry Delgado, BCS, MBA

A passionate software developer eager to learn, evaluate and share!