5 Steps to deploy spring boot application using Docker

5 Steps to deploy spring boot application using Docker

1. Overview

I’ll discuss how to deploy Spring Boot app on your local machine using Docker.

Wait, what is Docker? I didn’t know about that!! why didn’t you tell us about docker in the first place?

Well, if you don’t know about docker yet, it’s fine. Let me explain a little bit for you.

Docker is a virtualization management software for managing containers and images.

Now, what are the containers and images? okay let me clear some concepts here

  • A container is a runtime environment for images
  • An image is a software that you run within the containers.
  • The image is built using layers. Suppose you have an existing image, you added your app and built a new image and other people can build an image on top of your image.

Screen Shot 2016-08-22 at 8.32.52 PM

  • Suppose you have a spring boot app and you built it using Java 8. So your base image is Java 8 and after adding your app you’ll create a new image. Just like layers, you see? Java 8 API is a base layer, and you added your code/layer and made a new image. Other people can add their code/layer and create their own image. It works like that.

So what docker does actually?

  • Build that image
  • Deploy those images into containers
  • And manage those containers like starting and stopping those containers.

Why should we use Docker in the first place?

  • It’s easy to build docker images and put it into containers
  • It’s lightweight
  • It’s cloud agnostic. That means you don’t have to worry about a particular version of Linux is available or not, several cloud platforms support docker and you can just move your docker container from one cloud platform to another.
  • And it’s scalable. You can startup multiple containers with your image in a matter of seconds.

So let’s install Docker. it’s available for Linux, Mac or Windows.

2. Install Docker

Download Docker for your platform and install it.

You can check if it’s properly installed by running a command

It shows the existing containers running with docker. Otherwise, try adding docker to your environment variable.

Okay great!

Now that you are set up for using Docker. We’ll now deploy a sample spring boot app using Docker.

3. Add Maven Plugin

We have to add Docker Maven Plugin in our pom.xml file.

Inside our <plugins> </plugins> tag we need to add this plugin.

Look this code. For the image name we choose toracode/example-app, you can change it to your image name. For the base image, we wrote java:8.

Remember, we talked about our image is built on top on a base image here. We see java:8 is our base image here. Since spring boot needs Java 8 to run, we can’t use older versions than Java 8.

The <resources> tag tell what to copy into the root of the file system, in this case, its build jar file of our application and the <entryPoint> tag is what we need to run this jar file inside the container.

4. Build Docker Image

Now open your Terminal and cd to your project directory. And Run this command to build the image.

In my case my project location is

As I’m using a mac, although commands are similar in both Mac and Linux

If you get an exception like this:

then add Java 8 to as your system variable. For Mac, you can use this command to temporarily add Java as a system variable

or if you are on windows I’m referring you here.

Building new Docker image will take some time. After a successful build, you’ll see this success message on your terminal.

and you’ve successfully built your software image on top of Java 8 system image.

Wait, what?? where is Java 8 image then?

Okay, let me show you something. Scroll up to your terminal and let’s see if you can find the docker is downloading Java 8 image from their repository. If you found this you’re awesome, but if not it’s not my fault. Please consult an eye specialist 😛

If you want to verify if your docker image is built, you can run this command:

you’ll find your image on images list.

5. Run Docker Image

Now it’s time to finally run our application on our local machine. To run this docker image we have to execute this command:

Here -it stands for an interactive terminal, -p is the port, since docker is running on a host machine that has port 80 and your container is listening port 8080, we have to map that to two ports so that on host machine it li to tens port 80 and maps that port on port 8080.

If everything is okay the your app is deployed successfully.

Open a browser, and enter your ip address on address bar. Bamn!! you’re app is running!!

5 mistakes Java developers make that prevent them from working on awesome projects

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 806 other subscribers

%d bloggers like this: