Deploying sample spring boot app using Docker

Hello from ToraCode.

Now that we are set up with Docker virtualisation software. Let’s modify our pom.xml a little bit so that we can build our docker image.

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

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

    <plugin>
      <groupId>com.spotify</groupId>
      <artifactId>docker-maven-plugin</artifactId>
      <configuration>
        <imageName>toracode/example-app</imageName>
        <baseImage>java:8</baseImage>
        <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
        <!-- copy the service's jar file from target into the root directory of the image --> 
        <resources>
           <resource>
             <targetPath>/</targetPath>
             <directory>${project.build.directory}</directory>
             <include>${project.build.finalName}.jar</include>
           </resource>
        </resources>
      </configuration>
    </plugin>

Look a this code. For the image name we choose toracode/example-app, you can change it to your image name. For 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.

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

mvn clean package docker:build

In my case my project location is

/Documents/springspace/eampleapp

And I’m using mac. I’m using those commands:

cd Documents/springspace/AccountingApp && ./mvnw clean package docker:build

if you get exception like this:

java.lang.UnsupportedClassVersionError: org/apache/maven/wrapper/MavenWrapperMain : Unsupported major.minor version 51.0

then add java 8 to as your system variable. For mac you can use this command to temporarily add java as a system variable

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home

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

Building new docker image will take some time. After build is success you’ll see this success message on your terminal.

Screen Shot 2016-08-03 at 2.36.57 PM

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 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:

docker images

you’ll find your image on images list.

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

docker run -it -p 80:8080 toracode/example-app

Here -it stands for interactive terminal, -p is port, since docker is running on host machine that has port 80 and you container is listening port 8080, we have to map that two port so that on host machine it listens port 80 and maps that port on port 8080.

If everything is okay the your app is deployed successfully.

You’ll find a gretting like this:

Screen Shot 2016-08-03 at 2.59.23 PM

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

Look at my screen! I’m awesome, aren’t I?

Deployed Spring Boot App using Docker[/caption]

2 thoughts on “Deploying sample spring boot app using Docker

Leave a Reply

Your email address will not be published. Required fields are marked *