Docker : Spring boot with docker

In the previous post we saw docker basic concepts and installation. Here we will develop a spring boot app and create an image of it and run it on docker. I will be using maven as a build tool for the same. The final project structure would be like:

docker-spring-boot-basic1

Lets start with our pom.xml:


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.mynotes</groupId>
<artifactId>spring-boot-docker-start</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-boot-docker-start</name>
<description>Demo project for spring-boot-docker-start</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>

<properties>
<java.version>1.8</java.version>
<docker.image.prefix>spring-boot-docker</docker.image.prefix>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.11</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
</project>

Notice the docker-maven-plugin. The configuration specifies 3 things:

  1. The image name (or tag), which will end up here as spring-boot-docker/spring-boot-docker-start
  2. The directory in which to find the Dockerfile
  3. The resources (files) to copy from the target directory to the docker build (alongside the Dockerfile) – we only need the jar file in this example

Rest all configuration is what a typical spring basic web app stuffs.

Launcher class Application.java:


package com.mynotes;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

Simple controller HomeController.java


package com.mynotes;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

private Logger logger = LogManager.getLogger(this.getClass());
@RequestMapping(value="/")
public String home(){
String msg ="Congratulations! Docker app deployed successfully!";
logger.info(msg);
return msg;
}

}

Now we’ll have to set up our own Docker image. Docker has a simple Dockerfile file format that it uses to specify the “layers” of an image. To do that you create a fine named Dockerfile. I placed mine inside the src/main/docker folder


FROM java:8
<code class="plain">EXPOSE </code><code class="value">8080</code>
ADD spring-boot-docker-start.jar app.jar
RUN sh -c 'touch /app.jar'
ENTRYPOINT ["java","-jar","/app.jar"]

Lets see each line step by step :

  • FROM java:8 – This is our base image or first layer
  • EXPOSE 8080 - port number our app will be started
  • ADD spring-boot-docker-start.jar app.jar – The project JAR file is ADDed to the container as “app.jar” and then executed in the ENTRYPOINT.
  • RUN sh -c ‘touch /app.jar’ – touch our file so it has modification time (Docker creates all container files in an “unmodified” state by default)
  • ENTRYPOINT [“java”,”-jar”,”/app.jar”] –  execute our fat jar

You can build a tagged docker image like:


mvn package docker:build

Notice the build :

docker-spring-boot-basic2

Lets see the images using ‘docker images‘ and run it


docker run -d -p 3000:8080 spring-boot-docker/spring-boot-docker-start

Above we used -d to run in demon mode. Now the app is running on 8080 in the container which is isolated. We have to expose. We used -p for that. You can use the same or change the port as I did. Now the app is runing on 3000.

docker-spring-boot-basic3

Checking browser

docker-spring-boot-basic4

You can stop the app using ‘docker stop <id/name>‘ and then ‘docker rm <id/name>‘. Can remove image also using ‘docker rmi <id>

docker-spring-boot-basic5

 

Advertisements
%d bloggers like this: