Getting Your Java EE code up and Running in Docker Containers on OpenShift

Getting Your Java EE code up and Running in Docker Containers on OpenShift

Greetings Shifters! Today I am going to give a very basic introduction to ripping apart the Jboss Wildlfy quickstart repository to build a base template for your JEE Application and then get it running in OpenShift V3. This certainly nothing complicated or advanced, but it does help you get a better understanding of how GitHub, OpenShift V3, Source2Image, and Java EE (my co-favorite development plaform) fit together. As a follow up to this post I will be covering how to then take this project and use it for day to day development.  Let’s go ahead and get started…

Assumptions:

  1. You have installed the (or updated to) the latest version of the version of the OpenShift Origin All-In-One Vagrant box.
  2. You have a GitHub account
  3. You have read about Source to Image (S2I) and very generally understand how it works
  4. You have read some of the general introduction to OpenShift and some of it’s pieces
  5. You know about how great JAX-RS and CDI are and you want to use them to build a REST backend service. If you want more background on JAX-RS and CDI we have you covered

Getting Our Code Template Built

There is a great set of quickstarts that allow you to work with different pieces of JavaEE or even larger projects.
Today we are going to be building a JAX-RS and CDI application (two great tastes that taste great together).
The template for this is located in a directory inside the quickstart repository. So rather than just fork and use the repo. we are going to bring it local, fix it, and then push to a new repo. The steps will look like cloning the GitHub repo., taking just the JAX-RS hello world example, rename, update, and then push into a new repository. Let’s get started
1. On your local machine go ahead and clone the repository.
$ cd tmp
$ git clone https://github.com/wildfly/quickstart.git
 2. Then go ahead and move the directory for JAX-RS to another directory outside of your git repo and rename it. Windows users I assume you know how to do this with explorer if you need a GUI.
$ cd quickstart
$ mv helloworld-rs ../my-rs-app  #move the helloworld-rs directory up one directory and rename it mr-rs-app
3. Now let’s go in and modify the Maven pom.xml to match our new name and our details.
$ cd ../my-rs-app/
$ gedit pom.xml #pick your favorite editor here
  1. Now change the entries I have marked in red to either the same values as me OR with details you want.

<project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd”>
<modelVersion>4.0.0</modelVersion>
<groupId>org.openshift.steve</groupId>
<artifactId>my-rs-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>My JAX-RS APPlication</name>
<description>Our base of a Jax-RS application</description>

<licenses>

We are not going to change anything else about the project for now. Time to push this up into a GitHub repo. we want. Go ahead and make a new GitHub repository for yourself and call it “my-rs-app”. My repository is available in GitHub if you want to clone or import it.

 github-new-repository

 

Click on the + symbol in the top right corner and then click the “New repository” link. This will bring you to a new repository details page. Please go ahead and fill out the name (and description if you want) then just click the “Create respository” button:
github-new-repository-details

 

Now it is time to do some work back on our local machine. Since our local directory is not already a git repository we need to turn it into a git repository, add the files, and then push it up into this repository. Go to the command line (on windows make sure the git command is available in the console) and execute the following commands.
 $ git init
Initialized empty Git repository in /home/spousty/tmp/my-rs-app/.git/

$ git status
On branch master

Initial commit

Changes to be committed:
 (use "git rm --cached <file>..." to unstage)

 new file: README.md
...

$ git commit -m "initial import"
[master (root-commit) a11c972] initial import
 7 files changed, 399 insertions(+)
...

 $ git remote add origin git@github.com:thesteve0/my-rs-app.git
 $ git push -u origin master

And now our repository is in GitHub and all set for the next stage – run our code! I hope you also appreciate you now have your own little JAX-RS and CDI template to use whenever you start a new REST web endpoint in Java EE – FTW!

Getting the Application Running

Again, I assume you have the OpenShift Origin All-In-One VM on your local machine. Getting this code running is easy with either the command line or the web console. Be sure to login in to either the CLI or the Web Console with username admin and password password.  The URL for the login in either the web console or to pass to the CLI is https://10.2.2.2:8443

Web Console

  1. After logging in you should see all your projects. Go ahead and click on “New Project”

    openshift-new-project-button

     

  2. Go ahead and give you project a new name (and fill in the other fields if you wish) and then click create:openshift-new-project-name 
  3. Pick the wildfly:10.0 template
    openshift-new-project-wildfly

 

  • Now go ahead and give a name that will be used as a label for all your resources created with the code and image. Then put in the git repo URL from the repo you made above. NOTE, make sure you use the HTTPS URL and not the SSH URL for the GitHub repository. Then you are all done – click “create”:openshift-new-project-code

 

That’s it! All the pieces needed to run your quickstart are set in motion. Some pieces will be created right away, such as the service and the route, while others will take a while, such as the actual pod with the built code. The reason this pod takes a while to build is because making a new image with source in it involves:

  1. If the builder image (in this case the Wildfly builder) is not in the OpenShift cluster it needs to be pulled in
  2. Once that is in, the assemble step in the builder image gets called, and the source is brought into the builder imaged and compiled (along with pulling in dependencies)
  3. Once the compiling is complete that new Docker image, containing the compiled source and the app server, is written to the internal docker registry
  4. Finally, once that runnable Docker image is in the registry it is pulled out by OpenShift to deploy into a pod.

In other words, be patient. You can go to the overview page for the project to watch the status. When it is finished you should see a screen that looks like this:

openshift-deployed-code

 

You can go ahead and click on the link I have marked and you should see the resulting web page. If you get a DNS error that means that your DNS provider is blocking the XIP.io service and you might need to try this fix (if you can change your DNS settings).

Command Line

I am assuming you have already logged into your cluster with the oc client tools. There are only three steps needed to deploy your code:
  1. Make a new project
    $ oc new-project my-rs-app
  2. Combine the STI Builder Docker image with our source code using the “new-app” command
    $ oc new-app openshift/wildfly-100-centos7~https://github.com/thesteve0/my-rs-app.git

    To follow progress you can either go to the web console and look at the overview, as shown in the previous image or you can run

    $ oc status
  3. While we are waiting for it to finish we can go ahead and link up a URL to the service in front of our appservers. Go ahead and do these commands to create an externably routable URL for your service:
    $ oc get service
    NAME CLUSTER-IP EXTERNAL-IP PORT(S) SELECTOR AGE
    my-rs-app 172.30.98.148 <none> 8080/TCP app=my-rs-app,deploymentconfig=my-rs-app 4m
  4. This gives us the name of the service we need for the final command:
    $ oc expose service my-rs-app
  5. To see the final url you can use this command:
    $ oc get route
    NAME HOST/PORT PATH SERVICE TERMINATION LABELS
    my-rs-app my-rs-app-my-rs-app.apps.10.2.2.2.xip.io my-rs-app:8080-tcp app=my-rs-app
 You can either go to the URL http://my-rs-app-my-rs-app.apps.10.2.2.2.xip.io directly or you can go to the web console for your OpenShift instance and click on the link there.

Conclusion

Today we saw how to take some code, from this case in GitHub, fork it, take it into our own GitHub repository, and then combine it with an S2I builder image (for Wildfly), to create a real application in OpenShift. There was a lot of ground covered but I hope this gave you a good start. The next post in this series is going to explore the next step, which is how to do your day in and day out development work using this exact same code and OpenShift project. In the mean time please feel free to leave any question or comments below. Thanks and happy building!

Categories
Java, JBoss, OpenShift Origin
Tags
, , ,
Comments are closed.