Build JHipster application for and Run on Docker or Podman

Lucas Jellema

For the Open Day we organize shortly for our family, friends, neighbours and other interested parties, my colleagues create a custom web application to support our QR Scavenger Hunt through the offices and surrounding premises. This (progressive) web application is created using JHipster – leveraging Java, Angular, Spring, PostgreSQL and many more libraries and frameworks.

Early work in progress looks like this:

image

I have taken it upon myself to make this application available on a public cloud infrastructure so all our visitors can access it on their mobile phones. To keep things simple, I decided that I would build this JHipster application into a Container Image and run the application using Docker (or Podman) Compose on a cloud VM. (the step to take to Kubernetes seemed just a little too much for now).

I am by no means an expert in JHipster. Fortunately, for this particular challenge I do not have to know how to develop the application – only how to build and deploy it. The deployment is fairly straightforward: JHipster generates a yaml file that I can feed to Docker Compose or Podman Compose. Based on that file, two containers are started – one for a PostgreSQL database and a second one for the application (containing the Java backend based on Spring and handling HTTP requests from the browser that runs the also included Angular web application and using Hibernate and a JDBC connection to talk to the database in the first container). 

Once the application container image is built and pushed, running the application is as easy as:

docker-compose -f src/main/docker/app.yml up

or

podman-compose -f src/main/docker/app.yml up

The real challenge for me was how to build the JHipster Application Container Image. Part of the challenge was caused by the fact that I locally run a (not ideally configured) Podman based environment.

Build the JHipster Application Container Image

The Maven, Gradle and NPM build approaches for JHipster are extensive and well documented. They can easily be used to produce a Docker Container Image leveraging the local Docker daemon process. (read details in the docs).

However, in my environment I do not have a Docker daemon process running. JHipster also caters for that situation – using the Jib library that helps create container images for Java applications. The NPM build process cannot work with jib in this way, but both Maven and Gradle can. I am more familiar with Maven, so that is the option I go for.

All I should have to do at this point is determine what the artifact identifier for the container image should be and run this command:

./mvnw package -Pprod verify jib:build -Djib.to.image=<dockerhub-username>/<artifact-id>

Unfortunately, with the way I have set up Podman in my Ubuntu environment within Windows WSL2, the authentication details for connecting to Docker Hub cannot be retrieved by jib. I did not want to spend too much time on this almost one time only activity so I decided to go for the quick and dirty approach: including the registry authentication details in the command line statement. Really dirty – and also really quick.

The command that did it for me:

./mvnw package -Pprod verify jib:build -Djib.to.image=lucasjellema/amis-open-dag:0.0.1 -Djib.to.auth.username=lucasjellema -Djib.to.auth.password=$DOCKER_HUB_PASSWORD

image

The build process ran for a while and after creating the JAR file – where it normally might have stopped – dutifully continued to build the container image:

image

The image was built, tagged as lucasjellema\amis-open-dag:0.0.1 and pushed to Docker Hub.

The generated app.yml (in directory src/main/docker) needed a small edit – to set the proper name for the container image.

image

With this taken care of, the JHipster application (including its PostgreSQL database) could be started with a single and very simple command:

podman-compose -f src/main/docker/app.yml up

(I do not have docker-compose, that’s why podman-compose).

image

Note: this command will run the application in any environment, provided that environment has:

  • the app.yml file
  • Docker Compose or Podman Compose
  • access to Docker Hub (to retrieve container image)

Resources

JHipster Documentation – Docker and Docker Compose

jib documentation – what is Unauthorized?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: