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:
- You have installed the (or updated to) the latest version of the version of the OpenShift Origin All-In-One Vagrant box.
- You have a GitHub account
- You have read about Source to Image (S2I) and very generally understand how it works
- You have read some of the general introduction to OpenShift and some of it's pieces
- 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
$ cd tmp
$ git clone https://github.com/wildfly/quickstart.git
$ cd quickstart
$ mv helloworld-rs ../my-rs-app #move the helloworld-rs directory up one directory and rename it mr-rs-app
$ cd ../my-rs-app/
$ gedit pom.xml #pick your favorite editor here
- 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.
$ git init
Initialized empty Git repository in /home/spousty/tmp/my-rs-app/.git/
$ git status
On branch masterInitial 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
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
- After logging in you should see all your projects. Go ahead and click on "New Project"
- Go ahead and give you project a new name (and fill in the other fields if you wish) and then click create:
- Pick the wildfly:10.0 template
- 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":
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:
- If the builder image (in this case the Wildfly builder) is not in the OpenShift cluster it needs to be pulled in
- 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)
- Once the compiling is complete that new Docker image, containing the compiled source and the app server, is written to the internal docker registry
- 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:
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
- Make a new project
$ oc new-project my-rs-app
- 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
- 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
- This gives us the name of the service we need for the final command:
$ oc expose service my-rs-app
- To see the final url you can use this command:
$ oc get route
NAME HOST/PORT PATH SERVICE TERMINATION LABELS
my-rs-app <em>my-rs-app-my-rs-app.apps.10.2.2.2.xip.io</em> my-rs-app:8080-tcp app=my-rs-app
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