Dockerizing a Java RESTfulAPI

Rafael Ferreira
4 min readMay 14, 2020

--

Generating docker image and dockerizing Springboot API

Containers, can you imagine that this representation would mean such a life saving in software engineering? The principle of container in IT world means that ‘Containers are isolated from one another and bundle their own software’ and it helps our application ecosystem and scalability.

In this article I’m going to show you how to Dockerize a Java API, presuming you already understand the principles of Docker and API Development.

The API we’ll be using was developed with the following technologies:

  • Java
  • Springboot
  • Maven
  • MongoDB
  • Docker

Here’s the link for the project https://github.com/rafaelferreiram/quick-api.git

Dockerfile Maven

To generate a docker image using maven we’re going to need to use a frameworks that helps to do so, and this guys is the dockerfile-maven a plugin created by Spotify to make the generation and the communication of a docker image easier.

Dockerfile

Before we get started creating our docker image we have to create a Dockerfile file, responsible for for exposing our jar and configurations to Docker.
This dockerfile must be generate in the project root just like the image below.

Pom.xml

After setting up our Dockerfile configs, we have to add the dockerfile-maven plugin to the project`s pom.xml under the tag <plugins>looking something like this.

Note that inside the plugin tag we have another tag ‘configuration’ where we can set our docker configs to our project such as

  • useMavenSettingsForAuth (we set true to set the Authentication by the Maven Settings)
  • repository (we have to informe our DockerID and the name of the image that we want, so {dockerID}/{imageName}, this is used to send our image to the docker hub.)
  • tag (version/imageTag.)
  • JAR_FILE (our .jar file generated by our project).

Docker image

Once we have our Dockerfile set up and our pom.xml configured, we can go onto the next step to generate a docker image.
To do so we have to go to our project root and run the following command, which will generate a .jar file inside the Target folder.

mvn package

After running the ‘mvn package’ we can check for our local docker images and see if its there. We can do it by running

docker images

Notice that we have our docker image generated with the same name as we did set in the tag ‘repository’ in our pom.xml

Running Docker Image

What better way to test an application than to run it ? So let’s do it .

We have two ways of running a docker image, the first one will run and show us the application log and the second one will run in background mode.

docker run -p 8080:8080 {dockerID}/brand-api:0.0.1-SNAPSHOT

To run it in background mode we just have to add a property in the command. ‘-d’.

docker run -d -p 8080:8080 {dockerID}/brand-api:0.0.1-SNAPSHOT

After starting up our RESTful API application we can check if it’s running by accessing this URL:

localhost:8080/brand/brands

As this URL represents a GET HTTP method, you can call it from a browser, i called if from Postman, but it won’t change the response.

There you go !! You’re applications has been Dockerized !!

What comes next ?

Well, we have dockerized our application, but if you want to go even further and send this image to the Docker Hub, you’ll have to a few more things, let me show you how.

Maven settings.xml

To send our docker image to the Docker Hub, first we have to configurate the maven settings.xml, so it will be able to communicate with docker hub.

With the ‘server’ tag configured, we should be good to publish it by running :

mvn dockerfile:push

Now if you access your account on the docker hub website you will se your docker image published there !

That’s all folks

Hopefully, I could help you understand a little bit about how to dockerize your Java Springboot application. Stay tuned for future articles that will be coming up soon.

If you have any comments or questions, I’d love to hear what you think.
You can also check my previous articles about Multithreaded Programming in Java, developing a CLI Application with Node.Js and Applying Single Responsibility Principle (SRP)

--

--