Locally hosted DevOPS Part 2: The Java Application

Welcome to part 2 of this weird project idea to host an entire DevOPS pipeline on one machine by using VMs. Be warned that there’s no real reason for doing this other than curiosity and because I don’t have access to a bunch of servers as one would when setting up a real DevOPS pipeline. mexico ivermectin trial

In the previous post on this topic we covered some very basic stuff about setting up Virtual Machines, and we finished off by creating a git server on one of our VMs.

In this post we’re going to give ourselves something to deploy. We’re setting up a deployment pipeline after all, we need something to actually deploy. So we’ll cover:

  • creating a Java application
  • packaging it as a Docker image
  • testing it locally

I was hoping to get as far as setting up a Jenkins build server in this post but it already got super long, so we’ll leave it at that for now and next time we’ll cover the build server part of it.

So still outstanding on our pipeline:

  • build automation
  • deployment to Kubernetes

Creating a Java Spring Boot project

Hie thee over to start.spring.io. Select the dependency of Spring Web and leave everything else as default (Java 11 in particular as the java version). Click “generate”. Extract demo.zip to the local copy of your git repo that we created previously.

You can now test your build by running mvn clean package in the directory for the project. You will need to install Java 11 (OpenJDK for preference) and the latest maven if you haven’t already got them.

You can run the built application with:

java -jar target/demo-0.0.1-SNAPSHOT.jar 

And with any luck you should see the application running on localhost:8080 which if you choose to visit, will give you a charming error message about a white label error page, which sounds like something right wing nutjobs get excited about.

Add target/ to the .gitignore file to avoid annoyance, and git commit what you’ve got so far.

Packaging the application as a Docker image

I actually had to figure out how to install docker on my local (Manjaro) for the next step, as I want to test that everything works locally before pushing it to the git server. I followed the instructions here and they worked immaculately, which just goes to show that Manjaro is awesome. If you’re running Ubuntu or some other OS then you can go to here for installation instructions. If you’re installing on Linux take note of any post-install steps.

OK. So now. “What exactly is a Docker Image?” I hear you cry, if you don’t know, which you probably do so this is pointless. how much does ivermectin 3mg cost A docker image is likeā€¦ a mini-VM which runs a single program. You basically package up alla y’all’s dependencies, and you build a binary file image which can be run by Docker (or other container tools like Kubernetes) inside a container, which is an isolated environment where your program thinks it’s the only program in the whole world.

Don’t look at me like that, I’m not making this stuff up. It’s real.

To package this application as a Docker image I followed the guide here, but updated it for Java 11 and Maven 3.6.3. To find the correct names for the docker images I wanted to use I went to docker hub and poked around until I found the right ones.

I finally ended up with a docker file that looks like this:

FROM maven:3.6.3-openjdk-11-slim as maven

COPY ./pom.xml ./pom.xml

COPY ./src ./src

RUN mvn dependency:go-offline -B

RUN mvn package

FROM openjdk:11.0.9.1-jre-slim-buster

WORKDIR /demoDeploy

COPY --from=maven target/demo-*.jar ./demoDeploy/demo.jar

CMD ["java", "-jar", "./demoDeploy/demo.jar"]

What’s nice about this docker file is that the whole process of building the application and packaging it as a Docker image can now be carried out in one step:

docker build -t local-devops-demo .

The first time you do this it will take forever ‘cos it goes off to maven central and basically downloads the internet as far as I can tell, but after it’s done it once it doesn’t need to do it again, so your subsequent builds should be much faster.

Testing the Docker image

Once the image has built you can test that it worked with

docker run -p 8080:8080 local-devops-demo

You should see the logs as the application starts up and if you go to localhost:8080 with your browser you should get the same error page that you got when you ran the application locally.

You can check the size of the docker image produced with the docker images command:

REPOSITORY			SIZE
local-devops-demo		222MB

Kinda obviously, you want it to be small.

So ja, that’s about it. We’ve created a basic Spring Boot application and packaged it as a Docker image, next we need to make a build server so that every time we push to git we trigger a build.

About the Author